Unity 视频缩略图获取

 IEnumerator GetVideoThumb(string urlpath,int index)
    {
        print(index);
       Transform go= Instantiate(VideoPlayerPrefab);
       VideoPlayer tmpVp= go.gameObject.AddComponent<VideoPlayer>();
        go.gameObject.AddComponent<IndexNumber>();
        go.GetComponent<IndexNumber>().index = index;

        tmpVp.source = VideoSource.Url;
        tmpVp.url = urlpath;
        tmpVp.sendFrameReadyEvents = true;
        //绑定的事件会不断调用
        tmpVp.frameReady += OnNewFrame;
        tmpVp.Play();
        yield return null;
        //while (tmpVp.texture == null|| tmpVp.isPlaying==false)
        //{
        //    yield return new   WaitForSeconds(1.5f);
        //}

        //Texture2D videoFrameTexture = new Texture2D(Screen.width, Screen.width,TextureFormat.ARGB32, false);
        //RenderTexture tmp = RenderTexture.GetTemporary(
        //            Screen.width, Screen.width,
        //            0,
        //            RenderTextureFormat.Default,
        //            RenderTextureReadWrite.sRGB);
        将texture的像素复制到RenderTexture
        //Graphics.Blit(tmpVp.texture, tmp);

         RenderTexture.active = tmp;
        //videoFrameTexture.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
        //videoFrameTexture.Apply();

        //img.sprite = Sprite.Create(videoFrameTexture, new Rect(0,0, videoFrameTexture.width, videoFrameTexture.height), new Vector2(0.5f, 0.5f));
         RenderTexture.active = null;
         释放临时RenderTexture
        //RenderTexture.ReleaseTemporary(tmp);

        //Destroy(go.gameObject);

    }

    void OnNewFrame(VideoPlayer source, long frameIdx)
    {
         RenderTexture  renderTexture = source.texture as RenderTexture;
      
        if (frameIdx >=3)  //要用 > ,frameIdx可能会跳过这个数字
        {
          
            Texture2D videoFrameTexture = new Texture2D(Screen.width, Screen.width, TextureFormat.ARGB32, false);
            videoFrameTexture.Resize(renderTexture.width, renderTexture.height);
            RenderTexture.active = renderTexture;
            videoFrameTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
            videoFrameTexture.Apply();

            AllBtnObj[source.GetComponent<IndexNumber>().index].transform.GetComponent<Image>().sprite = Sprite.Create(videoFrameTexture, new Rect(0, 0, videoFrameTexture.width, videoFrameTexture.height), new Vector2(0.5f, 0.5f));

            source.frameReady -= OnNewFrame;
            source.sendFrameReadyEvents = false;
            Destroy(source.gameObject);
        }
    }


//调用
   void InitSuolueTu()
    {
        for (int i = 0; i <AllBtnObj.Count; ++i)
        {
            print(path[i]);  //E:/UnityProgram/KinectProject/Assets/StreamingAssets/Video/abc.mp4
            StartCoroutine( GetVideoThumb(path[i], i));
        }

    }


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IndexNumber : MonoBehaviour {

   public int index;
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值