問題描述
我有一個網頁,它(除其他外)需要從用戶上傳的視頻中提取特定的幀.用戶在播放器中尋找 .mp4 的特定部分,然后單擊一個按鈕,然后 ajax 調用被觸發到一個 php 腳本,該腳本獲取 .mp4 和視頻中的確切時間,并使用它來提取一個縮略圖"框架.
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.
我目前的解決方案是使用 php exec
命令:
My current solution is using the php exec
command:
exec("ffmpeg -i $videoPath -ss $timeOffset -vframes 1 $jpgOutputPath");
...這很好用,除了它像糖蜜一樣慢.我的猜測是 ffmpeg 對這項工作來說有點太多了,我可能可以通過利用底層庫或其他東西做得更好......但是我對如何做到這一點毫無想法.
...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.
理想情況下,我不想安裝任何需要真正安裝過程"的東西......即,將可執行文件放入帶有我的網絡應用程序的文件夾中很好,但我寧愿不必實際運行安裝人員.此外,該解決方案應該能夠在 mac、linux 和 windows 上運行(盡管 linux 是首要任務).
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?
謝謝.
推薦答案
當然,你可以編寫一些 C/C++ 代碼并鏈接到 -lav*,基本上創建了一個 ffmpeg 的簡化版本,僅用于提取幀,甚至可以這樣做它作為一個 php 擴展(我也不會以同一個用戶的身份運行它,更不用說在同一個進程中了).但結果不太可能更快,因為您只會避免一些分叉和設置開銷,但您可能的問題實際上是解碼,這仍然是相同的.
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.
相反,您應該首先考慮在快速搜索模式(或快速/準確混合模式)下使用 ffmpeg.他們的維基聲明關于快速搜索:
Instead, you should first look into using ffmpeg in fast seeking mode (or fast/accurate hybrid mode). Their wiki states about fast seeking:
需要在-i前指定-ss參數:
ffmpeg -ss 00:03:00 -i Underworld.Awakening.avi -frames:v 1 out1.jpg
ffmpeg -ss 00:03:00 -i Underworld.Awakening.avi -frames:v 1 out1.jpg
此示例將在周圍某處生成一個圖像幀 (out1.jpg)電影開始的第三分鐘.輸入將是使用關鍵幀解析,速度非常快.缺點是它也將在某個關鍵幀完成搜索,不一定位于在指定時間 (00:03:00),所以搜索不會那么準確正如預期的那樣.
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.
快速搜索不太準確,但要快得多,因為 ffmpeg 在搜索期間實際上不需要解碼(大部分)電影,而快速/準確混合模式是很好的折衷方案.閱讀維基頁面了解所有可用選項.
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.
10 年 6 月 14 日編輯:
從 FFmpeg 2.1 開始,當使用 ffmpeg 進行轉碼(即不是流復制)時,-ss 現在即使用作輸入選項也是準確的.可以使用 -noaccurate_seek 選項恢復以前的行為.(來源)
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)
因此,對于 2.1+,在重新編碼時不再需要混合"搜索以獲得準確的結果(并且保存到 .jpeg
是重新編碼).執行通常的快速搜索 (-ss ... -i ...
) 而不是慢速搜索 (-i ... -ss ...
) 就足夠了).
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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!