Video Editing

8 min read

How to extract audio from video using FFmpeg?

In this guide, learn how to extract audio from any video using FFmpeg. Also, learn about how to customize your extracted audio output.

How to extract audio from video using FFmpeg?
How to extract audio from video using FFmpeg?

So, before we dive into how to extract audio from a video using FFmpeg, let's get a few prerequisites out of the way:

  • Firstly, make sure FFmpeg is installed on your system. You can quickly check by running the command:
ffmpeg -version

If you haven't installed it yet, go to the official FFmpeg website to download and install it.

  • Next, know the exact path to the video file from which you want to extract audio. This will save you from any potential headaches later.
  • Finally, make sure you have the necessary read and write permissions for the directories where your video file is located and where you plan to save the extracted audio file.

Right then, now you’re all set to extract FFmpeg audio from video!

How to Extract Audio from Video without Re-Encoding using FFmpeg?

First, let's identify the audio stream we want to extract. We can use FFprobe to list all the streams in the video file:

ffprobe video.mkv

This will look something like:

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:

In this example, we see that the audio stream format is AAC. Now, we can specify a container format for the output audio file and use FFmpeg to extract the audio stream:

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

Here’s a breakdown of the command:

  • -i video.mkv: Specifies what the input video file is
  • -map 0:a: Gets all audio streams from the input file.
  • -acodec copy: simply copies the audio stream rather than re-encoding as well
  • audio.m4a: Specifies what the name and format of the output audio file will be.

Since we've used -acodec copy, we avoided re-encoding. This makes the extraction process much quicker and maintains the original audio quality. Now, the output audio file will be generated instantly since we're simply copying the stream.

Bear in mind that some container formats don't support certain audio codecs. For example, if the audio format is AAC, using .m4a or .mp4 as the output container works well. However, if the audio codec is less common, you should choose a different container (like .mkv or .wav) that supports the codec.

How to Extract Audio from a Specific Video portion using FFmpeg?

If your video file contains multiple audio streams, you can specify which audio stream to extract using the -map option. The syntax -map 0:a:n allows you to select the specific audio track. Here, n is the index of the audio stream you wish to extract.

For example:

ffmpeg -i multilingual-video.mp4 -map 0:a:1 -acodec copy -y output.mp3

Here’s a breakdown of the command:

  • -i multilingual-video.mp4: Specifies the input video file.
  • -map 0:a:1: Select the second audio stream (index 1) from the input file.
  • -acodec copy: Copies the audio stream without re-encoding.
  • -y: Overwrites the output file if it already exists.
  • output.mp3: Specifies the name of the output audio file.

Here are a few things to take care of:

  • If the specified audio stream does not exist, FFmpeg will generate an error. Check the streams with FFprobe to make sure you have the correct index:
ffprobe multilingual-video.mp4
  • Keep in mind, that if the audio codec is not compatible with the .mp3 extension, make sure you input a compatible extension like .m4a for AAC audio streams.

How to Extract Audio from Video with Re-Encoding using FFmpeg?

The process is similar to extraction without re-encoding, but it also involves specifying a new codec for the audio streams. Here's how you can do it:

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

Let's break down the command:

  • -i video.mkv: Specifies the input video file.
  • -map 0:a: Selects all audio streams from the input file.
  • -acodec libmp3lame: Specifies the audio codec (libmp3lame in this example) for re-encoding.
  • audio.mp4: Specifies the name and format of the output audio file.

By using -acodec libmp3lame, we aim to re-encode the audio streams to MP3 format. This typically takes some time depending on the size and duration of the video file. 

FFmpeg has various codecs like libmp3lame, libfdk_aac, etc. Make sure that you choose one that is supported by the output container format.

How to Extract Multiple/all audio tracks from video using FFmpeg?

With FFmpeg's versatile map command, extracting multiple audio tracks from a video becomes straightforward. Videos often contain various audio tracks, such as different languages or commentary. FFmpeg offers a convenient way to pull out these tracks into distinct audio files.

First, let's identify the audio streams in the video:

ffmpeg -i input_video.mp4 -hide_banner

This will reveal all streams in the input video, including audio streams. Make note of the audio stream IDs you wish to extract. Then, use the -map option alongside the corresponding stream ID to extract. Keep in mind that audio stream indexing starts from 0:

ffmpeg -i input_video.mp4 -map 0:a:0 output_audio_a.mp3
ffmpeg -i input_video.mp4 -map 0:a:1 output_audio_b.mp3

Here,

  • -map 0:a:0 denotes the first audio stream.
  • -map 0:a:1 denotes the second audio stream.

Replace output_audio_a.mp3 and output_audio_b.mp3 with your selected names for each extracted audio track.

Remember, not all videos feature multiple audio tracks. Attempting to extract them from a single-track video will result in errors.

How to Extract a Single Audio Track from video using FFmpeg?

The right method is to copy the audio stream directly from the video file without re-encoding. For this, we use the -c:a copy option, which does exactly this and is generally preferred because it extracts audio without altering its quality. Here's the command:

ffmpeg -i video.mp4 -vn -c:a copy music.mp3

