使用ffmpeg工具套件

2020年12月9日 / 63次阅读 / Last Modified 2021年7月5日
图像处理

ffmpeg是个非常强大的处理多媒体数据的工具,为了让自己在往后的日子里能够轻松驾驭各种需求,专门为它开一篇博文,记录自己的使用总结。

ffmpeg is a very fast video and audio converter that can also grab from a live audio/video source. It can also convert between arbitrary sample rates and resize video on the fly with a high quality polyphase filter.

ffmpeg命令行总体规范

ffmpeg reads from an arbitrary number of input "files" (which can be regular files, pipes, network streams, grabbing devices, etc.), specified by the "-i" option, and writes to an arbitrary number of output "files", which are specified by a plain output url. Anything found on the command line which cannot be interpreted as an option is considered to be an output url.
Each input or output url can, in principle, contain any number of streams of different types (video/audio/subtitle/attachment/data). The allowed number and/or types of streams may be limited by the container format. Selecting which streams from which inputs will go into which output is either done automatically or with the "-map" option (see the Stream selection chapter). To refer to input files in options, you must use their indices (0-based). E.g. the first input file is 0, the second is 1, etc. Similarly, streams within a file are referred to by their indices. E.g. "2:3" refers to the fourth stream in the third input file. Also see the Stream specifiers chapter.
As a general rule, options are applied to the next specified file. Therefore, order is important, and you can have the same option on the command line multiple times. Each occurrence is then applied to the next input or output file. Exceptions from this rule are the global options (e.g. verbosity level), which should be specified first.
Do not mix input and output files -- first specify all input files, then all output files. Also do not mix options which belong to different files. All options apply ONLY to the next input or output file and are reset between files.

这段文字反复读几遍,它规范了ffmpeg命令行使用。重点是:

  • 用 -i 指定输入,多个输入就多个 -i,其它不认识的都是输出;
  • 选择输入中的某个流(视频/音频/字幕/附件/数据),有自动默认规则,也有可以用 -map来手动指定;如果输入文件含有多个相同类型的流,比如多个不同视频流,请一定用-map来选择,默认的选择不一定对;
  • 命令行从左到右,对输入进行index编号,从0开始;这个index用于 -map 参数选择流;
  • 可以有多个输出,每一个输出前都要配置编解码等参数,可能会出现相同的配置串在命令行中多次出现,that's ok!

用ffprobe查看流信息

ffprobe是跟ffmpeg配套的工具(还有一个是ffplay)。

$ ffprobe fun.mp4
# or
$ ffprobe -show_streams fun.mp4

ffmpeg帮助信息

这是ffmpeg的简要帮助信息,通过这个信息,我们可以查询更多的关于ffmpeg的配置和参数信息:

$ ffmpeg -h
...
Print help / information / capabilities:
-L                  show license
-h topic            show help
-? topic            show help
-help topic         show help
--help topic        show help
-version            show version
-buildconf          show build configuration
-formats            show available formats
-muxers             show available muxers
-demuxers           show available demuxers
-devices            show available devices
-codecs             show available codecs
-decoders           show available decoders
-encoders           show available encoders
-bsfs               show available bit stream filters
-protocols          show available protocols
-filters            show available filters
-pix_fmts           show available pixel formats
-layouts            show standard channel layouts
-sample_fmts        show available audio sample formats
-colors             show available color names
-sources device     list sources of the input device
-sinks device       list sinks of the output device
-hwaccels           show available HW acceleration methods
...
$ ffmpeg -h long # a little more details 
$ ffmpeg -h full  
$ ffmpeg -h muxer-flv
$ ffmpeg -h encoders=h264

比如,查看 -pix_fmts 支持的参数有哪些:

$ ffmpeg -pix_fmts

截取频流

ffmpeg -i fun.mp4 -map 0:0 -ss 00:05:00 -t 00:00:02 v2s.mp4

-ss,指定截取开始时间;-t,指定时间长度;-map 0:0,选择第1个输入的0号stream(用ffprobe查看,0对应video,1对应audio)。

转码

转成YUV原始视频流格式

ffmpeg -i v2s.mp4 -map 0:0 -c rawvideo -pix_fmt yuv420p v2s.420p.yuv
ffmpeg -i v2s.mp4 -c:v rawvideo -pix_fmt yuv420p v2s2.420p.yuv

-c,codec;-c:v,对应输入中默认的video;后面跟 rawvideo,表示转成原始YUV格式;-pix_fmt,像素采样及存储格式;最后是输出文件。以上两个命令等价,后一个使用了ffmpeg的默认选择流机制。(视频原始数据文件非常大,所以先截取2秒,然后再转,就这样一个420p.yuv文件都要64M)

将YUV转成H265格式

ffmpeg -pix_fmt yuv420p -s 1280x720 -i v2s.420p.yuv -c:v libx265 out.265

要指定输入文件的yuv格式和size(-s),输入这个地方有个坑,out.265这个后缀必须要是.265,否则ffmpeg不能判断输出文件格式。

将1张JPG图片转成单帧H265格式的bitstream:

ffmpeg -i bizhi01.jpg bizhi.265

bizhi.265的后缀指明了格式,ffmpeg会自动调用x265进行编码,jpg图片自带尺寸信息。一张图片,IDR帧!

将1张JPG图片转成YUV格式文件:

ffmpeg -i bizhi01.jpg -c:v rawvideo -pix_fmt yuv420p bizhi.420p.yuv

-pix_fmt的常用值:yuv420p,yuv422p,yuv444p,gray(没有yuv400p)

ffplay的使用

播放压缩后的bin文件,不需要任何参数!

播放YUV文件

ffplay -f rawvideo -video_size 1280x720 v2s.420p.yuv
ffplay -f rawvideo -video_size 1280x720 -pix_fmt yuv422p bz.yuv

播放yuv文件有点费劲,必须要指定 video_size,默认是0x0。默认420p格式,其它格式需要用-pix_fmt指定。

播放完后自动退出

ffplay -autoexit v2s.mp4

默认播放完后就停在最后一个画面。

指定播放尺寸

ffplay -x 320 -y 240 v2s.mp4

循环播放

ffplay -loop 5 v2s.mp4

如果 -loop 0,就一直循环。

播放界面快捷键:

暂停,重启:p,space

单帧模式:s

鼠标右键的作用:选择播放位置!根据点击位置计算进度百分比,并跳转到那个位置,此时cmd界面会显示 seek to N%.....

全屏:f,或者左键双击。

-- EOF --

本文链接:https://www.pynote.net/archives/3010

留言区

您的电子邮箱地址不会被公开。 必填项已用*标注


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top