Introduction to ffmpeg - how does ffmpeg transcode?

How is the video played?

We know that most of the current players are based on ffmpeg secondary development. Have you ever thought about how you see the picture and hear the sound when you open a video with the player?

We use this diagram to describe the flow of the video being played.

The meaning of the figure below is explained in a little bit.

  1. Decapsulation: The player separates the data in the input encapsulation format (mp4, mkv) to generate two parts, the audio stream and the video stream. Note that these two parts of data are only compressed data at this time, similar to shunting. We will also introduce how to do this later. Extract audio and video from video files.
  2. The next step is the decoding operation. We say that decoding is to decode the encoded data of video and audio into uncompressed video and audio raw data. Here, the audio is decoded into data in pcm format, and the video is decoded into data in yuv format.
  3. Audio and video synchronization playback: The video information and audio and video data obtained by decapsulation and decoding are sent to the graphics card and sound card for playback.

Format of the ffmpeg command

When we introduced transcoding in the article Basic Concepts of Audio and Video , a transcoding command was thrown, as follows

ffmpeg -i input.flv output.mp4

For the basic format of the ffmpeg command, refer to the ffmpeg official website

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

translate it to

ffmpeg [全局选项] {[输入文件选项] -i 输入文件} ... {[输出文件选项] 输出文件} ...

... indicates that a command may have multiple inputs and multiple outputs.

For example, we can output multiple files after transcoding by inputting multiple files

ffmpeg -i input1 -i input2 \
    -acodec … -vcodec … output1 \
    -acodec … -vcodec … output2 \
    -acodec … -vcodec … output3

According to incomplete statistics, the number of options in the ffmpeg command is as small as tens of thousands, so it is not only necessary to master more learning skills, but also to accumulate more experience. It is completely unworkable to rely on rote memorization.

So many options are reflected in one command, and a basic general rule is:

The input file option only acts on the first input file after it, and naturally, the output file option only acts on the first output file after it. So there is an order requirement.

Global options can be written casually. For example, there is a global option -y, which asks us if we want to overwrite the output. The following two methods can be written.

ffmpeg -y -i input.flv output.mp4
ffmpeg -i input.flv -y output.mp4

However, if the command is very long (such as the audio and video case we gave earlier, a command even has hundreds of characters), such a global option is best written in front of the input file or the output file.

In addition, don't write the output file first, then write the input file, at least you should finish writing the input file, and then write the output file, such as the following is a bad way of writing

ffmpeg output.mp4 -i input.flv

Good habits are a good start.

At the end of the article, there are audio and video free learning materials to receive.

 

The process of ffmpeg transcoding output

ffmpeg -i input.flv output.mp4

Or the above simple transcoding command, for a command like this, how to deal with ffmpeg?

We use the following figure to represent the process of transcoding output.

 

The specific description is as follows

  1. ffmpeg calls a libavformat library that includes a demuxer, a demuxer, to read the encoded packets from the input file
  2. Then pass the encoded packet to the decoder (stream copy operation ignores this step)
  3. The decoder produces uncompressed frames (i.e. raw frames) that can be further processed by filters
  4. Next, the raw data processed by the filter is passed to the encoder
  5. The encoder encodes the passed data and outputs the encoded data packet
  6. Finally, the data is written to the output file by the muxer.

Any complex command must go through the above transcoding process. You can ignore the underlying code, but you must understand the whole process.

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/126394789