从视频中提取特定帧的最快方法(PHP / ffmpeg / anything) [英] Fastest way to extract a specific frame from a video (PHP/ffmpeg/anything)

查看:639
本文介绍了从视频中提取特定帧的最快方法(PHP / ffmpeg / anything)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,其中(除其他外)需要从用户上传的视频中提取特定的框架。用户尝试播放器中的.mp4的特定部分,然后单击一个按钮,并且ajax调用被触发到一个php脚本,该脚本采用.mp4,以及从视频中确切的时间,并将其用于提取一个缩略图框架。



我目前的解决方案是使用php exec 命令:



exec(ffmpeg -i $ videoPath -ss $ timeOffset -vframes 1 $ jpgOutputPath);



...它只是很棒,除了它像糖蜜一样缓慢。我的猜测是,ffmpeg对于这项工作来说有点太多了,我可以通过利用底层的图书馆或者某些东西来做得更好。但是我没有理由如何做到这一点。



理想情况下,我不想安装需要真正安装过程的任何东西,即将可执行文件放到我的网页应用程序的文件夹中,但我宁愿不必须实际运行安装程序。此外,该解决方案应该能够运行在mac,linux和windows(虽然linux是首要任务)。



我可以做些什么来加速这个过程? / p>

谢谢。

解决方案

当然你可以编写一些C / C ++和链接到-lav *,基本上创建一个ffmpeg的简化版本只是为了提取帧,甚至可以作为一个php扩展(也不会像同一个用户一样运行,更不用说在同一个进程)。但结果是不太可能更快,因为你只会避免一些分支和设置开销,但你可能的问题实际上是解码,这仍然是一样的。



相反,您应该首先查看在快速搜索模式(或快速/准确的混合模式)中使用ffmpeg。 他们的wiki状态关于快速寻找:


-i参数需要在-i 之前指定:



ffmpeg -ss 00:03:00 - i Underworld.Awakening.avi -frames:v 1 out1.jpg



此示例将在第三分钟左右的
周围产生一个图像框(out1.jpg)从电影的开始。输入将使用关键帧解析
,这非常快。缺点是它
也将完成在某些关键帧的寻找,不一定在指定的时间(00:03:00)位于
,所以寻求不会像预期的那样准确


快速寻找不太准确,但是死了很快,因为ffmpeg实际上不需要解码(大部分)电影在寻求的同时,快速/准确的混合模式是很好的妥协。有关所有可用选项,请参阅维基页面



编辑14/06/10


从FFmpeg 2.1开始,用ffmpeg流复制),即使用作输入选项,-ss也是准确的。可以使用-noaccurate_seek选项恢复以前的行为。
(来源)


因此,对于2.1+,对于重新编码(并保存到 .jpeg 是重新编码)。通常的快速寻找( -ss ... -i ... )而不是缓慢寻找( -i ...)就足够了-ss ... )。


I have a web page, which (among other things) needs to extract a specific frame from a user-uploaded video. The user seeks to a particular part of a .mp4 in the player, then clicks a button, and an ajax call gets fired off to a php script which takes the .mp4, and the exact time from the video, and uses that to extract a "thumbnail" frame.

My current solution is using the php exec command:

exec("ffmpeg -i $videoPath -ss $timeOffset -vframes 1 $jpgOutputPath");

...which works just great, except it's as slow as molasses. My guess is that ffmpeg is a little too much for the job, and I might be able to do better by utilizing the underlying libraries or something... however I have zero idea how to do that.

Ideally I don't want to have to install anything that requires a real "installation process"... i.e., dropping an executable into the folder with my web app is fine, but I'd rather not have to actually run an installer. Also, the solution should be able to run on mac, linux and windows (though linux is the top priority).

What can I do to speed this process up?

Thanks.

解决方案

Of course you could code up some C/C++ and link to -lav*, basically creating a simplified version of ffmpeg just for extracting frames, and maybe even do it as a php extension (also I wouldn't run it as the same user, let alone in the same process). But the result is very unlikely to be faster, because you would only avoid some forking and setup overhead, but your likely problem is actually the decoding, which would still be the same.

Instead, you should first look into using ffmpeg in fast seeking mode (or fast/accurate hybrid mode). Their wiki states about fast seeking:

The -ss parameter needs to be specified before -i:

ffmpeg -ss 00:03:00 -i Underworld.Awakening.avi -frames:v 1 out1.jpg

This example will produce one image frame (out1.jpg) somewhere around the third minute from the beginning of the movie. The input will be parsed using keyframes, which is very fast. The drawback is that it will also finish the seeking at some keyframe, not necessarily located at specified time (00:03:00), so the seeking will not be as accurate as expected.

Fast seeking is less accurate, but a damn lot faster, as ffmpeg will not actually need to decode (most of) the movie during the seek, while fast/accurate hybrid mode is good compromise. Read the wiki page for all available options.

Edit 14/06/10:

As of FFmpeg 2.1, when transcoding with ffmpeg (i.e. not stream copying), -ss is now accurate even when used as an input option. Previous behavior can be restored with the -noaccurate_seek option. (source)

So with 2.1+, "hybrid" seeking shouldn't be required anymore for accurate results when it comes to re-encodes (and saving to .jpeg is a re-encode). It is enough to do the usual fast seeking (-ss ... -i ...) instead of slow seeking (-i ... -ss ...).

这篇关于从视频中提取特定帧的最快方法(PHP / ffmpeg / anything)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