大家好,溢出者们,
我要完成的工作的简要概述;我有一个网站将接受视频上传,上传会转换为mp4格式,以便使用许多可用的播放器之一在网络上进行统一播放。那部分很好,花花公子。
现在的问题是,我想在用户单击播放视频之前向用户显示该视频的简短预览(动画gif)。我现在使用的代码是
ffmpeg -i test.mp4 -vf scale=150:-1 -t 10 -r 1 test.gif
用于创建固定宽度为150px的缩放动画gif,速率为每秒1帧,但这仅是视频的前10秒的动画。我正在尝试做一些扩展帧间隙以覆盖整个视频长度的事情,但是创建的动画礼物的长度不超过10秒。
例如,说我有一个30秒的视频,我希望gif长10秒,但要覆盖整个30秒的帧,所以它可能从第3帧或第3秒钟开始,并在gif中创建一个帧,然后在6秒钟时在视频中创建另一个帧,然后在另一个帧中播放9秒,依此类推,最终结果是
example video 30 seconds long example video 1 minute 45 second long
video position - gif frame/per second video position - gif frame/per second
00:03:00 1 00:10:50 1
00:06:00 2 00:21:00 2
00:09:00 3 00:31:50 3
00:12:00 4 00:42:00 4
00:15:00 5 00:52:50 5
00:18:00 6 01:03:00 6
00:21:00 7 01:13:50 7
00:24:00 8 01:24:00 8
00:27:00 9 01:34:50 9
00:30:00 10 01:45:00 10
3 second interval between frames 10.5 second interval between frames
最终您会得到10秒钟长的gif动画gif,无论视频的时长如何,它都会显示整个视频的预览。基本上可以归结为,video length / 10 (length of desired animated gif) = interval to use between frames
但是我不知道如何使用这些数据来解决我的问题...
那么,有谁对如何相对轻松地实现这一想法有何想法或建议呢?我可以通过以下方式来做到这一点:通过代码计算长度,然后运行命令从所需的视频中提取每个单独的帧,然后从图像中生成gif,但我希望能够仅用一个命令就可以完成全部操作。谢谢。
因此,我最终只是通过找到该脚本的代码来完成此操作(http://www.alberton.info/video_preview_as_animated_gif_with_ffmpeg_and_spl.html#.UxnU_IXYNyI),这使您可以轻松地根据帧中的百分比指定视频中的位置提取帧之间的间隔,我可以使用以下方法完成我的问题。
// where ffmpeg is located, such as /usr/sbin/ffmpeg
$ffmpeg = '/usr/bin/ffmpeg';
// the input video file
$video = 'sample.avi';
// extract one frame at 10% of the length, one at 20% and so on
$frames = array('10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%');
// set the delay between frames in the output GIF in ms (60 = 1 min)
$joiner = new Thumbnail_Joiner(60);
// loop through the extracted frames and add them to the joiner object specifying
// the max width/height to make the thumb based on the dimensions of the video
foreach (new Thumbnail_Extractor($video, $frames, '150x150', $ffmpeg) as $key => $frame) {
$joiner->add($frame);
}
$joiner->save('sample.gif');
以上内容将进入视频长度的10%,获取动画GIF的第一张图像的帧,创建60ms / 1sec的延迟,进入视频长度的20%,并针对每个指定的百分比重复该过程会产生任意长度的视频,该视频具有10秒钟长的动画GIF,其中包含视频中的10帧,且间隔为10%,每秒钟显示1秒钟。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句