ffmpeg可以說是一個比較全能的編解碼器,但我在分割視頻的時候視頻被他重新編碼了,明明是copy卻變成了encode。
我使用的命令是這樣的:
1
|
ffmpeg -vcodec copy -acodec copy -ss 01:00:00 -t 00:00:30 -i input_file_h264.mp4 output_file.mp4
|
本來只是想分割出一段視頻的,但卻把分離出來的視頻重新編碼了,畫質也變得慘不忍睹。
查了一些國外資料后發現了問題所在。
在ffmpeg的手冊中對於codec是這樣寫的:
‘-c[:stream_specifier] codec (input/output,per-stream)’
‘-codec[:stream_specifier] codec (input/output,per-stream)’
Select an encoder (when used before an output file) or a decoder (when used before an input file) for one or more streams. codec is the name of a decoder/encoder or a special value copy (output only) to indicate that the stream is not to be re-encoded.
意思就是如果把-codec放到輸出文件的前面就當做編碼器(encoder),在輸入文件前面就當做解碼器(decoder)。再看看我用的命令,-codec是在最前面的,也就是在輸入文件的前面,copy被當做了解碼器,這也是很多人遇到 Unknown decoder ‘copy’ 的原因。copy是一種特殊的編碼器,因此-codec必須放在輸出文件的前面。
還有就是關於-s選項的解釋:
‘-ss position (input/output)’
When used as an input option (before -i), seeks in this input file to position. When used as an output option (before an output filename), decodes but discards input until the timestamps reach position. This is slower, but more accurate.position may be either in seconds or in hh:mm:ss[.xxx] form.
意思就是如果要把-ss作為輸入選項的話要放在-i之前,當做輸出選項的話放在輸出文件之前。我們這是要截取一段視頻,應該當做輸入選項,所以-ss要在-i之前才有效,不然會花費很長一段時間來尋找-ss。
最后分割視頻的命令就變成了:
1
|
ffmpeg -ss 01:00:00 -i input_file_h264.mp4 -vcodec copy -acodec copy -t 00:06:00 output_file.mp4
|
果然,用最新版的ffmpeg也能成功分割。從上面我們可以發現一些選項的順序是非常重要的,錯誤的順序有時會造成截然不同的結果,不止ffmpeg,x264、mencoder等這些編碼器也是如此。