目录
help命令使用
流选择选项
视频封装解封装
3.1 视频格式转换
3.2 分离/合并视频音频流
3.3 截取视频片段
3.4 视频转换为m3u8格式
3.5 把视频文件推送到rtmp服务器
3.6 对视频进行切片
视频解码编码
4.1 视频截图
4.2 视频/图片尺寸修改
4.3 分离视频的YUV通道分量
4.4 图片序列与视频的互相转换
视频filter
5.1 视频加水印overlay
5.2 视频旋转transpose
5.3 调色3DLutFilter
5.4 图像/视频转换成黑白的hue
5.5 裁剪视频crop
5.6 视频尺寸缩放scale
5.7 视频中去水印delogo
5.8 给视频或图像加上黑边pad
5.9 计算psnr/ssim
5.10 视频按时间拼接
5.11 视频左右拼接
5.12 打印视频帧的一些信息showinfo
5.13 生成纯色的图片
5.14 打印帧序号/pts到视频帧水印drawtext
音频解码编码
6.1 PCM原始数据和WAV格式转换
音频filter
7.1 倍速atempo
字幕相关
8.1 字幕文件转换
8.2 集成字幕到视频文件
ffplay命
9.1 ffplay播放yuv视频数据
9.2 ffplay播放PCM音频数据
9.3 ffplay播放音频并显示波形图
9.4 ffplay指定显示宽高
ffprobe命令
10.1 ffprobe查看媒体文件的信息
10.2 ffprobe查看时间戳
其他
11.1 ffmpeg或ffplay 打开 DEBUG 日志输出
11.2 FFmpeg命令日志输出到文件
11.3 打印支持DirectShow的设备列表
参考
[1] ffmpeg.org/ffmpeg-all
【CSDN文末扫码进君羊,免费分享】资料包括《Andoird音视频开发必备手册+音视频学习视频+学习文档资料包+大厂面试真题+2022最新学习路线图》等等 ,音视频最新资料包
首先我们要学会使用ffmpeg工具提供的最最基本的一个命令:ffmpeg -help
。
有了他我们就可以更加愉快地学习使用ffmpeg的各种功能了。
执行ffmpeg -help
后,在终端会打印下面的帮助信息:
ffmpeg version 3.4.2-1~16.04.york0 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.6) 20160609
configuration: --prefix=/usr --extra-version='1~16.04.york0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Getting help:
-h -- print basic options
-h long -- print more options
-h full -- print all options (including all format and codec specific options, very long)
-h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter
See man ffmpeg for detailed description of the options.
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
Global options (affect whole program instead of just one file:
-loglevel loglevel set logging level
-v loglevel set logging level
-report generate a report
-max_alloc bytes set maximum size of a single allocated block
-y overwrite output files
-n never overwrite output files
-ignore_unknown Ignore unknown stream types
-filter_threads number of non-complex filter threads
-filter_complex_threads number of threads for -filter_complex
-stats print progress report during encoding
-max_error_rate ratio of errors (0.0: no errors, 1.0: 100% error maximum error rate
-bits_per_raw_sample number set the number of bits per raw sample
-vol volume change audio volume (256=normal)
Per-file main options:
-f fmt force format
-c codec codec name
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration record or transcode "duration" seconds of audio/video
-to time_stop record or transcode stop time
-fs limit_size set the limit file size in bytes
-ss time_off set the start time offset
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number set the number of frames to output
-filter filter_graph set stream filtergraph
-filter_script filename read stream filtergraph description from a file
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition disposition
Video options:
-vframes number set the number of video frames to output
-r rate set frame rate (Hz value, fraction or abbreviation)
-s size set frame size (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number set the number of bits per raw sample
-vn disable video
-vcodec codec force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 to 3)
-vf filter_graph set video filters
-ab bitrate audio bitrate (please use -b:a)
-b bitrate video bitrate (please use -b:v)
-dn disable data
Audio options:
-aframes number set the number of audio frames to output
-aq quality set audio quality (codec-specific)
-ar rate set audio sampling rate (in Hz)
-ac channels set number of audio channels
-an disable audio
-acodec codec force audio codec ('copy' to copy stream)
-vol volume change audio volume (256=normal)
-af filter_graph set audio filters
Subtitle options:
-s size set frame size (WxH or abbreviation)
-sn disable subtitle
-scodec codec force subtitle codec ('copy' to copy stream)
-stag fourcc/tag force subtitle tag/fourcc
-fix_sub_duration fix subtitles duration
-canvas_size size set canvas size (WxH or abbreviation)
-spre preset set the subtitle options to the indicated preset
1.1 ffmpeg命令的语法结构
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
[[infile options] -i infile]...
指定输入选项和输入文件,...
表示可以包含多个输入。{[outfile options] outfile}...
指定输出选项和输出文件,...
表示可以包含多个输出。1.2 获取详细的help信息
ffmpeg -help
给出了基本的帮助信息,其中下面的信息说明了如何获得更全面的帮助信息。
Getting help:
-h -- print basic options
-h long -- print more options
-h full -- print all options (including all format and codec specific options, very long)
-h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter
See man ffmpeg for detailed description of the options.
-h long
: 打印更多的选项参数。-h full
: 打印所有的选项参数,包括所有针对于format和codec的选项,信息特别的长。-h type=name
: 打印打印指定名称的decoder/encoder/demuxer/muxer/filter的所有选项信息。man ffmpeg
: 查看ffmpeg的帮助手册。特别强调一下-h type=name
这个命令,要经常去使用这个命令。比如查看libx264支持的参数,执行ffmpeg -h encoder=libx264
,会打印libx264 encoder的使用说明:
Encoder libx264 [libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10]:
General capabilities: delay threads
Threading capabilities: auto
Supported pixel formats: yuv420p yuvj420p yuv422p yuvj422p yuv444p yuvj444p nv12 nv16 nv21
libx264 AVOptions:
-preset E..V.... Set the encoding preset (cf. x264 --fullhelp) (default "medium")
-tune E..V.... Tune the encoding params (cf. x264 --fullhelp)
-profile E..V.... Set profile restrictions (cf. x264 --fullhelp)
-fastfirstpass E..V.... Use fast settings when encoding first pass (default true)
-level E..V.... Specify level (as defined by Annex A)
-passlogfile E..V.... Filename for 2 pass stats
-wpredp E..V.... Weighted prediction for P-frames
-a53cc E..V.... Use A53 Closed Captions (if available) (default true)
-x264opts E..V.... x264 options
-crf E..V.... Select the quality for constant quality mode (from -1 to FLT_MAX) (default -1)
-crf_max E..V.... In CRF mode, prevents VBV from lowering quality beyond this point. (from -1 to FLT_MAX) (default -1)
-qp E..V.... Constant quantization parameter rate control method (from -1 to INT_MAX) (default -1)
-aq-mode E..V.... AQ method (from -1 to INT_MAX) (default -1)
none E..V....
variance E..V.... Variance AQ (complexity mask)
autovariance E..V.... Auto-variance AQ
autovariance-biased E..V.... Auto-variance AQ with bias to dark scenes
-aq-strength E..V.... AQ strength. Reduces blocking and blurring in flat and textured areas. (from -1 to FLT_MAX) (default -1)
-psy E..V.... Use psychovisual optimizations. (default auto)
-psy-rd E..V.... Strength of psychovisual optimization, in : format.
-rc-lookahead E..V.... Number of frames to look ahead for frametype and ratecontrol (from -1 to INT_MAX) (default -1)
-weightb E..V.... Weighted prediction for B-frames. (default auto)
-weightp E..V.... Weighted prediction analysis method. (from -1 to INT_MAX) (default -1)
none E..V....
simple E..V....
smart E..V....
-ssim E..V.... Calculate and print SSIM stats. (default auto)
-intra-refresh E..V.... Use Periodic Intra Refresh instead of IDR frames. (default auto)
-bluray-compat E..V.... Bluray compatibility workarounds. (default auto)
-b-bias E..V.... Influences how often B-frames are used (from INT_MIN to INT_MAX) (default INT_MIN)
-b-pyramid E..V.... Keep some B-frames as references. (from -1 to INT_MAX) (default -1)
none E..V....
strict E..V.... Strictly hierarchical pyramid
normal E..V.... Non-strict (not Blu-ray compatible)
-mixed-refs E..V.... One reference per partition, as opposed to one reference per macroblock (default auto)
-8x8dct E..V.... High profile 8x8 transform. (default auto)
-fast-pskip E..V.... (default auto)
-aud E..V.... Use access unit delimiters. (default auto)
-mbtree E..V.... Use macroblock tree ratecontrol. (default auto)
-deblock E..V.... Loop filter parameters, in form.
-cplxblur E..V.... Reduce fluctuations in QP (before curve compression) (from -1 to FLT_MAX) (default -1)
-partitions E..V.... A comma-separated list of partitions to consider. Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all
-direct-pred E..V.... Direct MV prediction mode (from -1 to INT_MAX) (default -1)
none E..V....
spatial E..V....
temporal E..V....
auto E..V....
-slice-max-size E..V.... Limit the size of each slice in bytes (from -1 to INT_MAX) (default -1)
-stats E..V.... Filename for 2 pass stats
-nal-hrd E..V.... Signal HRD information (requires vbv-bufsize; cbr not allowed in .mp4) (from -1 to INT_MAX) (default -1)
none E..V....
vbr E..V....
cbr E..V....
-avcintra-class E..V.... AVC-Intra class 50/100/200 (from -1 to 200) (default -1)
-motion-est E..V.... Set motion estimation method (from -1 to 4) (default -1)
dia E..V....
hex E..V....
umh E..V....
esa E..V....
tesa E..V....
-forced-idr E..V.... If forcing keyframes, force them as IDR frames. (default false)
-coder E..V.... Coder type (from -1 to 1) (default default)
default E..V....
cavlc E..V....
cabac E..V....
vlc E..V....
ac E..V....
-b_strategy E..V.... Strategy to choose between I/P/B-frames (from -1 to 2) (default -1)
-chromaoffset E..V.... QP difference between chroma and luma (from INT_MIN to INT_MAX) (default -1)
-sc_threshold E..V.... Scene change threshold (from INT_MIN to INT_MAX) (default -1)
-noise_reduction E..V.... Noise reduction (from INT_MIN to INT_MAX) (default -1)
-x264-params E..V.... Override the x264 configuration using a :-separated list of key=value parameters
1.3 打印帮助或支持能力的信息
ffmpeg -help
打印的下面部分的信息说明了如何获取一些help信息和各种格式支持的能力。
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
1.4 全局选项
帮助信息的下面部分是全局选项的说明。
Global options (affect whole program instead of just one file:
-loglevel loglevel set logging level
-v loglevel set logging level
-report generate a report
-max_alloc bytes set maximum size of a single allocated block
-y overwrite output files
-n never overwrite output files
-ignore_unknown Ignore unknown stream types
-filter_threads number of non-complex filter threads
-filter_complex_threads number of threads for -filter_complex
-stats print progress report during encoding
-max_error_rate ratio of errors (0.0: no errors, 1.0: 100% error maximum error rate
-bits_per_raw_sample number set the number of bits per raw sample
-vol volume change audio volume (256=normal)
-v
, -y
。1.5 文件选项
帮助信息的下面部分是针对每个文件可以指定的选项。
Per-file main options:
-f fmt force format
-c codec codec name
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration record or transcode "duration" seconds of audio/video
-to time_stop record or transcode stop time
-fs limit_size set the limit file size in bytes
-ss time_off set the start time offset
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number set the number of frames to output
-filter filter_graph set stream filtergraph
-filter_script filename read stream filtergraph description from a file
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition disposition
-f fmt
指定文件的格式。-c
, -t
, -to
, -ss
这几个选项都比较常用。1.6 视频/音频/字幕相关的选项
帮助信息的下面部分是针对视频/音频/字幕相关的选项。
Video options:
-vframes number set the number of video frames to output
-r rate set frame rate (Hz value, fraction or abbreviation)
-s size set frame size (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number set the number of bits per raw sample
-vn disable video
-vcodec codec force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 to 3)
-vf filter_graph set video filters
-ab bitrate audio bitrate (please use -b:a)
-b bitrate video bitrate (please use -b:v)
-dn disable data
Audio options:
-aframes number set the number of audio frames to output
-aq quality set audio quality (codec-specific)
-ar rate set audio sampling rate (in Hz)
-ac channels set number of audio channels
-an disable audio
-acodec codec force audio codec ('copy' to copy stream)
-vol volume change audio volume (256=normal)
-af filter_graph set audio filters
Subtitle options:
-s size set frame size (WxH or abbreviation)
-sn disable subtitle
-scodec codec force subtitle codec ('copy' to copy stream)
-stag fourcc/tag force subtitle tag/fourcc
-fix_sub_duration fix subtitles duration
-canvas_size size set canvas size (WxH or abbreviation)
-spre preset set the subtitle options to the indicated preset
一些媒体格式如Mp4、AVI等可以包含多种类型的流。
FFmpeg可以识别5种流类型: 音频audio (a), 附件attachment (t), 数据data (d), 字幕subtitle (s) 和 视频video (v)。
可以通过-map选项来选择需要的流,语法格式如下:
file_number:stream_type[:stream_number]
file_number和stream_number分别表示文件索引和流索引。
有一些特别的流选择指令:
-map 0 :选择所有的流类型和其中所有的流。
-map i:v :选择文件i中的所有视频流; -map i:a 选择所有的音频流;-map i:s
选择所有的字幕流。
3.1 视频格式转换
ffmpeg -i input.avi c:a copy c:v copy output.mp4
ffmpeg -i input.mp4 output.ts
3.2 分离/合并视频音频流
ffmpeg -i input-video -vn -c:a copy output-audio //分离音频流
//-vn is no video.
//-acodec copy says use the same audio stream that's already in there.
ffmpeg -i input-video -c:v copy -an output-video //分离视频流
//-an is no audio.
//-vcodec copy says use the same video stream that's already in there.
ffmpeg -i input-video -c:v copy -an output-video -c:a -vn output-audio //同时分离音频和视频流
ffmpeg -i video_file -i audio_file -c:v copy -c:a copy output_file //合并视频音频流
3.3 截取视频片段
ffmpeg -ss 5 -i input.mp4 -t 10 -c:v copy -c:a copy output.mp4
//-ss 5指定从输入视频第5秒开始截取,-t 10指明最多截取10秒。
//而-c:v copy -c:a copy标示视频与音频的编码不发生改变,而是直接复制,这样会大大提升速度,因为这样就不需要完全解码视频。
截取10s到20s
ffmpeg -i input.mp4 -c:v libx264 -filter:v select="between(t\, 10\, 20)" out.mp4
从头截取前10帧
ffmpeg -i input.mp4 -c:v libx264 -filter:v select="gt(n\, -1)" -vframes 10 out.mp4
ffmpeg -i input.mp4 -c:v libx264 -filter:v select="between(n\, 0\, 9)" out.mp4
中间截取 (10-20帧)
ffmpeg -i input.mp4 -c:v libx264 -filter:v select="between(n\, 10\, 20)" out.mp4
3.4 视频转换为m3u8格式
ffmpeg -i yoona.mp4 -c copy -map 0 -f segment -segment_list yoona.m3u8 -segment_time 10 yoona-%04d.ts
3.5 把视频文件推送到rtmp服务器
ffmpeg -re -i jack.mp4 -c copy -f flv rtmp://host/live/test
3.6 对视频进行切片
均匀切片
ffmpeg -i input.mp4 -c copy -f segment -segment_format mp4 -segment_time 300 -reset_timestamps 1 test_output-%d.mp4
ffmpeg -h muxer=segment
查看按照时间点进行剪切
ffmpeg -i input.mp4 -c copy -f segment -segment_format mp4 -segment_times 60,120,150 -reset_timestamps 1 test_output-%d.mp4
4. 视频解码编码
4.1 视频截图
获取视频时间点01:23:45的截图
ffmpeg -ss 01:23:45 -i input -vframes 1 -q:v 2 output.jpg
-i input file the path to the input file
-ss 01:23:45 seek the position to the specified timestamp
-vframes 1 only handle one video frame
-q:v 2 to control output quality. Full range is a linear scale of 1-31 where a lower value results in a higher quality. 2-5 is a good range to try.
output.jpg output filename, should have a well-known extension
截取视频帧序号34的图像,帧序号从0开始。
ffmpeg -i -vf "select=eq(n\,34)" -vframes 1 out.png
4.2 视频/图片尺寸修改
ffmpeg -i sample.jpg -s w*h out.jpg
4.3 分离视频的YUV通道分量
ffmpeg -i jack.mp4 -filter_complex "extractplanes=y+u+v[y][u][v]" -map "[y]" jack_y.mp4 -map "[u]" jack_u.mp4 -map "[v]" jack_v.mp4
4.4 图片序列与视频的互相转换
ffmpeg -i %04d.jpg output.mp4
ffmpeg -i input.mp4 %04d.jpg
\\第一行命令是把0001.jpg、0002.jpg、0003.jpg等编码成output.mp4,
\\第二行则是相反把input.mp4变成0001.jpg……。
\\%04d.jpg表示从1开始用0补全的4位整数为文件名的jpg文件序列。
5.1 视频加水印
ffmpeg -i xizong.mp4 -i fleight.jpg -filter_complex "overlay=W-w-5:5" -codec:a copy xizong_fleight.mp4
//在右上角加水印,边距为5像素。
5.2 视频旋转transpose
顺时针翻转90度
ffmpeg -i input -vf transpose=1 output
翻转180度
ffmpeg -i in.mp4 -vf "transpose=1,transpose=1" out.mp4
5.3 调色3DLutFilter
应用颜色查找表Lut,有‘nearest’、‘trilinear’、‘tetrahedral’三种插值算法,命令行调用如下:
ffmpeg -i log.mp4 -vf lut3d="file=DK79.cube" out.mp4
5.4 图像/视频转换成黑白的
图像转换成黑白的
ffmpeg -i sample.png -vf hue=s=0 output.png
//把视频转换为黑白的
ffmpeg -i julin_5s.mp4 -vf hue=s=0 -c:a copy julin_monochrome.mp4
5.5 裁剪视频crop
使用-crop选项,语法如下:
crop=ow[:oh[:x[:y[:keep_aspect]]]]
ow、oh表示裁减之后输出视频的宽和高,
x、y表示在输入视频上开始裁减的横坐标和纵坐标,
keep_aspect: 1表示保持裁剪后输出的纵横比与输入一致,0表示不保持。
裁剪输入视频的左三分之一,中间三分之一,右三分之一:
ffmpeg -i input -vf crop=iw/3:ih :0:0 output
ffmpeg -i input -vf crop=iw/3:ih :iw/3:0 output
ffmpeg -i input -vf crop=iw/3:ih :iw/3*2:0 output
裁剪中间一半区域:
ffmpeg -i input -vf crop=iw/2:ih/2 output
5.6 视频尺寸缩放scale
将输入的1920x1080缩小到960x540输出
ffmpeg -i input.mp4 -vf scale=960:540 output.mp4
//ps: 如果540不写,写成-1,即scale=960:-1, 那也是可以的,ffmpeg会通知缩放滤镜在输出时保持原始的宽高比。
5.7 视频中去水印delogo
使用delogo filter
它对logo周围像素的简单插值来实现。只需设置一个覆盖logo的矩形。在每个方向上,紧靠矩形外的下一个像素的值将用于计算矩形内的插值像素值。
语法:-vf delogo=x:y:w:h[:t[:show]]
x:y 离左上角的坐标
w:h logo的宽和高
show:默认值0,若设置为1,指定区域的边界会绘制一个绿色的矩形框用于找到合适的x、y、w、h参数。
示例
ffmpeg -i input.mp4 -vf delogo=x=0:y=0:w=100:h=60 output.mp4
5.8 给视频或图像加上黑边pad
比如一个输入视频尺寸是1280x534的源,想要加上黑边变成1280x720,使用下面的命令可以实现。
ffmpeg -i input.mp4 -vf pad=1280:720:0:93:black output.mp4
(720-534) / 2
计算得到。如果视频原始1920x800的话,完整的语法应该是:
-vf 'scale=1280:534,pad=1280:720:0:93:black'
5.9 计算psnr/ssim
分析 psnr 主要是针对转码前的视频和转码后的视频的对比;如果转码是降分辨率,对比的时候,还需要对源视频加 -s widthxhigh;保证分辨率一致再做 psnr 和 ssim 的计算;
psnr
ffmpeg -i src.mp4 -i dst.mp4 -lavfi psnr="stats_file=psnr.log" -f null -
ssim
ffmpeg -i src.mp4 -i dst.mp4 -lavfi ssim="stats_file=ssim.log" -f null -
同时计算psnr和ssim
ffmpeg -i main.mpg -i ref.mpg -lavfi "ssim;[0:v][1:v]psnr" -f null -
使用 FFmpeg 的concat 工具。
先创建一个文本文件,包含需要拼接的文件的列表。
filelist.txt:
file 'input1.mp4'
file 'input2.mp4'
file 'input3.mp4'
命令
ffmpeg -f concat -i filelist.txt -c copy output.mp4
5.11 视频左右拼接
ffmpeg -ss 00:05:00 -i v1.mp4 -ss -ss 00:05:00 -i v2.mp4 -filter_complex "[0:v]crop=iw/2:ih:0:0[v1];[1:v]crop=iw/2:ih:iw/2:0[v2];[v1][v2]hstack[v3];[v3]drawbox=iw/2-1:0:2:ih[v4]" -map [v4] -map 0:a -c:a copy -c:v libx264 -t 00:01:00 -y out.mp4
5.12 打印视频帧的一些信息showinfo
ffmpeg -hide_banner -i v.mp4 -vf showinfo -frames:v 1 -f null /dev/null
打印的信息类似
[Parsed_showinfo_0 @ 0x74f2d40] config out time_base: 0/0, frame_rate: 0/0
[Parsed_showinfo_0 @ 0x74f2d40] n: 0 pts: 0 pts_time:0 pos: 55743 fmt:yuv420p sar:1395/1394 s:656x360 i:P iskey:1 type:I checksum:81726064 plane_checksum:[C119CB7C 5DDE813C 3A76139D] mean:[106 126 142] stdev:[52.5 8.3 18.8]
[Parsed_showinfo_0 @ 0x74f2d40] side data - User Data Unregistered:
[Parsed_showinfo_0 @ 0x74f2d40] UUID=dc45e9bd-e6d9-48b7-962c-d820d923eeef
[Parsed_showinfo_0 @ 0x74f2d40] color_range:unknown color_space:unknown color_primaries:unknown color_trc:unknown
[Parsed_showinfo_0 @ 0x74f2d40] n: 1 pts: 1024 pts_time:0.0666667 pos: 69559 fmt:yuv420p sar:1395/1394 s:656x360 i:P iskey:0 type:B checksum:3AB39554 plane_checksum:[C775F70F 3E19845B DFD819DB] mean:[107 126 142] stdev:[52.5 8.3 18.8]
[Parsed_showinfo_0 @ 0x74f2d40] color_range:unknown color_space:unknown color_primaries:unknown color_trc:unknown
5.13 生成纯色的图片
生成黑色的图片
ffmpeg -f lavfi -i color=Black:640x480 -frames:v 1 -y black.jpg
5.14 打印帧序号/pts到视频帧水印drawtext
打印帧序号
ffmpeg -i test.mp4 -vf drawtext=fontcolor=red:fontsize=40:fontfile=msyh.ttf:line_spacing=7:text=%{n}:x=50:y=50 -vsync 0 -y out.mp4
打印帧pts
ffmpeg -i test.mp4 -vf drawtext=fontcolor=red:fontsize=30:fontfile=msyh.ttf:line_spacing=7:text=%{pts}:x=50:y=50 -vsync 0 -y out.mp4
打印帧类型
ffmpeg -i test.mp4 -vf drawtext=fontcolor=red:fontsize=20:fontfile=msyh.ttf:line_spacing=7:text=%{pict_type}:x=50:y=50 -vsync 0 -y out.mp4
把视频的pts[时间戳]添加为视频水印,精度到毫秒
ffmpeg -i test.mp4 -vf “drawtext=fontsize=60:text=’%{pts:hms}’” -c:v libx264 -c copy -f mp4 output.mp4 -y
6.1 PCM原始数据和WAV格式转换
wav格式转换为PCM裸流
ffmpeg -i xizong.wav -f f32le -ar 44100 -acodec pcm_f32le output_f32le.raw
参数说明:
-f f32le … 浮点数32为小字端的采样格式
-ar 44100 … 采样频率
-ac 2 … 声道数量
PCM裸流转wav格式
ffmpeg -f f32le -ar 44100 -ac 2 -acodec pcm_f32le -i xizong_f32le.raw xizong_f32le.wav
说明需要先使用ffprobe查看wav中音频的采样格式,以上例子是使用的f32le采样格式的数据。
7.1 倍速atempo
速度减半
ffmpeg -i input.mp3 -af atempo=0.5 output.mp3
2倍速率
ffmpeg -i input.mp3 -af atempo=2 output.mp3
8.1 字幕文件转换
字幕文件有很多种,常见的有 .srt , .ass 文件等。
//将.srt文件转换成.ass文件
ffmpeg -i subtitle.srt subtitle.ass
将.ass文件转换成.srt文件
ffmpeg -i subtitle.ass subtitle.srt
8.2 集成字幕到视频文件
ffmpeg -i input.mp4 -i subtitles.srt -c:s mov_text -c:v copy -c:a copy output.mp4
9.1 ffplay播放yuv视频数据
ffplay -f <文件格式> -pix_fmt <像素格式> -video_size <视频尺寸> <文件名>
示例:
ffplay -f rawvideo -pix_fmt yuv420p -video_size 848x480 yuv_video
9.2 ffplay播放PCM音频数据
ffplay -f <格式名> -ac <声道数> -ar <采样率> <文件名>
示例:
ffplay -f f32le -ac 1 -ar 48000 pcm_audio
9.3 ffplay播放音频并显示波形图
ffplay -showmode 1 simple.mp4
9.4 ffplay指定显示的宽高
ffplay -x 1208 -y 720 sample.mp4
10.1 ffprobe查看媒体文件的信息
ffprobe target.mp4 -show_format -show_streams -print_format json -loglevel fatal
示例输出:
root@smallest:/home/video# ffprobe jack.mp4 -show_format -show_streams -print_format json -loglevel fatal
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "High",
"codec_type": "video",
"codec_time_base": "1/50",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 848,
"height": 480,
"coded_width": 848,
"coded_height": 480,
"has_b_frames": 1,
"sample_aspect_ratio": "1:1",
"display_aspect_ratio": "53:30",
"pix_fmt": "yuv420p",
"level": 30,
"chroma_location": "left",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"r_frame_rate": "25/1",
"avg_frame_rate": "25/1",
"time_base": "1/25000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 39754000,
"duration": "1590.160000",
"bit_rate": "449785",
"bits_per_raw_sample": "8",
"nb_frames": "7499",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2016-09-14T08:14:35.000000Z",
"language": "und",
"handler_name": "TrackHandler"
}
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "HE-AAC",
"codec_type": "audio",
"codec_time_base": "1/48000",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "48000",
"channels": 2,
"channel_layout": "stereo",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/24000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 7199744,
"duration": "299.989333",
"bit_rate": "48030",
"max_bit_rate": "56888",
"nb_frames": "7031",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2016-09-14T08:11:52.000000Z",
"language": "und",
"handler_name": "Sound Media Handler"
}
}
],
"format": {
"filename": "jack.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "299.988333",
"size": "18813764",
"bit_rate": "501719",
"probe_score": 100,
"tags": {
"major_brand": "mp42",
"minor_version": "0",
"compatible_brands": "isomavc1mp42",
"creation_time": "2016-09-14T08:14:35.000000Z"
}
}
}
-print_format json
: 结果以json格式呈现-show_format
: 打印封装格式信息,在format节点中。-show_streams
: 打印流信息,在streams节点中。10.2 ffprobe查看时间戳
查看视频dts
ffprobe -show_packets -select_streams v 1.ts | grep dts_time
查看视频pts
ffprobe -show_packets -select_streams v 1.ts | grep pts_time
查看视频流前几帧的pts
ffprobe -v error -of compact -read_intervals %+#10 -show_packets -select_streams v 1.ts
查看关键帧的pts
ffprobe -v error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=print_section=0 input.mp4 | awk -F',' '/K/ {print $1}'
查看关键帧帧间的间隔
./ffprobe -of compact -select_streams v -show_packets 'test.mp4'| grep K_ | awk 'BEGIN{FS="|";last=-1}{split($5,a,"="); if(last != -1) {print "Keframe pos: " a[2] ", Interval: " a[2]-last " seconds"} else {print "Keyframe: " a[2]}; last=a[2]}'
11.1 ffmpeg或ffplay 打开 DEBUG 日志输出。
$ ffplay -v debug $URL
API中开启debug日志
av_log_set_level(AV_LOG_DEBUG);
11.2 FFmpeg命令日志输出到文件
执行ffmpeg或者ffprobe输出重定向到文件的时候发现文件是空的。
可以采用下面的方式。
ffprobe xxx > file 2>&1
2>&1
表示错误输出重定向到屏幕输出。&标明其后面跟的是一个文件描述符,而不是一个文件名。11.3 打印支持DirectShow的设备列表
打印支持DirectShow的设备列表
ffmpeg -list_devices true -f dshow -i dummy
打开名称为“Lenovo EasyCamera”的摄像头
ffmpeg -f dshow -i video="Lenovo EasyCamera"