【Unity】获取视频某一帧的图片

这篇具有很好参考价值的文章主要介绍了【Unity】获取视频某一帧的图片。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

public class Test : MonoBehaviour
{
    #region 获取视频某一帧图片
    VideoPlayer video;//视频
    Texture2D videoTextrue;//2D图片
    RenderTexture renderTexture;
    Sprite sprite;
    public Image image;

   
    void Start()
    {
        InitVideoToImage();
    }
    void InitVideoToImage()
    {
      
        videoTextrue = new Texture2D(2, 2);
        video = GetComponent<VideoPlayer>();//播放的视频
        video.playOnAwake = false;
        video.waitForFirstFrame = true;
        video.sendFrameReadyEvents = true;//启用 frameReady 事件。  如果设置为 true,则在准备绘制帧时将调用使用 VideoPlayer.frameReady 注册的任何委托。如果设置为 false,则不会调用已注册的委托
        video.frameReady += GetNumTextrue;// 新的一帧准备好时被执行。
        video.Play();
    }
    int framesValue = 0;//获得视频第几帧的图片
    /// <summary>
    /// 获取到framesValue帧的Textrue
    /// </summary>
    void GetNumTextrue(VideoPlayer source, long frameIdx)
    {
        framesValue++;
        if (framesValue == 1)
        {
            renderTexture = source.texture as RenderTexture;//放置视频内容的内部纹理。
            if (videoTextrue.width != renderTexture.width || videoTextrue.height != renderTexture.height)
            {
                videoTextrue.Resize(renderTexture.width, renderTexture.height);
            }
           // 当前处于活动状态的渲染纹理。
            RenderTexture.active = renderTexture;
            videoTextrue.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);//将屏幕像素读取到保存的纹理数据中。
            videoTextrue.Apply();//应用像素
            RenderTexture.active = null;
            video.frameReady -= GetNumTextrue;
            video.sendFrameReadyEvents = false;
        }
        StartCoroutine(ScaleTexture(videoTextrue, 1920, 1080));
    }
    /// <summary>
    /// 生成缩略图
    /// </summary>
    IEnumerator ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
    {
        yield return new WaitForSeconds(2);

        Texture2D result = new Texture2D(targetWidth, targetHeight, TextureFormat.ARGB32, false);

        for (int i = 0; i < result.height; ++i)
        {
            for (int j = 0; j < result.width; ++j)
            {
                Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
                result.SetPixel(j, i, newColor);
            }
        }
        result.Apply();

        sprite = Sprite.Create(result, new Rect(0, 0, targetWidth, targetHeight), new Vector2(0.5f, 0.5f));
        image.sprite = sprite;
       
    }
    #endregion

}

增加获取图片后的委托

using System.IO;
using UnityEngine;
using UnityEngine.Video;

/// <summary>获取视频预览</summary>
public class VideoPreview : MonoBehaviour
{
    /// <summary>获得视频第几帧的图片</summary>
    private int framesValue = 0;
    private VideoPlayer videoPlayer;
    private Texture2D videoFrameTexture;
    private RenderTexture renderTexture;
    /// <summary>视频保存路径</summary>
    private string videoUrl;
    /// <summary>视频预览图存放路径</summary>
    private string videoPreviewPath;

    public delegate void VideoThumbCompleteEvent(Texture2D texture);
    private VideoThumbCompleteEvent onCompleted;

    public static VideoPreview Create()
    {
        GameObject go = new GameObject("VideoPreview");
        VideoPreview videoPreview = go.AddComponent<VideoPreview>();
        return videoPreview;
    }

    private void OnDestroy()
    {
        videoFrameTexture = null;
        renderTexture = null;
    }

