ffmpeg 输出视频相关信息

一、前言

ffmpeg是强大的视频支持库,功能上划分为媒体格式(libavformat,多媒体文件的读包和写包)、编解码(libavcodec,音视频的编解码)、核心工具(libutils,公用的功能函数)、设备(libavdevice,管理音视频设备的操作)和后处理(libavfilter, libswscale, libpostproc,音视频后处理),参考:FFMPEG详解(完整版)_davidullua的博客-CSDN博客_ffmpeg

PyAV提供ffmpeg的python接口,是使用Cython封装了ffmpeg的接口,实际调用的是ffmpeg:

在python中优雅的使用ffmpeg:PyAV - 简书

容器格式 V.S. 编解码格式

容器格式(container: avi,mp4,wmv,mpeg\mpg)

编解码格式(codec: mpeg4、h263、h264、h265)

参考:

视频文件的容器格式和编码格式 - 简书

多媒体container和codec概念_lelexin的博客-CSDN博客

更深入的了解可以参见博客:FFMPEG零基础入门_class_brick的博客-CSDN博客_ffmpeg入门

二、FFmpeg简介

“A complete, cross-platform solution to record, convert and stream audio and video.” 可以实现视频和音频的转码、裁剪、合并等。

安装:

sudo apt-get install ffmpeg

如下依赖项自动安装:

The following additional packages will be installed:
  libavdevice-ffmpeg56 libavfilter-ffmpeg5 libbs2b0 libflite1 libopenal-data
  libopenal1 libsodium18 libvdpau1 libzmq5 mesa-vdpau-drivers vdpau-driver-all
Suggested packages:
  ffmpeg-doc libvdpau-va-gl1 nvidia-vdpau-driver
  nvidia-legacy-340xx-vdpau-driver
The following NEW packages will be installed:
  ffmpeg libavdevice-ffmpeg56 libavfilter-ffmpeg5 libbs2b0 libflite1
  libopenal-data libopenal1 libsodium18 libvdpau1 libzmq5 mesa-vdpau-drivers
  vdpau-driver-all
0 upgraded, 12 newly installed, 0 to remove and 30 not upgraded.
Need to get 17.3 MB of archives.

三、查询视频信息

3.1如果容器中储存了视频的相关信息,可以快速通过容器查询:

ffprobe -show_streams -i video.h264
ffprobe -v error -show_format -show_streams video.h264

3.2如果容器中没有存储相关信息,只能读入视频计算相关信息:

ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 video.h264

或者

ffmpeg -i video.h264 -map 0:v:0 -c copy -f null -

-c/-codec copy,保留原始视频编码格式,不重新做视频编解码,因此非常快;

 -map [-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]],因此,-map 0:v:0表示第一个输入文件的第一个video stream;

 -f fmt (input/output). Force input or output file format.

参考:video - Fetch frame count with ffmpeg - Stack Overflow

四、转码

4.1 只转换容器容器格式,不改变编解码格式

Streamcopy: input file _demuxer_> encoded data packets_muxer__ > outputfile

ffmpeg -i input.avi -codec copy out.mp4

-c/-codec copy,保留原始视频编码格式,不重新做视频编解码,因此非常快;

注意:如果未加-codec copy,即ffmpeg -i input.avi out.mp4,则使用默认视频编解码器进行encoder和decoder,因此速度比较慢,属于同时转换编解码格式&容器格式的转换。

4.2 同时转换编解码格式&容器格式的转换

Transcoding process:input file _demuxer_> encoded data packets _decoder__>decoded frames _encoder_> encoded data packets __muxer__ > outputfile :

ffmpeg -i input.avi -vcodec h264 out.mp4

-i 表示输入视频选项;

-vcodec指定了视频编码格式为h264

五、改变播放速度或码率

改变帧率(播放速度):

ffmpeg -r f_output -i input output

-r f_output 表示帧率,例如 -r 20将output视频的帧率设为20fps。ouput和input视频的帧总数不变,视频时长根据修改的帧率做调整,即t_output=t_input*f_input/f_output.

