From white Wolf Stack: Check out the original

Some students are very careful in reading the article, and are more concerned about some marginal details inadvertently mentioned in the last class, such as -acodec, -vcodec, stream replication and so on. In fact, these are inseparable from the focus of today’s talk – flow.

Speaking of streaming, there may be a lot of friends first reaction is streaming media, but today we are going to talk about the type of in-container streaming. From the previous introduction, I believe you have some impression of the audio (A) and video (V) in the container. In addition, the types of intra-container streams include subtitle (S), Attachment (T), and ordinary data (data, D). Let’s focus on audio streaming, video streaming, and subtitles streaming.

The operation of streams means that we can choose different streams from the input file to operate on, and then output the result we want.

For example, if you have children at home, you should know that there are many English dubbing competitions in the school. 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 actually silent video. Silent video is not a video with the sound turned down to a minimum. It refers to a video with no audio, so that the video is only a picture. For example, for the material video of the previous case 1, the -an command can be used to remove the audio stream and only retain the video stream, that is, only the picture (if not downloaded, you can click here to download).

ffmpeg -i r1ori.mp4 -an -y r1-silent.mp4
Copy the code

Stream #0:1(und): Audio

» ffmPEG-I r1-silent. 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: Stream #0:0(und): Video: H264 (High) (AVC1/0x31637661), YUv420p, 544x960, 1684 KB /s, 29.83 FPS, 29.83 TBR, 11456 TBN, TBC (default) Metadata: Handler_NAME: VideoHandler At least one output file must be specifiedCopy the code

Now even if you put your stereo up and play this video at full volume, you won’t hear anything.

-an is -acodec none. A refers to audio, codec refers to decoder, -acodec refers to audio decoder, which together does not specify audio decoder. The transcoding process is easy to understand by reviewing our article on how to transcode FFMPEG.

As you might have guessed, we can also remove video streaming, subtitle streaming, and so on.

  1. -an Removes the audio stream
  2. -vn Removes the video stream
  3. -sn Deletes subtitle streams
  4. -dn Deletes data streams

Some students may have noticed that the length of our original video is 59 seconds, less than one minute, but 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 being spent? Yeah, it’s just recoding.

Here we’re just getting rid of the audio stream. Is it necessary to re-code? No, so wouldn’t it be nice if we could replicate the video stream?

The optimized command is as follows

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

The command output is instantaneous. We added a parameter -vcodec copy. – Vcodec refers to the video decoder, v refers to the video video, codec refers to the decoder, followed by the name of the decoder, copy copies the input video stream without decoding.

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

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

An error occurs after the command is executed

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

We are prompted that the audio stream is invalid due to a coDEC parameter error. Let’s 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) ......Copy the code

The -c:a copy parameter means that we want to load the aAC audio stream into the MP3 container, which is not possible. Aac audio streams require a dedicated AAC container, and MP3 audio streams require a dedicated MP3 container.

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

It’s easy to find the reason. Let’s change the output mp3 format to AAC format

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

Most of the audio streams in mp4 containers are in AAC format, but what if you had to write a program to extract and store the audio from the videos you upload, and the original video you upload is mp3?

To satisfy this scenario, let’s make an MP3 video and try the above command.

1. Transfer the audio in r1ori.mp4 video to MP3

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

Note: since ffmpeg does not have a native MP3 encoder, we specified an external libmp3lame library (although -c:a libmp3lame you can also change libmp3lame to mp3 and actually use libmp3lame). If you execute the command above and return an ERROR like this: Libmp3lame >= 3.98.3 not found ffmpeg does not include the –enable-libmp3lame compiler parameter.

2. Extract the audio of the video

Ffmpeg-i r2.mp4-vn -C :a copy -y r1-silent. Aac 错 误 : Only AAC streams can be muxed by the ADTS muxer Could not write header for output file #0 (incorrect codec parameters ?) : Invalid argumentCopy the code

So, -c:a copy is not everything, so if we want to convert video to audio, we should specify an encoder, aAC or libmp3lame? In terms of sound quality, we prefer libmp3lame, although the best audio encoder that ffMPEG comes with is AAC.

In summary, if you need to output audio in MP3 format, you can use it

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

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
Copy the code

Note: newer versions of FFMPEG support native AAC encoders, so you can use -c: aAC directly, older versions of FFMPEG like 2.x native AAC encoders are not fully supported, you must also specify strict -2 to use.

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

Since FFMPEG is so awesome, can it automatically help us choose the appropriate decoder to handle if we don’t specify it manually?

Very well.

Let’s take mp3, let’s try it out

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)) 
......
Copy the code

Notice that the output procedure code includes the Stream mapping and the next line of code, and ffmPEG does automatically select the libmp3LAME decoder for us.

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