問題描述
我有一項異常艱巨的任務要執行.本以為很容易,結果一切努力都沒有結果.
I have an oddly difficult task to perform. I thought it would be easy, but all my efforts have been fruitless.
我正在將上傳到 php 腳本的各種格式(.avi、.mpg、.wmv、.mov 等)的視頻轉換為單一的 .flv 格式.轉換效果很好,但我遇到的問題是視頻的分辨率.
I'm converting videos uploaded to a php script from various formats (.avi, .mpg, .wmv, .mov, etc.) to a single .flv format. The conversion is working great but what I'm having trouble with is the resolution of the videos.
這是我當前正在運行的命令(使用 PHP 變量):
This is the command I'm currently running (with PHP vars):
ffmpeg -i $original -ab 96k -b 700k -ar 44100 -s 640x480 -acodec mp3 $converted
$original 和 $converted 都包含這些文件的完整路徑.我的問題是,即使源較小,它也總是會轉換為 640x480(就像我告訴它的那樣).顯然,這是在下載視頻時浪費磁盤空間和帶寬.此外,這不考慮輸入視頻的寬高比為 4:3 以外的任何情況,如果我上傳 16:9 視頻,則會導致壓縮"轉換.
Both $original and $converted contain the full paths to those files. My problem is that this always converts to 640x480 (like I'm telling it to) even when the source is smaller. Obviously, this is a waste of disk space and bandwidth when the video is downloaded. Also, this doesn't account for input videos being in any aspect ratio other than 4:3, resulting in a "squished" conversion if I upload a 16:9 video.
我需要做三件事:
- 確定原始視頻的縱橫比.
- 如果不是 4:3,用黑條填充頂部和底部.
- 如果原件的尺寸較大或與原件的寬度/高度相關的寬高比為 4:3(以更接近 640x480 的為準),則轉換為 640x480.
我已經在一些視頻上運行了 ffmpeg -i
,但我沒有看到一致的格式或位置來查找原始分辨率.一旦我能夠弄清楚這一點,我知道我可以做數學"來找出正確的尺寸并指定填充以使用 -padttop、-padbottom 等來固定縱橫比.
I've run ffmpeg -i
on a few videos, but I don't see a consistent format or location to find the original's resolution from. Once I'm able to figure that out, I know I can "do the math" to figure out the right size and specify padding to fix the aspect ratio with -padttop, -padbottom, etc.
推薦答案
這對我有用:
$data = 'ffmpeg output';
$matches = array();
if (!preg_match('/Stream #(?:[0-9.]+)(?:.*): Video: (?P<videocodec>.*) (?P<width>[0-9]*)x(?P<height>[0-9]*)/',$data,$matches)
preg_match('/Could not find codec parameters (Video: (?P<videocodec>.*) (?P<width>[0-9]*)x(?P<height>[0-9]*))/',$data,$matches)
這可能不總是有效,但它在大多數情況下都有效,這對我來說已經足夠了:)
This might not always work, but it works most of the times, which was good enough in my case :)
這篇關于PHP 和 FFMPEG - 執行智能視頻轉換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!