头图

Forwarded from Bailang Stack: View original text

Read the table of contents

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 picture to briefly describe the flow of the video being played.

image

The following explains the meaning of the figure below.

  1. Decapsulation: The player separates the input data in the encapsulation format (mp4, mkv) to produce two parts of audio stream and video stream. Note that these two parts of data are only compressed data, similar to shunting. We will also introduce how to Extract audio and video from video files.
  2. The next step is the decoding operation. We say that decoding is to decode the video and audio compressed coded data into uncompressed video and audio raw data. Here, the audio is decoded into pcm format data, and the video is decoded into yuv format data.
  3. Synchronous audio and video 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 ffmpeg command

When we audio and video basic concepts , we threw a transcoding command, as follows

ffmpeg -i input.flv output.mp4

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

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

The translation is

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

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

For example, we can input multiple files and output multiple files after transcoding

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

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

So many options are reflected in one command. A basic general rule is:

The input file option only applies to the first input file after it. Naturally, the output file option only applies to 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 whether we want to overwrite the output, the following two ways of writing can be done

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

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

In addition, do not write the output file first, and then write the input file. At least you should finish writing the input file before writing the output file. For example, the following is a bad way to write

ffmpeg output.mp4 -i input.flv

Good habits are a good start.

The process of ffmpeg transcoding output

ffmpeg -i input.flv output.mp4

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

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

image

The specific description is as follows

  1. ffmpeg calls a libavformat library that contains the demuxer demuxer, and reads the data packet containing the encoding from the input file
  2. Then pass the encoded data packet to the decoder (stream copy operation ignores this step)
  3. The decoder (decoder) produces uncompressed frames (original frames) that can be further processed by filters
  4. Next, the original 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, these data are written into the output file by the muxer.

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


wwolf
147 声望38 粉丝

白狼栈: [链接]