How to Extract Multiple Audio Tracks from a Video

On
Extract multiple audio tracks from a video

Video files often contain multiple audio tracks, such as various languages, sound effects, background music, and more. What if you want to extract all these audio tracks into separate files? There are several tools and ways to do this. Today, we're going to look at one of the most popular and powerful tools for audio track extraction. It's extremely flexible and can work on multiple formats of both audio and video files. You can use this audio extraction tool on Linux, macOS, and Windows. Although several GUI versions are available, the raw power of this tool is experienced only in the command line mode.

Extract multiple audio tracks from a video
📷 Easy extraction of multiple audio tracks from a video file

The audio extraction process is often applied to YouTube videos to quickly save these tracks in standalone files for offline listening. You can use it for any video file with single or multiple audio tracks.

Read Also:
The Complete Guide to YouTube Links and Content Extraction

Familiarity with a command-line environment can help. If you prefer a GUI, feel free to switch to one of the applications using this tool, under the hood, saving you from typing commands manually.

Prerequisite for Audio Extraction

There's one and only one prerequisite for multi-track audio extraction. And that is the powerful FFmpeg software. It's free and open source. Install it on your system and you're good to go.

Installation on Windows

To install FFmpeg on Windows, use one of the following commands.

choco install ffmpeg-full
scoop install ffmpeg

Installation on Ubuntu Linux

To install FFmpeg on a Ubuntu machine, use the following command.

sudo apt install ffmpeg

Installation on macOS

Here's the 2-step method to install FFmpeg on a Mac.

# First install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Now install FFmpeg
brew install ffmpeg

Now that the installation phase is complete, let's move on to the actual audio track extraction process.

1. Inspect the Video for Audio Tracks

First, we'll inquire about the audio tracks in the video file. This step will help you know the number of audio tracks, languages, codecs, and much more.

Sample audio tracks information through FFmpeg
📷 Sample audio tracks information

Above is a sample screenshot of audio tracks information. A total of 3 audio tracks are visible. All of them are English audio tracks.

Use the following command to get information about audio tracks:

ffmpeg -i sample.mp4

The generic audio track information format looks like this:

Stream #0:1(eng): Audio: aac (LC), 48000 Hz, stereo, fltp
Stream #0:2(hin): Audio: aac (LC), 48000 Hz, stereo, fltp

This output includes information about the track's language, audio codec, number of tracks, etc.

2. Extract Specific Audio Tracks

Sometimes, you just want to extract select audio tracks from a group in a video file. To do this, a -map switch can be used. Here's an example:

ffmpeg -i sample.mp4 -map 0:a:0 track_english.m4a -map 0:a:1 track_hindi.m4a

In the command shown above:

  • -map 0:a:0 selects the first audio track.
  • -map 0:a:1 selects the second audio track.
  • m4a ensures the original AAC audio is kept as it is.

Re-encoding of the audio track is not done to speed up the extraction process.

3. Extract All Audio Tracks Automatically

Instead of selecting audio tracks, if you just want all the audio tracks to be extracted automatically in separate audio files, you can do so in a single command. Here's how to do it.

ffmpeg -i sample.mp4 -map 0:a -c copy audio_track_%02d.m4a

Here's the breakdown of what's happening in this command.

  • -map 0:a selects all the audio streams.
  • -c copy copies audio tracks without re-encoding them.
  • %02d automatically adds numbers to the files (e.g., audio_track_00.m4a, audio_track_01.m4a)

Feel free to customize the naming pattern of files as per your preferences.

4. Name Audio Files by Language

On Linux and macOS, you can use a shell script to name all the audio files by language or track number. You can scan a video file's metadata to grab all the track information.

for i in $(seq 0 $(($(ffprobe -v error -select_streams a -show_entries stream=index \
-of csv=p=0 sample.mp4 | wc -l) - 1))); do
  lang=$(ffprobe -v error -select_streams a:$i -show_entries stream_tags=language \
  -of default=noprint_wrappers=1:nokey=1 input.mp4)
  ffmpeg -i sample.mp4 -map 0:a:$i -c copy "${lang:-track_$i}.m4a"
done

If each and every audio track has the language tag associated with it, the files will be named as, viz., eng.m4a, hin.m4a, and so on.

And if the language tag is missing, the naming pattern will be track_0, track_1, and so on.

Miscellaneous Tips

Finally, here are some tips to help you get the most out of this tool while extracting audio tracks from different types of video files.

  • If you want to convert the extracted audio into MP3 files, here's how to do it.
    ffmpeg -i sample.mov -vn -c:a libmp3lame -q:a 2 audio_file.mp3
    Here, you have to replace -c copy with the -c:a libmp3lame -q:a 2 switches.
  • You can work with all popular video file formats, viz., MP4, MOV, MKV, and many more.
  • You can pipe output files to other processing tools for an advanced workflow.

Conclusion

Extracting multiple audio tracks with FFmpeg is quick, lossless, and works on almost any video file.

Whether you want just one language or all the streams, a simple -map command can do the job—and with metadata, you can even name files automatically.