ffmpeg # 视频切割 # copy模式的一点理解

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"

示例分析

假设素材如下:


image.png
示例1详细分析
ffmpeg -ss 5 -i input.mp4 -t 5 -vcodec copy -an -y out.mp4
image.png
image.png

以上目的是为了输出5秒的视频(从第5秒到第10秒),但是实际切割出来的实际是10秒(从第0秒到第10秒)。

-vcodec copy 会对视频启用拷贝模式,切割时如果要求精度不高,可使用拷贝模式进行切割
优点就是速度快。
缺点就是误差比较大,尤其是当gop size比较大的时候

image.png
image.png
image.png
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
image.png
image.png

小结

image.png

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容