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.
Why you may want to extract audio from video file
There are several reasons why you might want to extract audio from a video file:
- Podcast creation: Convert video interviews or presentations into audio-only podcasts.
- 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.
- Music extraction: Isolate soundtracks or background music from videos.
- Accessibility: Create audio versions of video content for visually impaired audiences.
- File size reduction: When only the audio is needed, extracting it can significantly reduce file size.
- 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.
Basic 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:
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.
Extracting audio in specific formats
You may want to extract the audio in a specific format, regardless of the source. Here are some common scenarios:
Extracting audio as MP3
In this command:
- -acodec libmp3lame: Specifies the MP3 encoder
- -q:a 4: Sets the audio quality (0-9, lower is better)
Extracting audio as 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.
Extracting audio as FLAC
FLAC is a lossless format, preserving audio quality while providing some compression.
Extracting 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:
Which will list out all of the streams available in the video, it will look something like this:
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:
The -map 0:a:1 option selects the second audio stream (streams are zero-indexed).
Trimming audio during extraction
You can extract only a portion of the audio:
This extracts 30 seconds of audio starting at 1 minute 30 seconds into the video.
Changing audio properties during extraction
You can modify audio properties like sample rate and bit rate:
This command sets the audio to 44.1 kHz sample rate, mixes to 2 channels, and 192 kbps bit rate.
Choosing the right audio format
Different audio formats have different benefits and drawbacks:
Why choose MP3:
Benefits:
- Widely compatible
- Good compression (smaller file size)
Drawbacks:
- Lossy compression (some quality loss)
Why choose WAV:
Benefits:
- Uncompressed audio
- Highest Quality and Lossless
- Widely supported in audio production
Drawbacks:
- Large file size and no compression
Why choose AAC:
Benefits:
- Better quality than MP3 at similar bit rates
- Good compatibility with mobile devices
Drawbacks:
- Less widely supported than MP3
Why choose FLAC:
Benefits:
- Lossless compression (no quality loss)
- Smaller file size than WAV
Drawbacks:
- Less widely supported than lossy formats
Tips for effective audio extraction
- Choose the right format: Consider your intended use. MP3 for general purposes, WAV for editing, FLAC for archiving.
- Mind the quality settings: Higher quality settings result in larger files. Find the balance that suits your needs.
- 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.
- Preserve metadata: Use the -map_metadata 0 option to keep relevant metadata from the video file.
- Normalize audio: If extracting from multiple sources, consider normalizing the audio levels for consistency.
- Batch processing: For multiple files, consider writing a script to automate the extraction process.
- 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.
Extracting 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.