There are several reasons you might want to extract just the audio from a video including:
FFmpeg is a versatile, open-source command line tool used to process and transform media files such as video and audio. In this article we'll show you how to use FFmpeg to convert video to mp3 audio. We'll use the mp3 audio format due to broad playback support and compression.
We'll assume you know how to run commands form a terminal or command line and have already installed FFmpeg. If it's not installed go to the FFmpeg download page and install the executable or static version for your operating system.
To follow along with this guide and the demonstrate the results of each command you can download the following source video file from GitHub and save it as input_video.mp4.
The source video looks like this:
The most straightforward way to convert a video to MP3 is with the following FFmpeg command:
ffmpeg -i input_video.mp4 output_audio.mp3
Here, -i
stands for input, which tells FFmpeg to grab the file you want to convert, in this case input_video.mp4
. The second part, output_audio.mp3
, is the name you want to give your converted file. FFmpeg understands that by .mp3
you want audio in the MP3 format, and it does all the work to convert it for you.
Here is the output MP3 file:
It is possible to set the quality of the converted audio file, adjusting the quality is useful to balance the audible quality versus the file size.
To adjust quality, use this command:
ffmpeg -i input_video.mp4 -q:a 2 output_audio.mp3
In this command, -q:a
sets the audio quality. The number after it can be from 0 to 9, where a zero means you want the best quality and the largest file size. Nine means you're okay with low quality if it gives you a smaller file. Picking a 2, like we did here, gives you a good balance: great audio with a reasonable file size.
Here is the mp3 file with quality setting 2:
And, with quality setting 9:
Note that the first MP3 is 351kb, and the second is 149kb.
Sometimes, you only want to trim a sample of the audio, not the whole track. To do that, use this command:
ffmpeg -i input_video.mp4 -ss 00:00:10 -t 00:00:05 output_audio.mp3
The -ss
option tells FFmpeg to start converting at a specific time in the video, and -t
tells it the duration of the audio to convert. In our example, FFmpeg will trim the start at 10 seconds and convert just 5 seconds of audio.
Here is the trimmed MP3 file:
You can speed up or slow down the extracted audio file using the following command:
ffmpeg -i input_video.mp4 -filter:a "atempo=2.0" -vn output_audio.mp3
The -filter:a
applies an audio filter, atempo
, which changes the speed. The 2.0
means double speed. The -vn
is also important - it tells FFmpeg to skip the video part and only work with audio. Setting the atempo
value to 0.5 will convert the audio to play at half speed.
Here is the mp3 sped up using the atempo filter:
An alternative method to speed up or slow down your audio is the rubberband filter. Here's the command:
ffmpeg -i input_video.mp4 -filter:a "rubberband=tempo=1.5" -vn output_audio.mp3
This command also speeds up the audio but uses a different filter, rubberband
, which can give a more natural sound, especially when changing the tempo slightly. The tempo=1.5
speeds up the audio by 50%.
Here is the MP3 sped up using the rubberband filter:
FFmpeg is a useful tool for converting video to MP3 along with hundreds of other use cases. FFmpeg syntax is not always easy to learn or understand which is why we wrote this article. At scale, trying to perform multiple commands at the same time, FFmpeg can become unwieldy, requiring sophisticated server infrastructure.
Shotstack is an FFmpeg alternative that makes it easy to process media files at scale. It provides an easy to use REST API with a simple to understand JSON format making media processing tasks easy to setup.
Here is a JSON example to convert a video to an MP3 file at double speed:
{
"url": "https://github.com/shotstack/test-media/raw/main/captioning/scott-ko.mp4",
"outputs": {
"renditions": [
{
"format": "mp3",
"speed": {
"speed": 2,
}
}
]
}
}
The API is cloud based so you can run commands in parallel without worrying about scaling or managing servers. Our video to mp3 demo shows you how you can automate the processing of videos using code, and build a simple video processing application.
curl --request POST 'https://api.shotstack.io/v1/render' \
--header 'x-api-key: YOUR_API_KEY' \
--data-raw '{
"timeline": {
"tracks": [
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3.amazonaws.com/footage/beach-overhead.mp4"
},
"start": 0,
"length": "auto"
}
]
}
]
},
"output": {
"format": "mp4",
"size": {
"width": 1280,
"height": 720
}
}
}'