主页

索引

模块索引

搜索页面

FFmpeg

1 Synopsis

$ ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...

2. Description

  • ffmpeg is a universal media converter.

Convert an input media file to a different format, by re-encoding media streams:

$ ffmpeg -i input.avi output.mp4

Set the video bitrate of the output file to 64 kbit/s:

$ ffmpeg -i input.avi -b:v 64k -bufsize 64k output.mp4

Force the frame rate of the output file to 24 fps:

$ ffmpeg -i input.avi -r 24 output.mp4

Force the frame rate of the input file (valid for raw formats only) to 1 fps and the frame rate of the output file to 24 fps:

$ ffmpeg -r 1 -i input.m2v -r 24 output.mp4

3. Detailed description

The transcoding process in ffmpeg for each output can be described by the following diagram:

 _______              ______________
|       |            |              |
| input |  demuxer   | encoded data |   decoder
| file  | ---------> | packets      | -----+
|_______|            |______________|      |
                                           v
                                       _________
                                      |         |
                                      | decoded |
                                      | frames  |
                                      |_________|
 ________             ______________       |
|        |           |              |      |
| output | <-------- | encoded data | <----+
| file   |   muxer   | packets      |   encoder
|________|           |______________|

3.1 Filtering

  • Before encoding, ffmpeg can process raw audio and video frames using filters from the libavfilter library.

  • Several chained filters form a filter graph.

  • ffmpeg distinguishes between two types of filtergraphs: simple and complex.

3.1.1 Simple filtergraphs

  • Simple filtergraphs are those that have exactly one input and output, both of the same type.

  • In the above diagram they can be represented by simply inserting an additional step between decoding and encoding:

     _________                        ______________
    |         |                      |              |
    | decoded |                      | encoded data |
    | frames  |\                   _ | packets      |
    |_________| \                  /||______________|
                 \   __________   /
      simple     _\||          | /  encoder
      filtergraph   | filtered |/
                    | frames   |
                    |__________|
    

3.1.2 Complex filtergraphs

  • Complex filtergraphs are those which cannot be described as simply a linear processing chain applied to one stream.

example, when the graph has more than one input and/or output, or when output stream type is different from input:

 _________
|         |
| input 0 |\                    __________
|_________| \                  |          |
             \   _________    /| output 0 |
              \ |         |  / |__________|
 _________     \| complex | /
|         |     |         |/
| input 1 |---->| filter  |\
|_________|     |         | \   __________
               /| graph   |  \ |          |
              / |         |   \| output 1 |
 _________   /  |_________|    |__________|
|         | /
| input 2 |/
|_________|

3.2 Stream copy

  • Stream copy is a mode selected by supplying the copy parameter to the -codec option.

  • It makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing.

  • It is useful for changing the container format or modifying container-level metadata.

  • The diagram above will, in this case, simplify to this:

     _______              ______________            ________
    |       |            |              |          |        |
    | input |  demuxer   | encoded data |  muxer   | output |
    | file  | ---------> | packets      | -------> | file   |
    |_______|            |______________|          |________|
    
  • Since there is no decoding or encoding, it is very fast and there is no quality loss.

  • However, it might not work in some cases because of many factors.

  • Applying filters is obviously also impossible, since filters work on uncompressed data.

4 Stream selection

  • ffmpeg provides the -map option for manual control of stream selection in each output file.

  • The -vn / -an / -sn / -dn options can be used to skip inclusion of video, audio, subtitle and data streams respectively

4.1 Description

4.1.1 Automatic stream selection

It will select that stream based upon the following criteria:

1. for video, it is the stream with the highest resolution,
2. for audio, it is the stream with the most channels,
3. for subtitles, it is the first subtitle stream found but there’s a caveat.
        The output format’s default subtitle encoder can be either text-based or image-based,
                and only a subtitle stream of the same type will be chosen.

4.1.2 Manual stream selection

  • When -map is used, only user-mapped streams are included in that output file, with one possible exception for filtergraph outputs described below.

4.1.3 Complex filtergraphs

  • If there are any complex filtergraph output streams with unlabeled pads, they will be added to the first output file.

  • Complex filtergraph output streams with labeled pads must be mapped once and exactly once.

4.1.4 Stream handling

  • Stream handling is independent of stream selection, with an exception for subtitles

4.2 Examples

They assume the following three input files:

input file 'A.avi'
      stream 0: video 640x360
      stream 1: audio 2 channels

input file 'B.mp4'
      stream 0: video 1920x1080
      stream 1: audio 2 channels
      stream 2: subtitles (text)
      stream 3: audio 5.1 channels
      stream 4: subtitles (text)

input file 'C.mkv'
      stream 0: video 1280x720
      stream 1: audio 2 channels
      stream 2: subtitles (image)

Example: automatic stream selection:

$ ffmpeg -i A.avi -i B.mp4 out1.mkv out2.wav -map 1:a -c:a copy out3.mov

Example: automatic subtitles selection:

$ ffmpeg -i C.mkv out1.mkv -c:s dvdsub -an out2.mkv

Example: unlabeled filtergraph outputs:

$ ffmpeg -i A.avi -i C.mkv -i B.mp4 -filter_complex "overlay" out1.mp4 out2.srt

Example: labeled filtergraph outputs:

$ ffmpeg -i A.avi -i B.mp4 -i C.mkv -filter_complex "[1:v]hue=s=0,split=2[outv1][outv2];overlay;aresample" \
-map '[outv1]' -an        out1.mp4 \
                          out2.mkv \
-map '[outv2]' -map 1:a:0 out3.mkv

5 Options

5.1 Stream specifiers

  • Some options are applied per-stream, e.g. bitrate or codec.

5.2 Generic options

  • These options are shared amongst the ff* tools.

5.3 AVOptions

  • These options are provided directly by the libavformat, libavdevice and libavcodec libraries.

  • They are separated into two categories:

    1. generic
    These options can be set for any container, codec or device.
    
    2. private
    These options are specific to the given container, device or codec.
    

5.4 Main options

  • target type parameters set for each target are as follows:

    VCD
    SVCD
    DVD
    DV
    

5.5 Video Options

5.6 Advanced Video options

5.7 Audio Options

5.8 Advanced Audio options

5.9 Subtitle options

5.10 Advanced Subtitle options

5.11 Advanced options

5.12 Preset files

  • A preset file contains a sequence of option=value pairs, one for each line, specifying a sequence of options which would be awkward to specify on the command line.

  • There are two types of preset files: ffpreset and avpreset files.

5.13 vstats file format

6 Examples

6.1 Video and Audio grabbing

6.2 X11 grabbing

6.3 Video and Audio file format conversion

主页

索引

模块索引

搜索页面