简单实现 Unity 视频的缩略图
最编程
2024-03-10 12:11:03
...
一.缩略图如何获取
1.第一种就是直接通过手动截屏,然后对截好的图片进行保存,接着再通过配置表进行配置
2.第二种就是直接通过代码动态保存视频的某一帧为视频的缩略图。
(我在这里采取的是第二种)
首先就是我们得把我们所需要进行处理的视频的路径配置好
这里我省略了如何获取表述数据的方式代码比较多
视频路径我们配置完成之后我们就可以通过携程的方式把每个视频的0.1秒的位置进行保存用来做视频的缩略图。
IEnumerator IESaveVideoThumb(string filePath, Action<string,string,Sprite> call)
{
foreach (var file in videoInfos)
{
mediaPlayer.m_VideoPath = file.VideoPath;
mediaPlayer.OpenVideoFromFile(mediaPlayer.m_VideoLocation, mediaPlayer.m_VideoPath, mediaPlayer.m_AutoStart);
if (!File.Exists(Path.GetDirectoryName(file.VideoPath) + "/" + Path.GetFileNameWithoutExtension(file.VideoPath) + ".png"))
{
yield return new WaitForSeconds(0.2f);
yield return null;
// mediaPlayer.Info.GetDurationMs() 取的是毫秒
float durationMs = mediaPlayer.Info.GetDurationMs() / 1000f;
float ms = durationMs < 1 ? durationMs : 0.1f;
//ms= 1;
//取视频中1秒位置保存为图片
Texture2D t = mediaPlayer.ExtractFrame(null, ms);
Sprite sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
string path = Path.GetDirectoryName(file.VideoPath) + "/" + Path.GetFileNameWithoutExtension(file.VideoPath) + ".png";
if (!Directory.Exists(Path.GetDirectoryName(file.VideoPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(file.VideoPath));
}
if (!File.Exists(path))
{
//写入文件
try
{
File.WriteAllBytes(path, t.EncodeToJPG());
Debug.Log(filePath);
}
catch
{
Debug.LogError(filePath);
}
}
else
{
Debug.Log("文件已存在!");
}
yield return new WaitForSeconds(0.1f);
//调用回调函数 刷新UI界面
call?.Invoke(path, file.VideoPath, sprite);
}
else
{
call?.Invoke(Path.GetDirectoryName(file.VideoPath) + "/" + Path.GetFileNameWithoutExtension(file.VideoPath) + ".png", file.VideoPath, null);
}
}
Debug.Log(mediaPlayer.Control.IsPlaying());
if (mediaPlayer.Control.IsPlaying())
{
mediaPlayer.Control.Seek(0);
mediaPlayer.Control.Stop();
Debug.Log(mediaPlayer.Control.IsPlaying());
}
}
二.如何将缩略图刷新到UI
上面我们已经得到了所有视频的缩略图,接下来就是怎么把缩略显示到UI上面,大概思路就是两个方向,我们第一次运行的时候和第一次运行之后两种情况,第一次时候我们需要把图片写入到本地,得先写入,这里我是在第一次时候直接把每次的截取的图片写入之前把Texture2D 转换为Sprite,把他为参数直接刷新到UI上面
//取视频中1秒位置保存为图片
Texture2D t = mediaPlayer.ExtractFrame(null, ms);
Sprite sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
public void UpdateUI(string path,string filePath,Sprite s)
{
//加载视频缩略图
disPlay.gameObject.SetActive(false);
GameObject gameObject = Instantiate(obj, content);
gameObject.name = typeof(PlayVideoItem).Name;
FileInfo fileinfo = new FileInfo(path);
gameObject.GetComponent<PlayVideoItem>().Init(filePath, "file://" + fileinfo.FullName,s);
}
第一次之后的情况则是直接通过保存好的图片的路径进行本地下载,然后在刷新到UI上面
public void Init(string videoPath,string url,Sprite s)
{
path = videoPath;
this.url = url;
if(s!=null)
{
playButton.GetComponent<Image>().sprite = s;
}
else
{
StartCoroutine(LoadAssetsOutside(url));
}
}
IEnumerator LoadAssetsOutside(string filePath)
{
WWW www = new WWW(filePath);
yield return www;
Texture2D t = www.texture;
Sprite sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
playButton.GetComponent<Image>().sprite = sprite;
www.Dispose();
}
三.效果图:
推荐阅读
-
Qt 实现的简单视频播放器,功能包括选择视频、播放、暂停、前进、后退、拖动进度条、显示视频长度等
-
60岁拿2000元创业,靠 "神 "做生意,如今年赚24亿--如今的天堂伞集团,无论是口碑还是销量,都是业内首屈一指的 "大牛"。王斌章的一把伞,仅去年的销售额就实现了近6亿元的好成绩。雨伞销售更是占据了中国所有雨伞行业的最大份额。可以说,如今的天堂伞,无论是质量还是口碑,都堪称行业翘楚。 一把天堂伞,如何在王斌章手中打出新高度,玩出大生意?总结起来,两点制胜法宝:一是质量绝对保证,二是服务有保障。这看似很简单,但几十年来不折不扣地执行,特别是在保护伞这个不太重要的对象上,想要做到始终如一,难度很大。这也是为什么中国只有一个王斌章被称为 "全球伞王 "的原因。 天堂伞成立之初,销售场地选在附近的一个广场,以摆摊的形式销售王斌章亲手制作的杭州伞。凭借几十年的手艺和严谨的态度,加上上乘的伞料,即使价格比普通伞高3倍5倍,也打开了市场,积累了第一批人气和资金。
-
以相反顺序播放 GIF 的简单实现
-
Unity Ml-Agent 猫和老鼠的实现
-
❤️ 创意网页:制作简单美观的网页旋转图(HTML 简单实现旋转图)操作简单可直接使用!
-
AVFoundation 框架分析(二十二)--源代码的简单视频流预览和播放示例(2)
-
深入分析 Tomcat(五)分析 Servlet 容器并实现简单的 Context 和 Wrapper 容器
-
坏消息:百度影音、快手关闭P2P服务器!-最近 P2P 老大百度影音*高度重视版权保护,采取通过多项技术创新实现对版权视频内容的保护,大力扶持正版,打击盗版。(包括停止关闭 P2P 服务器)。
-
用 PHP 实现用户注册表单验证功能的简单示例
-
最简单易懂的 24 点解决方案(Java 实现)