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. php面向对象的基础:OOP的常量

    常量(constant) 用来表示不会改变的值.对于从该类实例化的任何对象来说,常量值在这些对象的整个生命周期中都保持不变. class Computer{ const PI=3.1415926; } ...

  2. springmvc两种非注解的处理器适配器

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  3. C# 将string 转换为二维码图片,然后转为base64字符串编码 。

    需在nuget 添加此dll ///content字符串 public static string GetQRCode(string content, int moduleSize = 9) { va ...

  4. 洛谷P2766 最长不下降子序列问题 网络流_DP

    Code: #include<cstdio> #include<iostream> #include<vector> #include<algorithm&g ...

  5. layui层级

    zIndex:layer.zIndex, success : function(layero){ var zIndex = layer.index; $(layero).css(‘z-index’,z ...

  6. 推荐学习《组织与管理研究的实证方法(第2版)》中文PDF

    在写文章论文时,会涉及到观点论证,需要掌握一些实证方法. 建议学习<组织与管理研究的实证方法(第2版)>,对管理研究中涉及的方法进行了介绍,例如实验室研究,二手数据的研究,实地研究等,这对 ...

  7. harbor 安装过程

    [root@master01 harbor]# ./prepare  Clearing the configuration file: ./common/config/adminserver/env ...

  8. pandas 1 基本介绍

    import numpy as np import pandas as pd pd.Series() 构造数据 s = pd.Series([1, 3, 5, np.nan, 44, 1]) prin ...

  9. python 面向对象 封装

    什么是封装 广义上的封装:代码的保护,面对对象的思想本身就是 只让自己的对象能调自己类的方法 狭义上的封装:将属性和方法藏起来 私有属性/私有方法 python没有真正意义的私有属性,可以通过调用实例 ...

  10. 【BZOJ 1193】 [HNOI2006]马步距离

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 原问题可以等价为两个点. 然后其中一个点要移动到另外一个点. 那么我们可以把左下角那个点(对称总是可以得到一个点在左下角)放在原点的 ...