How to Remove Audio from Video using FFmpeg. Bonus: Add, Extract, Replace

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 copy copies the video track into videoWithoutAudio.mp4
  • -an tell 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 map command.

The general syntax of the map command is -map input_file_index:stream_type_specifier:stream_index.

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.

Also, -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

ffmpeg \
-i video.mp4 \
-i audio.mp3 \
-c copy \
-map 0:v:0 \
-map 1:a:0 \
videoWithAudio.mp4 

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:0 selects the 0th track of the 0th input file (our video input).
  • map 1:a:0 selects 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 

Simple, right?

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.


add remove replace extract audio using ffmpeg
Photo by Stas Knop on Pexels.com

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

  1. remove the audio
  2. 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 \
-map 1:0 \
-c:v copy \
-c:a libmp3lame -q:a 1 \
-shortest \
video_with_newAudio.mp4

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.

The -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.


Conclusion

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
Krishna Rao Vijayanagar
Founder at OTTVerse

Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.

With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as Harmonic Inc., MediaMelon, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.

1200x200-Pallycon

4 thoughts on “How to Remove Audio from Video using FFmpeg. Bonus: Add, Extract, Replace”

  1. Hi there Dr. Krishna,

    Thank you for this excellent article. For an added bonus: How might you remove the audio from an entire directory? I’m on a MAC and was able to successfully remove the audio from a .mov file and export it as a .mp4, but I still haven’t been able to do it in a batch for entire directory. I appreciate any help you can offer. Thanks for your time!

  2. Your first example in the “How to Remove a Specific Audio Track using FFmpeg” section is inaccurate.

    “Also, -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”

    Actually, that will copy every track (-map 0) and *ALSO* copy the 2nd audio track from the first input (-map 0:a:1) *AGAIN*. The audio track will be duplicated in the resulting videoOutput.mp4.

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.