FFmpeg is a super-powerful tool to add, remove, extract, or replace the audio in your video files. If you have a copy of FFmpeg installed on your computer, then you have just the tool to help you add or remove an audio track from your movie.
Let’s get to it and see how it’s done!
How to Remove Audio from Video using FFmpeg
Many people want to know how to remove or delete the audio track from a video they’ve recorded. This could be because of road-noise or background noise, etc. and they want to get rid of it.
The simplest way to remove or delete audio is to actually copy the video to a new file and discard the audio while doing this. This is a simple technique because it does not involve the re-encoding of the video while you are at it. Here is the command line for achieving this.
ffmpeg.exe -i videoWithAudio.mp4 -c:v copy -an videoWithoutAudio.mp4
-c:v copycopies the video track into
-antell FFmpeg to not copy the audio
Now this works great if you have only one audio track in your movie. But, what happens if you have three or four audio tracks and you want to remove only the second audio track?
How do you remove a specific audio track using FFmpeg?
Let’s take a look in the next section.
How to Remove a Specific Audio Track using FFmpeg
For removing a specific audio track using FFmpeg, you can always use the
The general syntax of the
map command is
So, you could select the 2nd audio track of the video by saying
-map 0:a:1 because the numbering starts at 0. In our example, if your file has 1 video track and two audio tracks, then you can use
-map 0:a:1 to only select the second audio track and copy it to your final output.
-map 0 selects everything from the first input. So, basically, you are selecting everything and then de-selecting the audio.
ffmpeg.exe -i videoWithAudio.mp4 -map 0 -map 0:a:1 -copy videoOutput.mp4
If you have 5 audio tracks and you want to select all but the first one, then you can use the negative
map command. By saying
-map -0:a:0 you are telling FFmpeg to ignore the first audio track. Negative
map is very powerful!
Actually, in the previous section, we learned how to remove audio from your video using
-an, right? You can do this using negative mapping as well as follows.
ffmpeg -i videoWithAudio.mp4 -map 0 -map -0:a videoWithoutAudio.mp4
How to Add an Audio Track using FFmpeg
Now that you removed an audio track, you might want to add one too, right? Here’s how you add an audio track to your video using FFmpeg.
Since you have already read about the
map command in the previous section, this should be very easy. Here is the command line
-i video.mp4 \
-i audio.mp3 \
-c copy \
-map 0:v:0 \
-map 1:a:0 \
This is very easy to understand. What you’re doing is copying the audio and video from two different files using the
map command to an output file.
-map 0:v:0selects the 0th track of the 0th input file (our video input).
map 1:a:0selects the 0th track of the 1st input file (our audio input).
-c copy` copies both the tracks (audio and video) to the output without re-encoding. If you want to re-encode, you can use the appropriate audio/video codecs with your choice of quality settings.
How to Extract Audio from Video using FFMpeg?
Another very useful and common operation is extracting audio from video using FFmpeg. You can do this with and without re-encoding the audio.
In the first case, let’s take a look at extracting the audio out of a media file and storing it without re-encoding it.
Extracting the audio involves discarding the video, right? And this is easily achieved using the
-vn command that removes the video. The
-vn command is similar to the
-an command which removes the audio track.
Then, all you have to do is copy the audio from the source to the destination. This is done using the
-acodec copy command that tells FFmpeg to only copy the audio and not re-encode it.
ffmpeg -i videoWithAudio.mp4 -vn -acodec copy onlyAudio.aac
Now, let’s take a different use case where you might want to re-encode the audio after extracting it. Here’s how to extract the audio from the video using FFmpeg and then encoding it to a different quality level using
libmp3lame and storing it as an mp3 file.
ffmpeg.exe -i videoWithAudio.mp4 -vn -c:a libmp3lame -q:a 1 onlyAudio.mp3
-q:a uses quality presents defined in LAME’s documentation ranging from 0-6 where 0 is high-quality audio and 6 is on the lower end.
How to Replace Audio in a Video using FFMpeg?
This is the final scenario that we will examine today. How do you replace the audio track of a video that already contains audio?
There is a two-step answer to this that we have already discussed which is to
- remove the audio
- add the replacement audio.
But, is there a better or quicker way to do this? Well, with FFmpeg, there is *always* a better way to do something 🙂
The general syntax of the
map command is
-map input_file_index:stream_type_specifier:stream_index. So, you could select the 3rd audio track of the 2nd input by saying
-map 1:a:2 because the numbering starts at 0.
ffmpeg -i video_with_audio.mp4 -i newAudio.wav \
-map 0:0 \
-c:v copy \
-c:a libmp3lame -q:a 1 \
In our situation, we want to take the video from one file and the audio from another file. So, the
map command comes in handy here as shown above. We are selecting the 0th track from the 1st input (our video), and the 0th track from the 2nd input (our audio).
Then we copy the video as is and re-encode the audio before putting them together in our new file. If you don’t want to re-encode the audio, you could simply say
-a:c copy and the audio track will get copied and not re-encoded.
-shortest command is used to ensure that the conversion stops when the length of the shorter of the two inputs is reached. You can discard this if it does not matter in your use case.
There you have it – now you know how to add, remove, replace, or extract audio from a video using FFmpeg.
A lot of these advanced options are discussed in FFmpeg’s documentation.
Krishna Rao Vijayanagar
I’m Dr. Krishna Rao Vijayanagar, founder of OTTVerse. I have a Ph.D. in Video Compression from the Illinois Institute of Technology, and I have worked on Video Compression (AVC, HEVC, MultiView Plus Depth), ABR streaming, and Video Analytics (QoE, Content & Audience, and Ad) for several years.
I hope to use my experience and love for video streaming to bring you information and insights into the OTT universe.