Here, 

  • -i video.mp4: Specifies the input video file.
  • -vn: Indicates that no video will be included in the output.
  • -c:a copy: Copies the audio stream without re-encoding, preserving the original audio quality.
  • music.mp3: Specifies the name of the output audio file.

This approach is ideal for situations where maintaining the highest audio quality is important. You can't change the output format or bitrate though.

However, if you wish to reduce the audio bitrate to save storage space or even ensure compatibility with MP3 players or devices, you can use the following command:

ffmpeg -i video.mp4 -f mp3 -ab 192000 -vn music.mp3

This approach is ideal only if you are okay with re-encoding the audio and if maintaining the original audio quality is not such a critical factor.

Advanced Options for Extracted Audio Output

Choosing the Audio Output Format

Below, we'll cover how to extract audio from different video formats and specify the desired output format:

Extract Audio from MOV

Getting audio from a .mov file using FFmpeg is simple. All you need to do is follow these steps:

  • Step 1: If you haven't already, download and install FFmpeg. Follow the installation instructions provided for your operating system.
  • Step 2: Fire up your terminal or command prompt.
  • Step 3: Use the cd command to navigate to the directory where your .MOV file is. For example:

cd /path/to/your/directory

  • Step 4: Next, use the following command to pull out the audio and save it as an MP3:
ffmpeg -i input.mov -vn -acodec mp3 output.mp3

Here's a breakdown of the commands:

  • -i input.mov: Points to your .mov file. You can replace "input.mov" with your file's name.
  • -vn: This eliminates the video recording since we're just after the audio.
  • -acodec mp3: designates the output audio codec to MP3.
  • output.mp3: Names your output MP3 file.

After executing the command, FFmpeg will extract the audio from the .MOV file and save it as an MP3 file.

Extracting MP3 from MP4

To extract audio in MP3 format from an MP4 file, use the following command:

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

Here, 

  • -i video.mp4: Specifies the input file.
  • -map 0:a: Selects all audio streams from the input file.
  • -y: Overwrites the output file if it already exists.
  • output.mp3: Specifies the name of the output MP3 file.

FFMPEG Extract Audio from MKV

To extract audio from an MKV file, use the following command:

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

Here, 

  • -i video.mkv: Specifies the input MKV file.
  • -map 0:a: Selects all audio streams from the input file.
  • -y: Overwrites the output file if it already exists.
  • output.mp3: Specifies the name of the output MP3 file.

Upon executing the command, FFmpeg will extract the audio from the MKV file and save it as an MP3 file.

Controlling Output Audio Quality through Bitrate

Audio bitrate plays a key in determining the quality of the output audio. It denotes the amount of audio data processed or transferred every second, and is a parameter that directly influences audio fidelity and file size.

When encoding audio with FFmpeg, you can set the output audio quality by specifying the desired audio bitrate using the -b:a option followed by the bitrate value. 

Here's one example to do it:

ffmpeg -i input.mp3 -b:a 192k output.mp3

In this example:

  • -i input.mp3: Specifies the input audio file.
  • -b:a 192k: Sets the audio bitrate to 192 kbps.
  • output.mp3: Specifies the name of the output audio file.

As a general guideline, most audio content sounds decent at bit rates ranging from 96 kbps to 192 kbps. A higher bitrate means more information is relayed every second, which in turn means you get higher-quality audio. But remember, higher bit rates also lead to larger file sizes. So, experiment with different bitrate values to find the right balance between audio quality and file size for your specific needs.

Conclusion

FFmpeg offers a versatile set of tools for content creators, filmmakers, and businesses looking to manipulate audio in their video projects. This article explains how you can seamlessly extract audio from videos—whether it's extracting single/multiple files (with or without re-encoding), trimming audio, or replacing it altogether. We've delved into advanced commands that let you choose your preferred output audio formats, control audio quality by adjusting bitrates, and more.

FAQs

How to replace a video’s audio with FFmpeg?

To replace FFmpeg audio, you can use the -i option, which specifies the input video file, and the -i option again to specify the input audio file you will replace it with. Then, you need to use the -c:v copy option to copy the video stream (without re-encoding), and the -c:a copy option to copy the audio stream from the new audio file. Here's how:

ffmpeg -i input_video.mp4 -i new_audio.mp3 -c:v copy -c:a copy output_video.mp4

How to trim audio using FFmpeg?

FFmpeg provides several commands for trimming audio files. One commonly used command involves utilizing the seeking parameter -ss to specify the starting point for trimming. This way, you can get a basic trim (trim from a specific timestamp to the end of the audio), precise trim (trimming within a specified duration), or extract a specific segment from the audio.

For more advanced trimming and editing, you can also use the trim filter in FFmpeg. This allows you to specify precise start and end times for trimming segments of the audio file.

How to copy audio in FFmpeg?

Simply add the -c copy option to your command. This instructs FFmpeg to copy both video and audio from your input file to the output file. Since it doesn't involve re-encoding, you maintain the original quality of your audio and video. It's a great way to ensure a lossless conversion while saving time.

How to remove audio from video using FFmpeg?

You can use the ‘-an’ option to remove the ffmpeg audio from your video. Here's how:

ffmpeg -i input_video.mp4 -c:v copy -an output_video.mp4
Images or Videos Loading Slow?

We compress them so your users don't need to wait.