1、videoplayer的RenderMode设置为APIOnly(这种模式不会生成视频视图,但是可以获取到数据)
2、设置videoplayer的URL或者videoclip
3、调用videoplayer.Prepare()
4、监听videoplayer.prepareCompleted(用isPrepared也可以)
5、在准备完成后调用videoplayer.Pause(),此时视频数据所处第0帧,可以在这里修改目标帧数,并取到对应位置的图片
小demo:
void Start()
{
_videoPlayer.source = VideoSource.Url;
_videoPlayer.renderMode = VideoRenderMode.APIOnly;
_videoPlayer.playOnAwake = false;
}
public void TakeFrameTexture(string path,float percent,Action<Texture> onFinish)
{
_videoPlayer.url = path;
_videoPlayer.Prepare();
_videoPlayer.prepareCompleted += source =>
{
_videoPlayer.Pause();
long targetFrame = (long)(_videoPlayer.frame * percent);
_videoPlayer.frame = targetFrame;
Texture tex = null;
//按着_videoPlayer.texture的数据写一张Texture
onFinish.Invoke(tex);
};
}
参考:Video - how to extract frames from a video - Unity Forum
|