Forward from White Wolf Stack: See the original article

Some friends read the article very carefully, for the last class inadvertently mentioned some edge details are more concerned, such as -acodec, -vcodec, stream replication and so on. In fact, all of these are inseparable from what we are going to talk about today — flow.

Speaking of streaming, there may be a lot of small partners first reaction is streaming media, but today we are going to talk about the type of streaming in the container. Through the previous introduction, I believe you have some impression of the audio (A) and video (V) in the container. In addition, the types of streams in the container include subtitle (S), attachment (T) and ordinary data (D). We’ll focus on audio streaming, video streaming, and captioning streaming.

Streaming operation means that we can select different streams from the input file to operate on, and then output the desired result.

For example, if you have children at home, you should know better. There are many English dubbing competitions in school now. A video is played on the big screen and students dub on the stage, which is very vivid.

In this scene, the video playing on the big screen is, in fact, silent video. Silent video is not a video with minimal sound. It is a video with no audio, so the video that plays back is just the picture. For example, the video of the first case above can be removed from the audio stream through the command -an, and only the video stream can be retained, that is, only the picture (if not downloaded, you can click here to download it).

ffmpeg -i r1ori.mp4 -an -y r1-silent.mp4

Stream #0:1(und): Audio is missing.

Mp4-hide_banner Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'r1-silent. Mp4 ': Metadata: major_brand: ISom minor_version: 512 compatible_brands: ISOMISO2AVC1MP41 encoder: Lavf58.20.100 Duration: 00:00:58.53, start: compatible_brands: ISOMISO2AVC1MP41 encoder: Lavf58.20.100 Duration: 00:00:58.53, start: 0.000000, bitrate: 1687 KB /s Stream #0:0(und): Video: H264 (HIGH) (AVC1/0x31637661), YUV420P, 544x960, 1684 KB /s, 29.83 FPS, 29.83 TBR, 11456 TBN, 59.67 TBC (Default) Metadata: handler_name: VideoHandler At least one output file must be specified

Now even if you hold the stereo and play this video at full volume, you won’t hear anything.

-an is -acodec none. A stands for audio, codec stands for decoder, -acodec stands for audio decoder, which together means you don’t specify the audio decoder, so if you review how we transcode in FFMPEG it’s easy to understand the transcoding process.

As you might have guessed, we can similarly remove video streams, subtitle streams, etc.

  1. -An removes audio streams
  2. – VN removes video streams
  3. -SN removes subtitle streams
  4. -DN removes the data stream

Some of you may have noticed that the length of our original video is 59 seconds, which is less than a minute. However, a command of -an takes more than ten seconds to process, which is too slow. Is there any way to optimize it?

If you think about it, where is the time going? Yes, that’s recoding.

Here we’re just removing the audio stream. Is it necessary to recode it? No, so wouldn’t it be nice if we could just copy the video stream?

The optimized command is as follows

ffmpeg -i r1ori.mp4 -an -vcodec copy -y r1-silent.mp4

This command prints the results in an instant. We added a parameter -vcodec copy. -VCodec refers to the video decoder, V is the video video, Codec is the decoder, followed by the name of the decoder, copy the input video stream, not for decoding processing.

Similarly, if we want to extract the audio from the video, or convert the video to audio, can we use the following command?

ffmpeg -i r1ori.mp4 -vn -c:a copy -y r1-silent.mp3

After executing the command, an error was reported

Invalid audio stream. Exactly one MP3 audio stream is required. Could not write header for output file #0 (incorrect codec parameters ?) : Invalid argument

The audio stream is invalid because the codec parameter is wrong. Let’s take a look at the information from the original video

» FFmpeg-i r1ori. MP4-hide_banner...... Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 129 kb/s (default) ......

Note that the audio stream is in AAC format and we want to output in MP3 format. The -c:a copy parameter means that we want to load the AAC audio stream into the MP3 container. This is not possible. AAC audio streams require a dedicated AAC container and MP3 audio streams require a dedicated MP3 container.

Note: AAC and MP3 are both lossy compressed audio encoding formats.

It’s easy to find the reason, we change the output of MP3 format to AAC format

ffmpeg -i r1ori.mp4 -vn -c:a copy -y r1-silent.aac

Although most of the audio streams in an MP4 container are in AAC format, imagine if we wrote a program to extract and store audio for the user’s uploaded video, but the audio in the original video uploaded by the user was MP3?

To satisfy this scenario, let’s make a video in MP3 format, and then try executing the command above.

1. Transfer the audio in R1ori. MP4 video to MP3

ffmpeg -i r1ori.mp4 -c:a libmp3lame -c:v copy -y r2.mp4

Note: Since ffmpeg does not have a native mp3 encoder, we specify an external libmp3lame library (although -c:a libmp3lame you can also change libmp3lame to mp3, which is still libmp3lame). ERROR: libmp3lame >= 3.98.3 not found If you execute the command above and report an ERROR like this: libmp3lame = 3.98.3 not found, your local ffmpeg did not add the –enable-libmp3lame compile parameter
This articleSelect the appropriate way to reinstall FFMPEG;

2. Extract the audio of the video

FFMPEG-I R2. MP4-VN-C: A COPY-Y R1-SILENT. FFMPEG-I R2. MP4-VN-C: A COPY-Y R1-SILENT. Only AAC streams can be muxed by the ADTS muxer Could not write header for output file #0 (incorrect codec parameters ?) : Invalid argument

So, -C: A copy is not everything, which means that if we want to convert video to audio, it’s better to specify an encoder, AAC or libmp3lame? In terms of sound quality, we’d prefer libmp3lame, although the best audio encoder that comes with FFmpeg is AAC.

To sum up, if you need to output MP3 audio, you can use it

ffmpeg -i r1ori.mp4 -vn -c:a libmp3lame -y r1-silent.mp3

If you want to output audio in AAC format, you can use it

ffmpeg -i r1ori.mp4 -vn -c:a aac -y r1-silent.aac

Note: The new version of FFmpeg supports native AAC encoding, so you can use -c:a AAC directly. The lower version of FFmpeg, like the 2.x version of the native AAC encoder, is not fully supported and must also specify -strict -2 to be used.

Above, we introduced manually specifying the audio decoder to successfully convert video to audio.

Since FFmpeg is so powerful, if we don’t specify it manually, will it automatically help us choose the appropriate decoder for processing?

Very good.

Take MP3, for example. Let’s try it

ffmpeg -i r1ori.mp4 -y r1-silent.mp3 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'r1ori.mp4': 
...... 
Stream mapping: Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame)) 
......

Notice that the output procedure code includes the Stream mapping and the next line of code, and you can see that FFMPEG did automatically select the libmp3lame decoder for us.

What if the original video has multiple audio streams? We’ll talk about that in the next video.