Unity 并无直接获取视频某一帧图像的API,所以想要生成缩略图就要自己写方法了,

图片和视频都可以用这种方式生成缩略图,另,转载请标明出处,谢谢。

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using System.IO; public class NewBehaviourScript : MonoBehaviour { VideoPlayer vp;
Texture2D videoFrameTexture;
RenderTexture renderTexture;
void Start()
{
videoFrameTexture = new Texture2D(, );
vp = GetComponent<VideoPlayer>();
vp.playOnAwake = false;
vp.waitForFirstFrame = true; vp.sendFrameReadyEvents = true;
vp.frameReady += OnNewFrame;
vp.Play(); }
int framesValue=;//获得视频第几帧的图片
void OnNewFrame(VideoPlayer source, long frameIdx)
{
framesValue++;
if (framesValue==) {
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 (, , renderTexture.width, renderTexture.height), , );
videoFrameTexture.Apply ();
RenderTexture.active = null;
vp.frameReady -= OnNewFrame;
vp.sendFrameReadyEvents = false;
}
} void OnDisable()
{
if (!File.Exists(Application.persistentDataPath+"/temp.jpg")) {
ScaleTexture (videoFrameTexture, , , (Application.persistentDataPath+"/temp.jpg"));
}
}
//生成缩略图
void ScaleTexture(Texture2D source, int targetWidth, int targetHeight,string savePath)
{ Texture2D result = new Texture2D(targetWidth, targetHeight,TextureFormat.ARGB32, false); for (int i = ; i < result.height; ++i)
{
for (int j = ; 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());
} }

Unity 获得视频的某一帧,生成缩略图的更多相关文章

  1. php 使用 ffmpeg 转换视频,截图,并生成缩略图

    http://blog.csdn.net/toss156/article/details/7003059 把ffmpeg 和  生成缩略图整合了一下. include("ImageResiz ...

  2. 使用AVFoundation仅仅生成缩略图,不进行播放视频(本地和网络文件都可以创建视频缩略图)

    使用MPMoviePlayerController来生成缩略图足够简单,但是如果仅仅是是为了生成缩略图而不进行视频播放的话,此刻使用 MPMoviePlayerController就有点大材小用了.其 ...

  3. C#:获取视频某一帧的缩略图

    读取方式:使用ffmpeg读取,所以需要先下载ffmpeg.网上资源有很多. 原理是通过ffmpeg执行一条命令获取视频某一帧的缩略图. 首先,需要获取视频的帧高度和帧宽度,这样获取的缩略图才不会变形 ...

  4. php如何截取出视频中的指定帧作为图片

    php如何截取出视频中的指定帧作为图片 一.总结 一句话总结:截取视频指定帧为图片,php ffmpeg扩展已经完美实现,并且php ffmpeg是开源的 二.php如何截取出视频中的指定帧作为图片 ...

  5. 从H264码流中获取视频宽高 (SPS帧) 升级篇

    之前写过 <从H264码流中获取视频宽高 (SPS帧)> . 但发现很多局限性,而且有时解出来是错误的. 所以重新去研究了. 用了 官方提供的代码库来解析. 花了点时间,从代码库里单独把解 ...

  6. Avi视频生成缩略图时,提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”

    需求:录制Avi格式视频成功后,使用DirectShow生成缩略图,由于视频录制时,宽高分辨率可调节,所以有些情况下,生成缩略图会抛出异常“尝试读取或写入受保护的内存.这通常指示其他内存已损坏”. 异 ...

  7. PHP -- 七牛云 在线视频 获取某一帧作为封面图

    ### 最近碰到视频处理,需要视频封面? 但用的是七牛云存储视频,索性搜了一下,怎么获取视频的某一帧作为视频的封面图... 发现了七牛官网又自身的接口 ### https://developer.qi ...

  8. 【matlab】使用VideoReader提取视频的每一帧,不能用aviread函数~

    这个问题是matlab版本问题,已经不用aviread函数了~ VideoReader里面没有cdata这个函数! MATLAB不支持avireader了,而且没有cdata这个属性了,详情去官网ht ...

  9. 通视频URL截取第一帧图片

    为了方便直接给UIImage加个类别,以后什么时候使用可以直接调用. #import <UIKit/UIKit.h> @interface UIImage (Video) /** 通过视频 ...

随机推荐

  1. 哪位大兄弟有用 cMake 开发Android ndk的

    一直用 Android studio 开发ndk,但是gradle支持的不是很好,只有experimental 版本支持 配置各种蛋疼.主要每次新建一个module都要修改配置半天.之前也看到过goo ...

  2. [ Linux ] user, password, sudoers

    - 1. Add Linux user,  setup password http://linux.vbird.org/linux_basic/0410accountmanager.php - 2. ...

  3. ActiveMQ学习笔记(19)----Consumer高级特性(一)

    1. Exclusive Consumer 独有消费者:Queue中的消息是按照顺序被分发到consumer的,然而,当你有多个consumers同时从相同的queue中提取消息时,你将失去这个保证. ...

  4. linux上测试磁盘IO速度

    运维工作,经常要测试服务器硬件性能,以此来判断是否存在性能瓶颈. 下面介绍在linux上测试磁盘IO速度的工具: 1.hdparm CentOS中,安装的两种方法: 1) yum安装. # yum i ...

  5. java中的json使用

    import org.json.simple.JSONArray;import org.json.simple.JSONObject; /** * 使用的是json-lib-2.4.jar * @au ...

  6. ArrayList的使用方法

    1.什么是ArrayList    ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和I ...

  7. 全国所有省市县地理坐标Json格式

    https://www.cnblogs.com/yzbubble/p/7707129.html

  8. HDU-1083 Courses 二分图 最大匹配

    题目链接:https://cn.vjudge.net/problem/HDU-1083 题意 有一些学生,有一些课程 给出哪些学生可以学哪些课程,每个学生可以选多课,但只能做一个课程的代表 问所有课能 ...

  9. [POJ2823][洛谷P1886]滑动窗口 Sliding Window

    题目大意:有一列数,和一个窗口,一次能框连续的s个数,初始时窗口在左端,不断往右移动,移到最右端为止,求每次被框住的s个数中的最小数和最大数. 解题思路:这道题是一道区间查询问题,可以用线段树做.每个 ...

  10. Vir-manager 创建虚拟机