Objective

In many situations you need to rip off the audio of a video file into a separate video file. This can be a music track, a voice-over, etc. FFMPEG is an easy tool for doing this eventually if you are a little handy with the command-line.

If you are look for removing the audio track from the video file instead of extracting the audio stream into a new file, you can check the sibbling article in this section here.

You can download the video files I will use for the examples:

Extracting audio with FFMPEG

Extract audio from video

There are different ways of extracting the audio streams from a video file with FFMPEG, and depending on your situation and environment, you can choose the one that better fits your needs.

Extract audio using the -vn parameter

The -vn parameter blocks any video stream to be copied from the input to the output and can be used for transforming a video file into an audio only file.

video file
ffmpeg \
    -i video1.mp4 \
    -vn \
    -y output.mp3 

In the command above, after specifying the video input file, the -vn blocks any video stream to be copied and -y output.mp3 defines the output MP3 filename to use.

Extract audio using the -map parameter

As an alternative to the -vn parameter, you can use the -map parameter that gives you more flexibility defining what streams are copied from the input to the output:

ffmpeg \
    -i video1.mp4 \
    -map 0:a \
    -y output.mp3 

Now, the -map 0:a tells FFMPEG to "map" the all the audio tracks from the first input (it starts counting from zero) to the destination file. As we are not mapping any video stream, the output file will contain audio only.

Extract a particular audio track from a multi-stream video file

In case there are more than one audio stream in the video file, you can specifiy what audio stream you want to extract with -map 0:a:n, where n is the audio track you want to extract.

video file
ffmpeg \
    -i multilingual-video.mp4 \
    -map 0:a:1 \
    -y output.mp3 

The example, copies the second audio stream (the Italian audio track) from the first input parameter (-map 0:a:1)) to output.mp3

Extract multiple audio streams

MP3 and WAV audio formats do not support multiple audio streams in the same file. They have channels for left and right in stereo files, but not multiple channels for (by example) having different languages.

However, M4A and WMA technically support multiple audio streams, but their use is not very extended.

extract 2 audio tracks
ffmpeg \
    -i multilingual-video.mp4 \
    -map 0:a:1 \
    -map 0:a:2 \
    -y output.m4a

Particular format conversions

Most if not all the examples above work for extracting audio tracks from any video format to any audio format.

But for those that are a little afraid of experimenting with the FFMPEG parameters, below are command templates ready to use:

Extract audio from MP4 to MP3

ffmpeg \
    -i video.mp4 \
    -map 0:a \
    -y output.mp3

Extract audio from MP4 to WAV

ffmpeg \
    -i video.mp4 \
    -map 0:a \
    -y output.wav

Extract audio from MKV to MP3

ffmpeg \
    -i video.mkv \
    -map 0:a \
    -y output.mp3

Extract audio from MKV to WAV

ffmpeg \
    -i video.mkv \
    -map 0:a \
    -y output.wav

Convert video to audio

The best thing of FFMPEG is that has many default options that cover most of the use cases and allow you to use very simple commands.

If you are OK with the defaults, you can simple convert a video file into an audio file with this simple command:

ffmpeg -i [video_file] [audio_file]

This makes a simple conversion from a video file (MP4, AVI, MKV) into an audio file (MP3, WAV, M4A).

With this simple command we cannot specify what track to choose or the codecs to use, but FFMPEG is smart enough to figure it out in most cases.

Published on January 12th, 2022

Author
David Bosch
David Bosch David is an experienced engineer, now collaborating with JSON2Video.