    /// <summary>
    /// 获取视频缩略图
    /// </summary>
    /// <param name="videoUrl">视频地址</param>
    /// <param name="completed">生成缩略图完成</param>
    public void VideoThumb(string videoUrl, VideoThumbCompleteEvent completed)
    {
        this.onCompleted = completed;
        this.videoUrl = videoUrl;
        string[] nameArr = videoUrl.Split('/');
        string imageName = nameArr[nameArr.Length - 1];// 缩略图的名字等于视频url的最后
        videoPreviewPath = Application.persistentDataPath + "/" + imageName + ".png";

        videoFrameTexture = new Texture2D(2, 2);
        videoPlayer = gameObject.AddComponent<VideoPlayer>();
        videoPlayer.source = VideoSource.Url;
        videoPlayer.url = videoUrl;
        videoPlayer.playOnAwake = false;
        videoPlayer.waitForFirstFrame = true;

        videoPlayer.sendFrameReadyEvents = true;
        videoPlayer.frameReady += onFrameReady;
        videoPlayer.Play();
    }

    private void onFrameReady(VideoPlayer source, long frameIdx)
    {
        framesValue++;
        if (framesValue == 5)
        {
            renderTexture = source.texture as RenderTexture;
            if (videoFrameTexture.width != renderTexture.width || videoFrameTexture.height != renderTexture.height)
            {
                videoFrameTexture.Resize(renderTexture.width, renderTexture.height);
            }
            RenderTexture.active = renderTexture;
            videoFrameTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
            videoFrameTexture.Apply();
            RenderTexture.active = null;
            videoPlayer.frameReady -= onFrameReady;
            videoPlayer.sendFrameReadyEvents = false;

            scaleTexture(videoFrameTexture, 600, 300, videoPreviewPath);
        }
    }

    //生成缩略图
    private void scaleTexture(Texture2D source, int targetWidth, int targetHeight, string savePath)
    {
        Texture2D result = new Texture2D(targetWidth, targetHeight, TextureFormat.ARGB32, false);
        for (int i = 0; i < result.height; ++i)
        {
            for (int j = 0; j < result.width; ++j)
            {
                Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
                result.SetPixel(j, i, newColor);
            }
        }
        result.Apply();
        File.WriteAllBytes(savePath, result.EncodeToJPG());
        if (onCompleted != null) onCompleted(result);
        Destroy(gameObject);
    }

}

 测试:文章来源地址https://www.toymoban.com/news/detail-611875.html


using UnityEngine;
using UnityEngine.UI;
 
public class Test : MonoBehaviour
{
    /// <summary>用来显示视频1缩略图UI</summary>
    public RawImage rawImage01;
    
 
    /// <summary>视频路径1(可以本地或网络)</summary>
    private string videoUrl1 = "/Users/chenyongliang/Desktop/相册/1657100515231.mp4";
   
 
    private void Start()
    {
        VideoPreview videoPreview1 = VideoPreview.Create();
        VideoPreview videoPreview2 = VideoPreview.Create();
        VideoPreview videoPreview3 = VideoPreview.Create();
 
        videoPreview1.VideoThumb(videoUrl1, (texture) =>
        {
            rawImage01.texture = texture;
        });

}

到了这里,关于【Unity】获取视频某一帧的图片的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 提取视频中的某一帧画面,留住视频中的美好瞬间

    你是否曾经被视频中的某一帧画面深深吸引,却又惋惜于无法将其永久保存?现在,有了我们【媒体梦工厂】,这一遗憾将成为过去,这个软件可以提取视频中的某一帧保存为图片,为你留住那些稍纵即逝的美好。 所需工具: 一个【媒体梦工厂】软件 视频素材 操作步骤:

    2024年01月25日
    浏览(34)
  • js截取video视频某一帧做封面的简单案例

    可以使用 canvas 元素来截取视频某一帧并生成封面。 首先,在 video  标签上设置视频源地址和自动播放属性: 然后,在 canvas  标签上定义宽高和样式,并通过 JavaScript 获取视频元素和 canvas 元素: 接着,定义一个函数来截取视频某一帧,并将其渲染到 canvas 上: 在需要生成

    2024年02月04日
    浏览(39)
  • Unity项目中查找所有使用某一张图片的材质球,再查找所有使用材质球的预设

