"The Installation and Basic Use of FFmpeg"

"The Installation and Basic Use of FFmpeg"

      ___ ___ ___ ___ ___ ___     
   //:/_//:/_ | |::///::///:/_//:/_   
  //://///:///| |:|:///:/\:///://///:///

cnblog original address

0. Know FFmpeg

0.1 What is FFmpeg?

FFmpeg is a leading open source multimedia framework, which can handle almost all multimedia files. Contains libavcodec: this is a decoder library for audio and video in multiple projects, and libavformat: an audio and video format conversion library.

0.2 FFmpeg composition

Command line application:

  • ffmpeg
    : Used to convert the format of video files or audio files.
  • ffplay
    : A simple player based on SDL and FFmpeg library.
  • ffprobe
    : Used to display the information of media files.

Function library:

  • libswresample
  • libavresample
  • libavcodec : Contains all FFmpeg audio/video codec libraries
  • libavformat : Contains demuxers and muxer libraries
  • libavutil : contains some tool libraries
  • libpostproc : A library for video pre-processing
  • libswscale : A library for scaling images
  • libavfilter

0.3 Install FFmpeg on Linux

$ sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next $ sudo apt update $ sudo apt install ffmpeg Copy code

Check if the installation is successful

$ Ffmpeg -version copy the code

Output similar to the following information indicates that the installation was successful:

ffmpeg version 3.4.8-0ubuntu0.2 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04) Copy code

1. Basic use

1.1 Basic concepts

Before you can use FFmpeg, there are a few most basic concepts that you need to understand. These concepts will help you understand and use the FFmpeg tool.

Multimedia file

At the computer level, a multimedia file can be composed of container + streams (containers and data streams), and streams are roughly divided into multiple categories, such as audio stream, video stream, and so on. Therefore, in the past, multimedia also had a name called streaming media, which must be familiar to Nokia users. These streams are all encoded when they are generated and decoded when they are read. However, because the decoder needs to meet different scenarios (file size, video quality, etc.), there have been many decoders, and they all have Pros and cons. For example, codec.

The container is a container that holds these streams. It thinks that the multimedia player exposes a separate interface so that the player can read and write with multimedia files. The interfaces exposed by multimedia files of different formats are naturally different. The implementation of the need for containers is also diverse, and some have strong capabilities to accommodate multiple stream data. For example, contain subtitles, audio, video and other information at the same time.

When people talk about audio/video format conversion, they are actually converting containers.

1.2 Basic syntax format

$ Ffmpeg [global_options] {[input_file_options ] -i input_url} ... {[output_file_options] output_url} ... copy the code

this is

