FFmpeg命令行工具-实用命令

目录

  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 图片序列与视频的互相转换
  • 4.5 yuv压缩为264 MP4文件
  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查看时间戳
  • 10.3 ffprobe查看每帧的帧类型信息
  1. 其他
  • 11.1 ffmpeg或ffplay 打开 DEBUG 日志输出
  • 11.2 FFmpeg命令日志输出到文件
  • 11.3 打印支持DirectShow的设备列表

参考

1. help命令使用

首先我们要学会使用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
  • 最上面部分是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/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            <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 AQ
     autovariance-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 pyramid
     normal                       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 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
  • 上面列出的选项大多数都是经常会用到的。

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 test.mp4 -c:v libx264 -filter:v select="between(t\, 10\, 20)" out.mp4

从头截取前10帧

ffmpeg -i test.mp4 -c:v libx264 -filter:v select="gt(n\, -1)" -vframes 10 out.mp4
ffmpeg -i test.mp4 -c:v libx264 -filter:v select="between(n\, 0\, 9)" out.mp4

中间截取 (10-20帧)

ffmpeg -i test.mp4 -c:v libx264 -filter:v select="between(n\, 10\, 20)" out.mp4

截取第10帧

ffmpeg -i test.mp4 -vf "select=eq(n\, 9)" -vframes -y 1 out.png

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文件序列。 

4.5 yuv压缩为264 MP4文件

ffmpeg -s 1280x720 -r 25 -f rawvideo -pix_fmt yuv420p -i test_1280x720_25.yuv -c:v libx264 -y test.mp4

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

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 -framerate 30 -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
ffplay -y 720 sample.mp4
  • 播放窗口高为720,宽会等比缩放

9.5 ffplay播放中的常用按键

  • 空格键或者p:暂停或者继续
  • s:跳转到下一帧
  • m: 静音或者取消静音
  • 向左或向右方向键:向后或者向前seek 10s
  • 点击鼠标右键:seek到对应窗口宽度百分比的进度
  • 鼠标左键双击:切换全屏/非全屏

9.6 ffplay播放加载字幕

ffplay -vf subtitles=video.srt test.mp4

9.7 ffplay显示视频编码的运动向量(mv)信息

ffplay -flags2 +export_mvs -vf codecview=mv=pf+bf+bb test.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]}'

10.3 ffprobe查看每帧的帧类型信息

ffprobe -select_streams v:0 -show_frames -of xml test.mp4
  • 该命令的目的是以 XML 格式输出视频文件中第一个视频流的每一帧的详细信息。这些信息可以包括关键帧标识、时间戳、帧的宽度和高度等。

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"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,613评论 4 59
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,500评论 1 180
  • 难得的周末,睡到自然醒。拿出几年前在丽江街头闲逛时淘来的青瓷茶杯,泡起了菊花。热水沸腾,入杯,三五分钟,蜷缩着的干...
    月萌胧阅读 262评论 0 4
  • 专题 青春散场 目录 上一章【连载】青春散场 第二章 报到入学 (2) 青春就像是一个大大的笑脸和一个大大的哭脸,...
    圆琛儿阅读 202评论 2 4
  • 昨晚拍线上直播拍到凌晨一点。今天谈谈当下正流行的网红和网红经济。 今年流行三大经济:网红经济、社群经济和共享经济。...
    Rene_Yu阅读 755评论 0 0