改变码流:

ffmpeg -i input -r f_output output

output视频总时长与input视频相同,output视频的码流设置成-r f_output。

六、剪切

按时间剪切:

ffmpeg -i video.h264 -ss 00:00:50.0 -c copy -t 00:00:20.0 video_copy.h264

从50s开始,向后剪切20s。

-c[:stream_specifier] codec. Select an encoder (when used before an output file) or a decoder (when used before an input file) for one or more streams.  -c:v 选中video stream, -c:a选中audio stream。-c 表示选中所有stream。

注意:如果容器中不包含时间信息,则无法通过此工具裁剪,因为ffmpeg不支持根据帧数剪切,只支持基于时间剪切。

参考:FFmpeg:视频转码、剪切、合并、播放速调整_angus_17的博客-CSDN博客_ffmpeg视频剪切

按帧剪切:

Filtering: input file _demuxer_> encoded data packets _decoder_>decoded frames__simple filtergraph  >filtered frames _encoder_> encoded data packets __muxer__ > outputfile

ffmpeg -i input.h264 -vf "select=between(n\,20\,200)*not(mod(n\,2))" -vsync 0 ./cut_result.h264  

其中,Simple filtergraphs are configured with the per-stream -filter option (with -vf and -af aliases for video and audio respectively). 使用filter进入视频先解码,再编码的模式,则不能再使用stream copy。

-vf: filtergraph. Create the filtergraph specified by filtergraph and use it to filter the stream. This is an alias for "-filter:v".

select filter中between(n\,20\,200)*not(mod(n\,1))中 between内的参数为开始帧和结束帧,not(mod n\,2)中的参数为间隔数

关于filters的详细文档可以参见:FFmpeg Filters Documentation

PS:

 -vsync 控制时间戳
Video sync method.  For compatibility reasons old values can be specified as numbers.  Newly added values will have to be specified as strings always.

0, passthrough.  Each frame is passed with its timestamp from the demuxer to the muxer.

1, cfr. Frames will be duplicated and dropped to achieve exactly the requested constant frame rate.

2, vfr. Frames are passed through with their timestamp or dropped so as to prevent 2 frames from having the same timestamp.

drop. As passthrough but destroys all timestamps, making the muxer generate fresh timestamps based on frame-rate.

-1, auto. Chooses between 1 and 2 depending on muxer capabilities. This is the default method.

七、合并

建立filelist.txt文件,里面写入需要合并(时间上)的视频信息:

file '1.h264'
file '2.h264'
file '3.h264'
file '4.h264'

运行命令:

ffmpeg -f concat -i filelist.txt -c copy output.h264

八、拼接

将多个视频在空间上拼合成多个窗口,播放时同时显示。
垂直拼接:

ffmpeg -i input0.avi -i input1.avi -filter_complex "[0:v]scale=1600:-1[v0];[v0][1:v]vstack=inputs=2" output.avi

选中[0:v]第一个视频的video stream,

scale=1600:-1, scale为缩放滤镜,宽度为1600,-1表示输出时保持原始的宽高比。

水平拼接:

ffmpeg   -i input0.avi  -i input1.avi   -filter_complex '[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]'   -map [vid]   -c:v libx264   -crf 23   -preset veryfast   output.avi

PS:

 -filter[:stream_specifier] filtergraph (output,per-stream). 单进->单出。例如:单一视频文件的格式转换。Create the filtergraph specified by filtergraph and use it to filter the stream.  filtergraph is a description of the filtergraph to apply to the stream, and must have a single input and a single output of the same type of the stream.

-filter_complex: filtergraph (global). 多进->单出 or 单进->多出 or 多进->多出。例如,多个视频拼合一个文件。Define a complex filtergraph, i.e. one with arbitrary number of inputs and/or outputs.

关于filter的详细文档可以参见:FFmpeg Filters Documentation

参考:Vertically or horizontally stack (mosaic) several videos using ffmpeg? - Stack Overflow

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuyuelongfly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值