In this post we are going to show you how to extract the audio from any video file in order to have the audio in a separated audio file using ffmpeg tool.

Basic audio extraction

ffmpeg -i input_video.mp4 -vn -acodec copy output_audio.aac

In this command we are using the parameters:

  • -i input_video.mp4 which indicates the input video file. Note that this video must have an audio stream to extract.

  • -vn this parameters indicate that the video stream is not processed and is not used in the output file.

  • -acodec copy indicates that ffmpeg should only copy (and not process) the audio stream. With this option we make audio extraction a lot faster, but we can not encode it to another audio codec or process the audio to change its quality. (We'll see this later)

  • output_audio.aac specifies the output file. In this case we are using aac extension as the input file had aac audio stream and we have choosen to extract the audio with the -acodec copy option (only extract and not processing) so the result will be in the same codec than the original file.

Extract the audio to other format

Like in many other commands with FFMPEG, you can process the result and encode it to have an output file in a format different than the original. In this case we extract the audio from the input video file and convert it to a mp3 file:

ffmpeg -i input_video.mp4 -vn output_audio.mp3

It isn't even necessary to specify the codec. In this case, as the output file has mp3 extension ffmpeg will encode it as mp3 file using the liblame library. Note that in this process ffmpeg will be converting the audio stream so it is much slower than just extracting the audio stream like in previous example.

In this way, we can convert the audio stream to different audio streams. Here are some popular examples in which we are specifying the audio stream.

Extracting audio as mp3

ffmpeg -i input_video.mp4 -vn -acodec mp3 output_audio.mp3

With the parameter -acodec mp3 we are specifying to use mp3 audio codec

Extracting audio as aac

ffmpeg -i input_video.mp4 -vn -acodec aac output_audio.aac

In this case with the parameter -acodec aac and the output file extension aac we're specifying ffmpeg to use aac.

Extracting audio as vorbis

ffmpeg -i input_video.mp4 -vn -acodec libvorbis output_audio.oga

In this case we're using -acodec libvorbis as parameter and oga as output file extension.

If we are not using a well-known file extension we can get errors from ffmpeg, so use valid file extensions for each case.

Final notes on audio extraction and processing

As with video encoding, audio encoding can have its own options and parameters depending on the format and codec used. With this parameters we can set better or worse audio quality for the output file, controlling things like the sample rate, bitrate and others.

It is important to have in mind that as in many other ffmpeg operations, we can combine audio extraction/audio conversion with other commands. It can be interesting to extract audio only from one part of the input video. For example, extract audio from second 5 to second 10 of the input video could be done with this command:

ffmpeg -i input_video.avi -ss 5 -to 10 -vn -acodec mp3 output_audio.mp3

Read more about cutting a video with ffmpeg here.

    Did you find this article helpful?