Skip to content

Extract audio from a video file with FFmpeg

Extracting audio from a video file is a common task for content creators, podcast producers, and multimedia enthusiasts. Whether you're looking to repurpose the audio from a video interview, create a podcast from a video series, or simply want to enjoy the soundtrack of a movie, FFmpeg provides powerful tools to extract audio from video files quickly and efficiently.

LinkWhy you may want to extract audio from video file

There are several reasons why you might want to extract audio from a video file:

  1. Podcast creation: Convert video interviews or presentations into audio-only podcasts.
  2. AI processing: You may want to take the audio file as an input for an AI model to do some processing like tagging, summarization or content moderation.
  3. Music extraction: Isolate soundtracks or background music from videos.
  4. Accessibility: Create audio versions of video content for visually impaired audiences.
  5. File size reduction: When only the audio is needed, extracting it can significantly reduce file size.
  6. Audio editing: Separate audio for independent editing before recombining with video.

Let's explore how to use FFmpeg to extract audio from video files in various formats.

LinkBasic audio extraction

The simplest way to extract audio from a video file is to use FFmpeg's audio copying feature using the copy flag. This is very fast because it doesn't transcode the audio at all, retaining the quality of the original track:

bash
ffmpeg -i input_video.mp4 \ -vn -acodec copy \ output.m4a

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file
  • -vn: Disables video output
  • -acodec copy: Copies the audio codec without re-encoding
  • output.m4a: Name of the output audio file (format determined by codec)

This command will extract the audio in its original format. If the video contains AAC audio, the output will be an M4A file.

LinkExtracting audio in specific formats

You may want to extract the audio in a specific format, regardless of the source. Here are some common scenarios:

LinkExtracting audio as MP3

bash
ffmpeg -i input_video.mp4 \ -vn -acodec libmp3lame -q:a 4 \ output.mp3

In this command:

  • -acodec libmp3lame: Specifies the MP3 encoder
  • -q:a 4: Sets the audio quality (0-9, lower is better)

LinkExtracting audio as WAV

bash
ffmpeg -i input_video.mp4 \ -vn -acodec pcm_s16le \ output.wav

Here, pcm_s16le specifies Pulse Code Modulation which is a fancy word for uncompressed audio. s16le stands for Signed 16 bit Little Endian. This is the most common format for raw audio. Another popular option is pcm_s24le which is similar but only with 24 bit audio. WAV is the container format.

LinkExtracting audio as FLAC

bash
ffmpeg -i input_video.mp4 \ -vn -acodec flac \ output.flac

FLAC is a lossless format, preserving audio quality while providing some compression.

LinkExtracting a specific audio stream

Before extracting any tracks, you might want to inspect the video file to see how many tracks there are and to figure out which one you want to grab. ffprobe is a tool that is installed with FFmpeg and can be used to inspect a file like this:

bash
ffprobe input-file.mp4

Which will list out all of the streams available in the video, it will look something like this:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input-video.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42mp41isomavc1 creation_time : 2015-08-07T09:13:02.000000Z Duration: 00:00:30.53, start: 0.000000, bitrate: 411 kb/s Stream #0:0[0x1](und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(progressive), 480x270 [SAR 1:1 DAR 16:9], 301 kb/s, 30 fps, 30 tbr, 30 tbn (default) Metadata: creation_time : 2015-08-07T09:13:02.000000Z handler_name : L-SMASH Video Handler vendor_id : [0][0][0][0] encoder : AVC Coding Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 112 kb/s (default) Metadata: creation_time : 2015-08-07T09:13:02.000000Z handler_name : L-SMASH Audio Handler vendor_id : [0][0][0][0]

Stream #0:1 here is a reference to the stream number. The leading 0 is the input ID, which will always be 0 if we only have one input file. The audio track in this file is stream 1, which is the second stream as 0 is the video in this example.

Once you you know which track you want, you can extract it like this:

bash
ffmpeg -i input_video.mp4 \ -map 0:a:1 -vn \ output.m4a

The -map 0:a:1 option selects the second audio stream (streams are zero-indexed).

LinkTrimming audio during extraction

You can extract only a portion of the audio:

bash
ffmpeg -i input_video.mp4 \ -vn -ss 00:01:30 -t 00:00:30 \ output.m4a

This extracts 30 seconds of audio starting at 1 minute 30 seconds into the video.

LinkChanging audio properties during extraction

You can modify audio properties like sample rate and bit rate:

bash
ffmpeg -i input_video.mp4 \ -vn -ar 44100 -ac 2 -ab 192k \ output.mp3

This command sets the audio to 44.1 kHz sample rate, mixes to 2 channels, and 192 kbps bit rate.

LinkChoosing the right audio format

Different audio formats have different benefits and drawbacks:

LinkWhy choose MP3:

Benefits:

  • Widely compatible
  • Good compression (smaller file size)

Drawbacks:

  • Lossy compression (some quality loss)

LinkWhy choose WAV:

Benefits:

  • Uncompressed audio
  • Highest Quality and Lossless
  • Widely supported in audio production

Drawbacks:

  • Large file size and no compression

LinkWhy choose AAC:

Benefits:

  • Better quality than MP3 at similar bit rates
  • Good compatibility with mobile devices

Drawbacks:

  • Less widely supported than MP3

LinkWhy choose FLAC:

Benefits:

  • Lossless compression (no quality loss)
  • Smaller file size than WAV

Drawbacks:

  • Less widely supported than lossy formats

LinkTips for effective audio extraction

  1. Choose the right format: Consider your intended use. MP3 for general purposes, WAV for editing, FLAC for archiving.
  2. Mind the quality settings: Higher quality settings result in larger files. Find the balance that suits your needs.
  3. Check your source: The output quality can't exceed the input quality. Extracting high-quality audio from a low-quality video won't improve the audio.
  4. Preserve metadata: Use the -map_metadata 0 option to keep relevant metadata from the video file.
  5. Normalize audio: If extracting from multiple sources, consider normalizing the audio levels for consistency.
  6. Batch processing: For multiple files, consider writing a script to automate the extraction process.
  7. Verify the output: Always check the extracted audio to ensure it meets your quality standards and contains the expected content.

Remember that the quality of your audio output is dependent on the quality of the audio in the source file. Always start with the highest quality available for the best results. As you become more comfortable with FFmpeg's audio extraction capabilities, you can experiment with more advanced options to fine-tune your output and streamline your workflow.

LinkExtracting audio from video files with Mux

If you have videos hosted with the Mux Video API you can extract audio when static renditions are enabled on the assets. Static renditions give you access to files in mp4 format and an audio-only version of the video. See the guide for enabling static MP4 renditions.

No credit card required to start using Mux.