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 }
增加获取图片后的委托文章来源:https://www.toymoban.com/news/detail-611875.html
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模板网!