    废话少说,直接上代码。 上述代码中,我们首先使用 AssetDatabase.FindAssets 方法通过过滤类型(\\\"t:Material\\\")获取所有材质球的GUID。然后遍历每个GUID,加载对应的材质球,判断该材质球是否引用了指定的图片。我们定义了一个辅助方法 HasTexture 来检查材质球中的纹理是否引用了

    2024年02月14日
    浏览(25)
  • 【音视频原理】视频帧的 I P B 帧概念 ① ( 码率 / 帧率 / 分辨率 视频信息 | I 帧 - 内部编码帧 | I 帧 - 关键帧压缩法 | P 帧 - 前向预测帧 )

    使用 MediaInfo 软件 打开一个 mp4 文件 , 查看其属性 ; 该视频的属性如下 : 码率 : 212kb/s , 这是 视频文件 的 视频信息 在 单位时间内的 数据流量 , 码率越大 , 单位时间内采样率越大 , 数据流精度越高 , 视频质量越高 ; 视频帧率 : 5fps , 1 秒中有 5 帧的信息 , 帧率越高 , 视频越流畅

    2024年02月20日
    浏览(33)
  • Unity Metaverse(八)、RTC Engine 基于Agora声网SDK实现音视频通话

    本文介绍如何在Unity中接入声网SDK,它可以应用的场景有许多,例如直播、电商、游戏、社交等,音视频通话是其实时互动的基础能力。 如下图所示,可以在官网中选择Unity SDK进行下载,也可以到 Unity Asset Store 资源商店中搜索 Agora SDK 进行下载导入。 在官网中前往 Console 控制

    2024年02月09日
    浏览(43)
  • 【音视频原理】视频帧的 I P B 帧概念 ② ( B 帧 - 双向内插帧 | 画面组 Group of Pictures 概念 | 各类型帧解码错误影响 | 画面组编解码顺序 | 常用视频压缩算 )

    B 帧 全称 \\\" 双向内插帧 ( Bi-directional Predicted Frames ) \\\" , 采用 双向预测编码方式 , 也就是 B 帧 记录的是 本帧 B 帧 与 前后 I 帧 或 P 帧 的差别 ; 注意 : B 帧 需要依赖于其前的最近的一个 I 帧 或者 P 帧 及其后的最近的一个 P 帧 进行解码 , B 帧 不能 依赖与 B 帧 ; B 帧 依赖的 前一

    2024年01月25日
    浏览(34)
  • vue 获取上传视频的第一帧做为视频封面

    上一篇文章记录了vue上传视频,接下来,需要在上传成功后截取视频第一帧做为视频封面。 具体实现如下: 上传视频之后,在成功回调函数中拿到视频地址, video.src=url ,然后,使用canvas截取图片。 截取视频第一帧使用的是canvas,相关步骤如下: canvas 可以用来截取图片。

    2024年02月03日
    浏览(35)
  • 如何将视频的每一帧提取成图片

    有时候我们需要将视频按帧提取出来,但是一个普通的24帧的视频每秒就有24张图片,一分钟的视频就有1440张图片,如果一帧一帧的截取,那无疑十分的浪费时间,而且如何按帧播放也让人头疼,那么如何做到呢,这里我们采用ffmpeg来进行操作,关于这是什么就不赘述了,话

    2024年02月11日
    浏览(58)
  • JavaScrip获取视频第一帧作为封面图

    在JavaScript中,你可以使用HTML5的video元素来加载视频,然后使用Canvas来捕获视频的第一帧作为封面图。以下是一个简单的例子: 请注意,这个例子中使用了loadeddata事件,该事件在视频的第一帧加载完成后触发。这里创建了一个Canvas元素,通过drawImage方法将视频的第一帧绘制在

    2024年01月16日
    浏览(24)
  • video截取视频第一帧作为播放前默认图片

    重要!不设置会导致第一帧图片不显示 实现js 附上全部代码

    2024年02月12日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包