Stream copy
Stream copy is a mode selected by supplying the copy parameter to the -codec option.
It makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing. It is useful for changing the container format or modifying container-level metadata. The diagram above will, in this case, simplify to this:
_______ ______________ ________
| | | | | |
| input | demuxer | encoded data | muxer | output |
| file | ---------> | packets | -------> | file |
|_______| |______________| |________|
拷貝模式的過程僅包含demuxing和muxing的過程。
Since there is no decoding or encoding, it is very fast and there is no quality loss.
However, it might not work in some cases because of many factors.
Applying filters is obviously also impossible, since filters work on uncompressed data.
因為不涉及解碼和編碼過程,所以速度很快,而且沒有質量損失。
但是有些場景是不適合的,比如嘗試用filter是不可能的,因為大多數filter在解壓后的數據上使用。
ffmpeg -i in.flv -c copy -f mp4 out.mp4
以上命令,將in.flv進行stream copy,轉封裝成mp4.
Key Frame Seeking
The fastest way to extract a portion of video from a larger video (with a 60 second clip starting 30 seconds in) would be the following:
ffmpeg -ss 30 -i input_vid.mp4 -t 60 -c copy output_clip.mp4
This method is so fast because it uses Key frames when performing seek, and there are far fewer Key frames than Predicted frames.
It also is a stream copy meaning that the encoded data extracted from the original video is taken directly without decoding and then re-encoding.
This feature may be useful for some users who don’t care about frame accuracy
"stream copy" means "duplicate the stream without modification"
示例分析
假設素材如下:
示例1詳細分析
ffmpeg -ss 5 -i input.mp4 -t 5 -vcodec copy -an -y out.mp4
以上目的是為了輸出5秒的視頻(從第5秒到第10秒),但是實際切割出來的實際是10秒(從第0秒到第10秒)。
-vcodec copy 會對視頻啟用拷貝模式,切割時如果要求精度不高,可使用拷貝模式進行切割。
優點就是速度快。
缺點就是誤差比較大,尤其是當gop size比較大的時候。
copy模式的不良使用方式
All-Frame Seeking
A slightly slower and more accurate method of video cutting would be the following:
ffmpeg -i input_vid.mp4 -ss 30 -t 60 -c copy output_clip.mp4
This method uses all-frame seeking, also known as “output seeking” by FFmpeg users. It is also a stream copy, but considers all frames (including Predicted frames) when performing a seek.
This means that your output clip will start with the nearest frame to 30 seconds even if it is a predicted frame.
這種切割方式,首幀可能為P幀。
If you use this command and try to play your output clip, you may notice that the clip starts frozen, or with black frames in the beginning.
...
By stream copying with all-frame seeking we have removed the reference Key frames which came before our first frame.
This is why your video player displays frozen or black frames: the necessary data to represent the first portion of your video isn’t included in the file.
可能出現開始畫面靜止或黑屏的現象。
示例詳解
ffmpeg -i input.mp4 -ss 5 -t 5 -vcodec copy -an -y out.mp4
小結
References:
https://lists.libav.org/pipermail/ffmpeg-user/2005-June/000486.html
http://www.markbuckler.com/post/cutting-ffmpeg/
https://trac.ffmpeg.org/wiki/Seeking#Notes
https://ffmpeg.org/ffmpeg.html#Stream-copy