目录

  1. help命令使用

  2. 流选择选项

  3. 视频封装解封装

  • 3.1 视频格式转换

  • 3.2 分离/合并视频音频流

  • 3.3 截取视频片段

  • 3.4 视频转换为m3u8格式

  • 3.5 把视频文件推送到rtmp服务器

  • 3.6 对视频进行切片

  1. 视频解码编码

  • 4.1 视频截图

  • 4.2 视频/图片尺寸修改

  • 4.3 分离视频的YUV通道分量

  • 4.4 图片序列与视频的互相转换

  1. 视频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

  1. 音频解码编码

  • 6.1 PCM原始数据和WAV格式转换

  1. 音频filter

  • 7.1 倍速atempo

  1. 字幕相关

  • 8.1 字幕文件转换

  • 8.2 集成字幕到视频文件

  1. ffplay命

  • 9.1 ffplay播放yuv视频数据

  • 9.2 ffplay播放PCM音频数据

  • 9.3 ffplay播放音频并显示波形图

  • 9.4 ffplay指定显示宽高

  1. ffprobe命令

  • 10.1 ffprobe查看媒体文件的信息

  • 10.2 ffprobe查看时间戳

  1. 其他

  • 11.1 ffmpeg或ffplay 打开 DEBUG 日志输出

  • 11.2 FFmpeg命令日志输出到文件

  • 11.3 打印支持DirectShow的设备列表

参考

  • [1] ffmpeg.org/ffmpeg-all

【CSDN文末扫码进君羊,免费分享】资料包括《Andoird音视频开发必备手册+音视频学习视频+学习文档资料包+大厂面试真题+2022最新学习路线图》等等 ,音视频最新资料包

1. help命令使用

首先我们要学会使用ffmpeg工具提供的最最基本的一个命令:ffmpeg -help

有了他我们就可以更加愉快地学习使用ffmpeg的各种功能了。

执行ffmpeg -help后,在终端会打印下面的帮助信息:

ffmpeg version 3.4.2-1~16.04.york0 Copyright (c) 2000-2018 the FFmpeg developersbuilt with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.6) 20160609configuration: --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-sharedlibavutil      55. 78.100 / 55. 78.100libavcodec     57.107.100 / 57.107.100libavformat    57. 83.100 / 57. 83.100libavdevice    57. 10.100 / 57. 10.100libavfilter     6.107.100 /  6.107.100libavresample   3.  7.  0 /  3.  7.  0libswscale      4.  8.100 /  4.  8.100libswresample   2.  9.100 /  2.  9.100libpostproc    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/filterSee 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 methodsGlobal 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        dispositionVideo 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 dataAudio 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 filtersSubtitle 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
  • 最上面部分是ffmpeg的版本信息和各模块的版本信息。

1.1 ffmpeg命令的语法结构

usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
  • 语法结构很简单。
  • 中括号中的参数是可选项。
  • options是对输入和输出都有影响的选项。
  • [[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/filterSee 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: autoSupported pixel formats: yuv420p yuvj420p yuv422p yuvj422p yuv444p yuvj444p nv12 nv16 nv21
libx264 AVOptions:-preset            <string>     E..V.... Set the encoding preset (cf. x264 --fullhelp) (default "medium")-tune              <string>     E..V.... Tune the encoding params (cf. x264 --fullhelp)-profile           <string>     E..V.... Set profile restrictions (cf. x264 --fullhelp) -fastfirstpass     <boolean>    E..V.... Use fast settings when encoding first pass (default true)-level             <string>     E..V.... Specify level (as defined by Annex A)-passlogfile       <string>     E..V.... Filename for 2 pass stats-wpredp            <string>     E..V.... Weighted prediction for P-frames-a53cc             <boolean>    E..V.... Use A53 Closed Captions (if available) (default true)-x264opts          <string>     E..V.... x264 options-crf               <float>      E..V.... Select the quality for constant quality mode (from -1 to FLT_MAX) (default -1)-crf_max           <float>      E..V.... In CRF mode, prevents VBV from lowering quality beyond this point. (from -1 to FLT_MAX) (default -1)-qp                <int>        E..V.... Constant quantization parameter rate control method (from -1 to INT_MAX) (default -1)-aq-mode           <int>        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 AQautovariance-biased              E..V.... Auto-variance AQ with bias to dark scenes-aq-strength       <float>      E..V.... AQ strength. Reduces blocking and blurring in flat and textured areas. (from -1 to FLT_MAX) (default -1)-psy               <boolean>    E..V.... Use psychovisual optimizations. (default auto)-psy-rd            <string>     E..V.... Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.-rc-lookahead      <int>        E..V.... Number of frames to look ahead for frametype and ratecontrol (from -1 to INT_MAX) (default -1)-weightb           <boolean>    E..V.... Weighted prediction for B-frames. (default auto)-weightp           <int>        E..V.... Weighted prediction analysis method. (from -1 to INT_MAX) (default -1)none                         E..V....simple                       E..V....smart                        E..V....-ssim              <boolean>    E..V.... Calculate and print SSIM stats. (default auto)-intra-refresh     <boolean>    E..V.... Use Periodic Intra Refresh instead of IDR frames. (default auto)-bluray-compat     <boolean>    E..V.... Bluray compatibility workarounds. (default auto)-b-bias            <int>        E..V.... Influences how often B-frames are used (from INT_MIN to INT_MAX) (default INT_MIN)-b-pyramid         <int>        E..V.... Keep some B-frames as references. (from -1 to INT_MAX) (default -1)none                         E..V....strict                       E..V.... Strictly hierarchical pyramidnormal                       E..V.... Non-strict (not Blu-ray compatible)-mixed-refs        <boolean>    E..V.... One reference per partition, as opposed to one reference per macroblock (default auto)-8x8dct            <boolean>    E..V.... High profile 8x8 transform. (default auto)-fast-pskip        <boolean>    E..V.... (default auto)-aud               <boolean>    E..V.... Use access unit delimiters. (default auto)-mbtree            <boolean>    E..V.... Use macroblock tree ratecontrol. (default auto)-deblock           <string>     E..V.... Loop filter parameters, in <alpha:beta> form.-cplxblur          <float>      E..V.... Reduce fluctuations in QP (before curve compression) (from -1 to FLT_MAX) (default -1)-partitions        <string>     E..V.... A comma-separated list of partitions to consider. Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all-direct-pred       <int>        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    <int>        E..V.... Limit the size of each slice in bytes (from -1 to INT_MAX) (default -1)-stats             <string>     E..V.... Filename for 2 pass stats-nal-hrd           <int>        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    <int>        E..V.... AVC-Intra class 50/100/200 (from -1 to 200) (default -1)-motion-est        <int>        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        <boolean>    E..V.... If forcing keyframes, force them as IDR frames. (default false)-coder             <int>        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        <int>        E..V.... Strategy to choose between I/P/B-frames (from -1 to 2) (default -1)-chromaoffset      <int>        E..V.... QP difference between chroma and luma (from INT_MIN to INT_MAX) (default -1)-sc_threshold      <int>        E..V.... Scene change threshold (from INT_MIN to INT_MAX) (default -1)-noise_reduction   <int>        E..V.... Noise reduction (from INT_MIN to INT_MAX) (default -1)-x264-params       <string>     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 dataAudio 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 filtersSubtitle 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
  • 上面列出的选项大多数都是经常会用到的。

2. 流选择选项

一些媒体格式如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. 视频封装解封装

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

  • Set segment duration to time, the value must be a duration specification. Default value is "2".
  • Note that splitting may not be accurate, unless you force the reference stream key-frames at the given time. See the introductory notice and the examples below.

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 
  • 以上命令把input.mp4切分为5分钟时长的视频片段。
  • segment muxer的参数可以使用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 
  • 在第60秒、第120秒和第150秒这三个时间点进行切片。

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 <input> -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. 视频filter

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
  • 按照从左到右的顺序依次为:“宽”、“高”、“X坐标”和“Y坐标”,宽和高指的是输入视频尺寸(包含加黑边的尺寸),X、Y指的是视频所在位置。
  • 默认是加黑色的边,black可以不写。
  • 上面的命令中93是根据(720-534) / 2计算得到。

如果视频原始1920x800的话,完整的语法应该是:

   -vf 'scale=1280:534,pad=1280:720:0:93:black'
  • 先将视频缩小到1280x534,然后在加入黑边变成1280x720,将1280x534的视频放置在x=0,y=93的地方。

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 -

5.10 视频按时间拼接

使用 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
  • 详细参数介绍见ffmpeg文档

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. 音频解码编码

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. 音频filter

7.1 倍速atempo

速度减半

ffmpeg -i input.mp3 -af atempo=0.5 output.mp3

2倍速率

ffmpeg -i input.mp3 -af atempo=2 output.mp3

8. 字幕相关

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. ffplay命令

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. ffprobe命令

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. 其他

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
  • shell输出分好多种,0表示键盘输出,1表示屏幕输出,2表示错误输出。 cmd > file,只是把屏幕输出重定向到文件中。
  • 调用ffmpeg相关命令,打印信息都被终端认为是错误输出,但是一般情况下这种信息也会输出到屏幕上,所以我们可以在屏幕上看到它。但是,当我们想重定向到文件中,却被系统认为是错误输出而忽略。
  • 2>&1表示错误输出重定向到屏幕输出。&标明其后面跟的是一个文件描述符,而不是一个文件名。

11.3 打印支持DirectShow的设备列表

打印支持DirectShow的设备列表

ffmpeg -list_devices true -f dshow -i dummy

打开名称为“Lenovo EasyCamera”的摄像头

ffmpeg -f dshow -i video="Lenovo EasyCamera"

FFmpeg命令行工具-实用命令相关推荐

  1. 音视频处理 ffmpeg初级开发 命令行工具-实用命令

    参考链接 ffmpeg Documentation 作者:smallest_one  链接:FFmpeg命令行工具-实用命令 - 简书 目录 1,help命令使用 1.1 ffmpeg命令的语法结构 ...

  2. python命令行工具_python命令行工具Click快速掌握

    前言 写 Python 的经常要写一些命令行工具,虽然标准库提供有命令行解析工具 Argparse,但是写起来非常麻烦,我很少会使用它.命令行工具中用起来最爽的就是 Click,它是 Flask 的团 ...

  3. Linux装逼命令行以及实用命令行

    Shell中实用命令行 1 mkdir 创建文件夹 2 mv 剪切文件夹 3 rm 解决方法:rm -r XXX 删除文件夹啊 4 touch a.txt 创建文件 5 find . -name 'm ...

  4. android 的命令行工具(dos命令)

    可以通过电脑的dos界面实现对andorid(s手机)的内部文件增删改查 adb shell      进入手机中linux操作系统 ls 列举当前目录下的所有文件 cd 文件名     切换(跳转) ...

  5. 【Android 命令行工具】Android 命令行工具简介 ( 官方文档 | SDK 命令行工具 | SDK 构建工具 | SDK 平台工具 | 模拟器工具 | Jetifier 工具 )

    文章目录 一.官方文档 二.Android 命令行工具简介 1.SDK 命令行工具 2.SDK 构建工具 3.SDK 平台工具 4.模拟器工具 5.Jetifier 工具 一.官方文档 Android ...

  6. 命令行工具开发:如何快速实现命令行提示?

    简介:对于稍微复杂一些的命令行工具,命令行的提示功能必不可少.那么对于不同语言的开发者,有没有一种简单快捷的实现方式呢?本文分享一种快速实现的方法,使用YAML文件定义命令行工具的使用规范,再通过工具 ...

  7. 18个堪称神器的命令行工具,高效运维必备

    ***上期入口:***24个 CSS 高级技巧合集 本资源原链接地址:18个堪称神器的命令行工具 thefuck 命令行打错了以后,打一个fuck就会自动纠正. GitHub:https://gith ...

  8. 使用.Net Core编写命令行工具(CLI)

    使用.Net Core编写命令行工具(CLI) 命令行工具(CLI) 命令行工具(CLI)是在图形用户界面得到普及之前使用最为广泛的用户界面,它通常不支持鼠标,用户通过键盘输入指令,计算机接收到指令后 ...

  9. php 开启命令模式,如何启用PhpStorm中的命令行工具

    本篇文章主要给大家介绍如何使用phpstorm中的命令行工具. PhpStorm下载地址: PhpStorm使用命令行工具,我们可以直接从IDE调用命令!在我们使用任何命令行工具之前,我们必须在设置中 ...

最新文章

  1. 关于解决arduino SHT1x诸多问题
  2. 1091. Acute Stroke (30)
  3. mpls企业组网怎么样?
  4. 白名单模板_亚马逊品牌备案常见问题—— 白名单、IP加速器及品牌备案后无法使用A+...
  5. Dart Hello World 示例程序【Dart专题1】
  6. Eclipse4.8.0无法打开 Eclipse MarketPlace
  7. 端口复用(解决C/S模型的BUG)
  8. wordvba编程代码大全_面向对象、设计原则、设计模式、编程规范、重构
  9. 转JS总结1--(CSS书写规范和正则表达式)
  10. 一些sql 语句(行列转换等)
  11. 【Python 必会技巧】利用 utf-8-sig 编码格式解决写入 csv 文件乱码问题
  12. Jco服务配置以及程序编写
  13. 获取手机idfa_东风无力百花残 | IDFA留给开发者的时间不多了
  14. 一步步实现windows版ijkplayer系列文章之一Windows10平台编译ffmpeg 4.0.2,生成ffplay
  15. 移动手机病毒的进化历程
  16. matlab中abs函数,Abs函数
  17. mysql数据库学习心得之一
  18. cx oracle 中文 u,cx_Oracle 中文乱码
  19. DDR3内存技术原理
  20. 转 解决打印机无法打印的方法

热门文章

  1. 浅谈sstream头文件
  2. QTextDocument
  3. Java虚拟机-JVM
  4. eclipse写python怎么样_eclipse python 使用教程(怎么用eclipse写python)
  5. 积分商城搭建前的5个关键准备工作?
  6. 深度学习 Deeplab语义分割
  7. 静态库与动态库的区别
  8. 分享 :CSS常见面试题
  9. 必考题系列--十种常见的运行时异常
  10. 正则表达式中,表示匹配非数字字符的字符