The basic grammatical format, related options can be viewed in the help, or first through the following [more] (#3. More (further understanding of ffmpeg and use)) to do a basic understanding first.

FFmpeg is quite smart. It has a large number of default settings. Usually, even when you do not specify various complex parameters, it can automatically specify the correct codecs and containers for you behind the scenes.

For example, suppose you want to convert an MP3 file to an OGG file, you only need the following basic instructions to complete:

$ Ffmpeg -i input.mp3 output.ogg copy the code

Or convert MP4 to WEBM

$ Ffmpeg -i input.mp4 output.webm copy the code

This is because there is a well-defined format like WebM, FFmpeg can automatically know what audio and video are supported and how to process it, and can convert these streams into valid WebM files.

But in some cases, it doesn t work. It depends on the type of container you are dealing with. For example, like Matroska (.mkv file), the container is designed to hold various streams, and it s impossible to know what you want to achieve. So the following command will probably not output the .mkv file you want

$ Ffmpeg -i input.mp4 output.mkv copy the code

So you need to configure some processing to let FFmpeg know what you want to do.

1.3 Choose your codecs

FFmpeg provides

Options allow you to specify various codecs. It allows you to specify its own decoder for each stream. E.g:

$ Ffmpeg -i input.mp3 -c: a libvorbis output.ogg copy the code
$ Ffmpeg -i input.mp4 -c: v vp9 -c: a libvorbis output.mkv copy the code

Develop a Matroska container whose video stream is decoded according to the VP9 rules, and the audio stream is decoded according to the Vorbis rules.


ffmpeg -codecs
All codecs supported by FFmpeg will be listed.

1.4 Modify a single stream

As mentioned earlier, containers generally support multiple types of streams, and FFmpeg supports individual modification of a certain stream, for example:

$ Ffmpeg -i input.webm -c: v copy -c: a flac output.mkv copy the code

This instruction copies the video stream from input.webm directly to the new container output.mkv, and then encodes the audio stream according to the flac rule.

1.5 Modify the container (container/format)

As mentioned above, we can just change the container, in fact, it is the conversion format:

$ Ffmpeg -i input.webm -c: av copy output.mkv copy the code

Copy the audio and video encoding rules of input.webm to the new container output.mkv, because it has not undergone any stream-level modification operations, it is a lossless conversion.

1.6 Set quality for each stream

When we have multimedia file conversion needs, we often have requirements for the quality of the video, so how to modify the quality of the stream?

Bit rate

The simplest way is to modify the bit rate (bitrate), also known as the "bitrate", which refers to the number of bits occupied by each frame of image storage and the rate of data transmission.

Assuming the bit rate of a movie is 600kbps, 90 minutes, then its size is 395MB (600kb/s =75KB/s (75KB per second), then 75KB/s*5400s =405000KB =395MB), including audio Roughly more than 400 MB. Therefore, at the same resolution, the larger the video capacity (the higher the bit rate), the better the quality. Of course, there are other factors that determine the video quality. In addition, when the bit rate exceeds a certain value, it does not have much impact on the image quality, so an appropriate value is important. Related can see this Zhihu answer .

To set bitrate for each stream, you need to specify one

Options, and specified encoding
Similarly, you also need to pass
The colon specifies a parameter. E.g:

$ Ffmpeg -i input.webm -c: a copy -c: v vp9 -b: v 1M output.mkv duplicated code

Copy the audio encoding from input.webm (

-c:a copy
), Convert the video to vp9 encoding rules (
-c:v vp9
), Specify the video bit rate as 1M/s (
), And then package the output as Matroska container (output.mkv). If you want to specify the audio bit rate

Frame rate

$ Ffmpeg -i input.webm -c: a copy -c: v vp9 -r 30 output.mkv duplicated code

Copy the audio encoding type from input.webm, set the video encoding to vp9, set the video frame rate to 30fps, and output to output.mkv

Video picture demension (size/resolution)

You can use FFmpeg to adjust the size of the video. The easiest way is to use a predefined video size. For example, set a video to 720p:

$ Ffmpeg -i input.mkv -c: a copy -s hd720 output.mkv copy the code

You can also customize the width and height of the screen:

$ Ffmpeg -i input.mkv -c: a copy -s 1280x720 output.mkv copy the code

:warning: Specify the screen size, namely size, the specified format of the change parameter is

-s width x height

FFmepg has a lot of predefined video sizes, attachment 1 at the end of the article ,

1.7 Intercept (cut) multimedia files

Take video as an example. If you need to cut a video, it may be more convenient to use video editing software first, but if you know where and where you need to edit, FFmpeg only needs one command to complete.

$ Ffmpeg -i input.mkv -c: av copy -ss 00:01:00 -t 10 output.mkv copy the code

Copy the audio and video stream decoding rules from input.mkv, (

-c:av copy
), and then start editing from 00:01:00 (
-ss 00:01:00
), cut back 10s for the duration (
-t 10
), and then output the 10 s video to the output.mkv file.

1.8 Extract audio

$ Ffmpeg -i input.mkv -vn outputaudio.ogg copy the code

Line selection refers to only audio processing. No audio encoding is specified here, Vorbis encoding will be used by default. The whole instruction means to output the audio in input.mkv to the outputaudio.ogg file with the default encoding Vorbis.

1.9 Convert video to GIF

A very interesting way is to convert a video into a Gif dynamic image.

$ Ffmpeg -i input.mkv output.gif copy the code

When converting to Gif, the file size is worth studying. Through the simplest instructions above, the details of the source video will be preserved to the maximum. I tried it. A 15min, 70MB video was converted to Gif and it was 513MB, and I opened it through firefox to be smooth Watch. Ordinary viewing software can't see it yet. So generally speaking, Gif conversion is suitable for short videos.

2. Some common examples

2.0 View file information

View file details:

$ Ffprobe -i abc.MOV -hide_banner copy the code
$ Ffmpeg -i abc.MOV -hide_banner copy the code

2.1 View supported decoding and encoding

$ ffmpeg -decoders $ ffmpeg -encoders Copy code

2.2 Common format conversion

2.2.1 Video:

mp4 -> webm

$ Ffmpeg -i input.mp4 output.webm copy the code

mov -> mp4

$ Ffmpeg -i input.mov output.mp4 copy the code

Specify the encoding format:

$ Ffmpeg -i input.mov -vcodec h264 -acodec acc output.mp4 copy the code

mp4 -> flv

$ Ffmpeg -i input.mp4 -acodec copy -vcodec copy -f output.flv copy the code

2.2.2 Audio:

$ Ffmpeg -i input.mp3 output.ogg copy the code

Specify the encoding format:

$ Ffmpeg -i input.mp3 -c: a libopus output.ogg copy the code

2.3 Video screen rotation

-I input.mp4 -metadata FFmpeg $: S: V = Rotate "90" output.mp4 copy the code

2.4 Set video bit rate

$ Ffmpeg -i input.avi -b: v 64k output.avi copy the code

2.5 Set video frame rate

$ Ffmpeg -i input.avi -r 24 output.avi copy the code

Force the frame rate of the input file (only for the original format) to 1 fps, and force the frame rate of the output file to 24 fps:

$ Ffmpeg -r 1 -i input.m2v -r 24 output.avi copy the code

2.6 Modify the screen size

$ Ffmpeg -vcodec mpeg4 -b 1000 -r 10 -g 300 -i input.mp4 -s 800x600 output.mp4 duplicated code

2.7 Limit file size

$ Ffmpeg -i input.mp4 -fs 70M output.mp4 copy the code

This ability looks like nb, but it is easy to encounter problems. It can indeed output the file size you want, but if the value you set is too much smaller than the original video, the output file will automatically cut the excess part. . For example, for the original 80M file, you specify the output as 10M, then the video may be reduced from 15 minutes to tens of seconds. If you want to adjust the size of a video file, the most effective way is to modify the resolution, bit rate, and frame rate. See the part mentioned above [Jump] (#1.6 Set quality for each stream)

3. More (learn more about ffmpeg and use)

FFmpeg is very powerful and can handle almost all multimedia files at the file level. At the same time, it is also very complicated, and the document is divided into:

  • Command Line Tools Documentation

  • Libraries Documentation

  • API Documentation

  • Components Documentation

  • General Documentation

  • Community Contributed Documentation

But ordinary non-professionals usually use video transcoding at most. So here I only focus on basic command line operations related to video. The content in the corresponding document is:

Command Line Tools Documentation |____ffmpeg Documentation |____1.Synopsis |____5.Options |____5.2 Generic Options |____5.4 Main Options |____5.5 Video Options |____5.6 Advanced Video Options Copy code

3.1 Syntax

$ Ffmpeg [global_options] {[input_file_options ] -i input_url} ... {[output_file_options] output_url} ... copy the code

Quick description:

The content of the package refers to optional.

3.2 Diagram of the transcoding process:

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

Quick description:

Call the libavformat library (including demuxers and muxer libraries, demuxer refers to the separation of audio and video) to read the file, and get the package containing the encoded data. If there are multiple input files,
Will try to read synchronously based on the timestamp.

The encoded data packets are then passed to the decoder, and the decoder processes decoded frames, which are then passed to the encoder after passing through filtering (Filtering, specifically described below). The encoder encodes these frame data and outputs the encoded data packet, which is finally transmitted to the muxer, and the audio and video are combined into one and then written to the output file.

About filters

Before encoding, the decoded frame will be filtered. Filtering processing is some additional processing, which is composed of filters one by one. According to the different input and output, it can be divided into simple and complex filter graphs. The filter comes from the libavfilter library.

About stream replication and stream selection

An important concept in ffmpeg is streaming. Processing video is actually processing video streams, and processing audio is actually processing audio streams.

Stream copy is Stream copy, which is

An option for decoders-
, This option will make
Ignore the steps of decoding and encoding for the specified range of streams, and only do
Processing, that is, only split the audio and video when reading, and then repackage and process. This is very useful when you only need to modify the container format and container-level metadata information. In ffmpeg, the file format of the video is called the container format. The container format, that is, abc.mp4 and abc.mov are just different container formats. When converting each other, if we do not need to frame the video, such as modification For operations such as encoding and decoding types, there is no need for decoding and encoding processes at all. You only need to demux the audio and video demuxing processing, then merge them through muxing processing, and write them into the new container format to complete the format conversion.

A process similar to this:

_______ ______________ ________ | | | | | | | input | demuxer | encoded data | muxer | output | | file | ---------> | packets | -------> | file | |_______| |______________| |________| Copy code

Since there is no decoding->encoding process here, the process will be very fast and there will be no quality loss. However, it may not be executed normally due to many factors. At this time, adding a filter will not work, because the filter works based on the decompressed data.

Stream selection : I just said that ffmepg is actually processing data streams when processing audio and video, so when processing streams, you usually need to select the stream range first.

Provides for manually selecting streams for each output file
Options. User can skip
Option, let ffmpeg automatically select the stream. specific
Options can be used to skip video, audio, subtitles, and data stream selections.

3.3 Options

Generally, all numeric options, if not specifically specified, accept a number as input, followed by a unit, which can be

,can also be
.... ,Such as:

If an option has no parameters, it means that the parameter is a boolean type. If this parameter is specified, the default is to set the corresponding parameter value to true. If you need to specify the parameter value to false, just add no in front of it. E.g

which is
foo = true
, Then
foo = false

3.3.1 Stream specifiers

There are some options that will be applied per stream, such as bitrate or codec. The stream specifier is used to precisely specify the stream to which the given option belongs.

The stream specifier is usually a string separated by a colon, appended to the option name.


-codec:a:1 ac3
Stream specifier, it will match the second audio stream. In addition he will choose
The decoder processes it.

A flow specifier can match multiple flows, therefore, the specified options will be applied to all matched flows. E.g

-b:a 128k
Will match the left and right audio streams.

An empty stream specifier will match all streams, for example:

-codec copy
-codec: copy
All streams will be copied without re-encoding.

The possible forms of the stream specifier are as follows:

  • stream_index
    : Match the stream with the specified index. E.g
    -threads:1 4
    The thread number of the second stream will be set to 4. If stream_index is used as another stream specifier (see below), then it will select the stream with the corresponding number stream_index from the matching streams. The stream numbers are based on the order in which the streams are detected.

  • stream_type[:additional_stream_specifier]
    : stream_type is one of the following:

    : Match all videos,
    Only match video streams without pictures, video thumbnails or cover art.

    Refers to optional

    If specified

    , It will match that is the designation
    , While matching the specified
    Conditional flow. Otherwise, the unspecified transformation will match all the specified


    -codec:a:1 ac3
    Stream specifier, it will match the second audio stream. In addition he will choose
    The decoder processes it.

  • There are several less useful ones:

    • p:program_id[:additional_stream_specifier]

    • stream_id or i:stream_id

    • u

3.3.2 General options

There are many common options, and these options are shared between ff* tools. Most of them are some help and auxiliary options. The following briefly talks about a few options that may be used, and lists some brief information.

  • -h
    View help: such as:

    $ ffmpeg -h decoder=decoder_name $ ffmpeg -h protocol=protocol_name Copy code
  • -hide_banner
    Hide banner:

    After the default ff* is executed, a series of information such as copyright and version of ffmpeg will be printed out. If you don't want to see it, you can use it

    Do not show. E.g:

    Ffprobe -i abc.MOV $ # will display a banner $ ffprobe -i abc.MOV -hide_banner # does not display the information banner copy the code

    Used to view file details.

# Simple list of some -L #Show license. -h, -?, -help, --help [arg] #Show help -version #Show version. -buildconf #Show the build configuration, one option per line. -formats #Show available formats (including devices). -demuxers #Show available demuxers. -muxers #Show available muxers. -devices #Show available devices. -codecs #Show all codecs known to libavcodec. -decoders #Show available decoders. -encoders #Show all available encoders. -bsfs #Show available bitstream filters. -protocols #Show available protocols. -filters #Show available libavfilter filters. -pix_fmts #Show available pixel formats. -sample_fmts #Show available sample formats. -layouts #Show channel names and standard channel layouts. -colors #Show recognized color names. -hide_banner #Suppress printing banner. -nohide_banner: show banner ...... Copy code

For more, please check here . Don't make too much explanation.

3.3.3 Main options

  • -i
    : Set the input file name.
  • -f
    : Set the output format.
  • -y
    : If the output file already exists, the file will be overwritten.
  • -fs
    : The conversion ends when the specified file size is exceeded.
  • -t
    : Specify the duration of the output file in seconds.
  • -ss
    : Start conversion from the specified time, in seconds.
  • -t
    The time starts to change, (for example, `-ss 00:00:01.00 -t 00:00:10.00 means from 00:00:01.00 to 00:00:11.00).
  • -title
    : Set the title.
  • -timestamp
    : Set the time stamp.
  • -vsync
    : Increase or decrease Frame to synchronize video and audio.
  • -c
    : Specify the encoding of the output file.
  • -metadata
    : Change the metadata of the output file.

3.3.4 Video options

Image parameters:

  • -b:v
    : Set the image flow rate, the default is 200kit/s.
  • -r[:stream_specifier] fps (input/output,per-stream)
    : Set the frame rate value, the default is 25.
  • -fpsmax[:stream_specifier] fps (output,per-stream)
    : Set the maximum frame rate value
  • -s[:stream_specifier] size (input/output,per-stream)
    : Set the width and height of the screen.
  • -aspect[:stream_specifier] aspect (output,per-stream)
    : Set the scale of the screen.
  • -vn
    : Do not process the image, but use it when processing the sound only.
  • -vcodec( -c:v)
    : Set the video codec, if not set, use the same codec as the input file.


Shorthand for predetermined video sizeActual value