FFmpeg is a powerful command-line tool for handling multimedia files. It can be used for converting, extracting, and manipulating audio files, including MP3 and other formats. In this guide, we'll explore common FFmpeg commands for working with audio files, including extracting audio from video.
Installing FFmpeg
Before using FFmpeg, ensure it is installed on your system. You can check the installation by running:
ffmpeg -version
If it is not installed, you can install it using the following commands:
On Ubuntu/Debian:
sudo apt update sudo apt install ffmpeg
On macOS (using Homebrew):
brew install ffmpeg
On Windows: Download the latest static build from FFmpeg's official site and add it to your system path.
Extract Audio from a Video File
To extract audio from a video file (e.g., MP4, MKV, AVI) and save it as an MP3 file, use:
ffmpeg -i input_video.mp4 -q:a 0 -map a output_audio.mp3
- -i input_video.mp4 specifies the input file.
- -q:a 0 ensures high audio quality.
- -map a selects the audio stream.
- output_audio.mp3 is the final audio file.
If you want to extract audio in its original format without re-encoding, use:
ffmpeg -i input_video.mp4 -vn -acodec copy output_audio.aac
Here, -vn removes the video stream, and -acodec copy keeps the original audio codec.
Convert Audio to MP3
To convert an audio file (e.g., WAV, AAC) to MP3:
ffmpeg -i input_audio.wav -codec:a libmp3lame -qscale:a 2 output_audio.mp3
- -codec:a libmp3lame specifies the MP3 codec.
- -qscale:a 2 sets high-quality encoding.
For a lower bitrate (e.g., 128kbps):
ffmpeg -i input_audio.wav -b:a 128k output_audio.mp3
Adjusting Audio Bitrate
To change the bitrate of an MP3 file:
ffmpeg -i input.mp3 -b:a 192k output.mp3
This sets the audio bitrate to 192kbps.
Merge Multiple Audio Files
If you have multiple MP3 files and want to merge them:
- Create a text file listing the files:
echo "file 'track1.mp3'" > file_list.txt echo "file 'track2.mp3'" >> file_list.txt
- Merge the files using FFmpeg:
ffmpeg -f concat -safe 0 -i file_list.txt -c copy merged_audio.mp3
This method works if the files have the same format and encoding.
Trim an Audio File
To extract a specific portion of an audio file, use:
ffmpeg -i input.mp3 -ss 00:01:30 -to 00:02:30 -c copy output.mp3
- -ss 00:01:30 starts at 1 minute 30 seconds.
- -to 00:02:30 stops at 2 minutes 30 seconds.
- -c copy ensures no re-encoding.
For re-encoding:
ffmpeg -i input.mp3 -ss 90 -t 60 -acodec libmp3lame output.mp3
Here, -t 60 specifies a 60-second duration.
Increase Audio Volume
To amplify the volume of an audio file:
ffmpeg -i input.mp3 -filter:a "volume=1.5" output.mp3
This increases the volume by 1.5 times. For reducing volume, use values below 1 (e.g., volume=0.5).
Normalize Audio Volume
To normalize the volume of an MP3 file:
ffmpeg -i input.mp3 -af loudnorm output.mp3
This applies automatic loudness normalization.
Convert MP3 to Other Formats
To convert MP3 to WAV:
ffmpeg -i input.mp3 output.wav
To convert MP3 to AAC:
ffmpeg -i input.mp3 -c:a aac output.aac
Extract Audio from Specific Streams in a Video
If a video has multiple audio tracks, list them first:
ffmpeg -i input_video.mp4
Then, extract a specific stream (e.g., second audio track):
ffmpeg -i input_video.mp4 -map 0:a:1 -c copy output_audio.mp3
Conclusion
FFmpeg is an incredibly versatile tool for working with MP3 and other
audio files. Whether you need to extract, convert, trim, merge, or
adjust volume, FFmpeg provides powerful command-line options to get the
job done efficiently. Mastering these commands can significantly enhance
your audio processing workflow!