1. Overview

In this article, we'll learn how to use the FFmpeg audio and video converter to extract audio from video files. In addition, we'll see how to specify the output audio format and how to set the bitrate of the extracted audio.

2. Installing FFmpeg

To install FFmpeg on Ubuntu/Debian, we can run the following command:

$ sudo apt install ffmpeg

Now let's make sure it's installed:

$ ffmpeg -version
ffmpeg version 4.4.2-1ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers
...

We have successfully installed FFmpeg.

3. Extracting All Audio Tracks

Importantly, when extracting multiple audio tracks, we must use a container format for the final file. In this example, we chose the mp4 format.

There are two main ways we can extract all tracks from an input video file, depending on the encoding. Let's look at both.

3.1. Extracting Audio Without Re-Encoding

First, we have to select the audio stream to extract. ffprobe will list all the streams:

$ ffprobe video.mkv
...
Input #0, matroska,webm, from 'video.mkv':
  Metadata:
    encoder         : no_variable_data
    creation_time   : 1970-01-01T00:00:00.000000Z
  Duration: 00:23:30.07, start: 0.000000, bitrate: 1392 kb/s
    Stream #0:0: Audio: aac (LC), 48000 Hz, stereo, fltp (default)
    Metadata:
...

We can see that the audio stream format is aac. Now, we can specify a container format for the output audio file:

$ ffmpeg -i video.mkv -map 0:a -acodec copy audio.mp4

First, the -i flag specifies the input video file name. Second, using -map 0:a selects all audio streams we found before. Next, -acodec copy copies the stream without re-encoding. Finally, audio.mp4 will be the name of the output audio file.

The output audio file will be generated quickly since we didn't re-encode the audio stream and just copied it.

3.2. Extracting Audio With Re-Encoding

Same as before, but this time, instead of copying the audio streams, we need to specify the codec after the -acodec flag:

$ ffmpeg -i video.mkv -map 0:a -acodec libmp3lame audio.mp4

We used libmp3lame from the FFmpeg codecs to encode the audio streams and chose mp4 as the container format. Because of the re-encoding from aac to mp3, this will take some time to finish.

4. Extracting Parts of the Audio

Of course, we can also decide to extract only parts of the audio tracks or given streams. Let's use the aac format.

4.1. Extracting Based on Stream

Initially, we extracted all streams. However, we can also select a particular one for extraction with the -map flag:

$ ffmpeg -i video.mkv -map 0:0 -acodec copy audio.aac

Here, we extract only stream 0:0.

4.2. Extracting Based on Time

Previously, we covered cutting videos based on time. Now, we'll learn how to extract a particular portion of the audio based on time:

$ ffmpeg -i video.mkv -map 0:0 -ss 00:03:00 -t 00:00:20.0 -acodec copy audio.aac

The additions are the following:

  • -ss to specify the starting point of the portion
  • -t to specify the duration of the audio to be extracted

Therefore, our example above will extract 20 seconds of audio, starting 3 minutes into the input video file.

5. Specifying the Bitrate Encoding

Additionally, we can also set the bitrate encoding of the output audio file. In particular, we can choose variable bitrate (VBR) or constant bitrate (CBR).

5.1. Variable Bitrate (VBR)

Often, VBR is recommended since it gives us the best balance between quality and file size:

$ ffmpeg -i video.mkv -map 0:0 -q:a 0 -acodec copy audio.aac

Using -q:a 0 outputs audio with variable bitrate and the highest quality. We can set the quality from 0 to 9, where 0 is the highest quality and 9 is the lowest. Also, 4 is the default quality.

5.2. Constant Bitrate (CBR)

Finally, to get audio with a constant bitrate, we can enter the following:

$ ffmpeg -i video.mkv -map 0:0 -b:a 320k -acodec copy audio.aac

Here, -b:a 320k sets the bitrate to 320k. Alternatively, we can set it to 128k256k, or other bitrates.

6. Conclusion

To sum up, we learned how to extract audio from a video file, how to change the output audio format, how to specify the bitrate encoding, and how to extract only a portion of the audio from the video file.