php 需要为视频上传创建缩略图(非常简单的代码)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9053048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
need to create thumbnail for video uploading (very simple code)
提问by jq beginner
i have this page (very simple to show what i need) to upload flv files - i read some posts about ffmpeg-php but how to install in on the server if it's the solution and how to use it?
我有这个页面(非常简单地显示我需要的内容)来上传 flv 文件 - 我阅读了一些关于 ffmpeg-php 的帖子,但是如果它是解决方案以及如何使用它,如何在服务器上安装它?
<?php
if(isset($_REQUEST['upload'])){
$tmp_name = $_FILES['video']['tmp_name'];
$name = $_FILES['video']['name'];
$path = "videos/";
move_uploaded_file($tmp_name,$path.$name);
}
else{
?>
<form action="" method="post" enctype="multipart/form-data">
<input name="video" type="file" /> <input name="upload" type="submit" value="upload" />
</form>
<?php
}
?>
and need to create a thumbnail for video uploaded in another folder with the same name any help ? thanks in advance
并且需要为上传到另一个同名文件夹中的视频创建缩略图有什么帮助吗?提前致谢
回答by Tony
Installing ffmpeg should be straightforward. On any Ubuntu/Debian based distro, use apt-get:
安装 ffmpeg 应该很简单。在任何基于 Ubuntu/Debian 的发行版上,使用 apt-get:
apt-get install ffmpeg
After that, you can use it to create a thumbnail.
之后,您可以使用它来创建缩略图。
First you need to get a random time location from your file:
首先,您需要从文件中获取随机时间位置:
$video = $path . escapeshellcmd($_FILES['video']['name']);
$cmd = "ffmpeg -i $video 2>&1";
$second = 1;
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
$total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
$second = rand(1, ($total - 1));
}
Now that your $second
variable is set. Get the actual thumbnail:
现在您的$second
变量已设置。获取实际缩略图:
$image = 'thumbnails/random_name.jpg';
$cmd = "ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
$do = `$cmd`;
It will automatically save the thumbnail to thumbnails/random_name.jpg
(you may want to change that name based on the uploaded video)
它会自动将缩略图保存到thumbnails/random_name.jpg
(您可能希望根据上传的视频更改该名称)
If you want to resize the thumbnail, use the -s
parameter (-s 300x300
)
如果要调整缩略图大小,请使用-s
参数 ( -s 300x300
)
Check out the ffmpeg documentationfor a complete list of parameters you can use.
查看ffmpeg 文档以获取您可以使用的完整参数列表。
回答by Adam Jimenez
Or you can do it in the browser with HTML5's video tag and canvas, see: https://gist.github.com/adamjimenez/5917897
或者你可以在浏览器中使用 HTML5 的视频标签和画布来完成,参见:https: //gist.github.com/adamjimenez/5917897