In this FFmpeg tutorial, we shall understand how to change the audio codec in a media file from one codec/container to another without changing the video. This is very useful if you want to change the audio codec from AAC to MP3, or similar without modifying the video codec.

What are Codecs and Containers?
The most important thing you need to understand is the difference between containers and codecs.
Simply put,
- A codec takes raw audio and video and compresses it so that it can fit in a reasonable amount of space. This compression is done taking video and audio quality into consideration. Read more about Video Compression here where we describe the “why” of video compression.
- The output of the video and audio compression process is packaged into a well-defined format called a container and there are rules and guidelines on how to unpack the container and read the audio + video. There are different container formats such as MP4, AVI, WebM, MKV, etc.

Transcoding Audio using FFmpeg (Change Audio Codecs Without Changing the Video)
In this exercise, we shall re-encode the audio without changing any of the video settings in the media file. This is a preferable option as we don’t want to use time to re-encode the video when it is not needed.
Okay, let’s look at the FFmpeg command line now.
ffmpeg -i input_filename.avi -acodec mp3 -vcodec copy output_filename.avi
Here, you are instructing FFmpeg to
- read the input file named
input_filename.avi
that is in anavi
media container that holds both the audio and video. - copy the video content as is using the
-vcodec copy
parameter (no video re-encoding), - use
mp3
audio codec to re-encode the audio - and provide the output in an avi file with the newly encoded audio.
Transcoding Audio in an Audio-Only File
But, what if your file contains only audio and no video? Then, its simpler! Just ensure that you have the right extensions and FFmpeg will do the conversion for you. In this case, FFmpeg with re-encode the video and provide it in an mp3 container.
ffmpeg -i audio_file.wav audio_file.mp3
That’s it and with these two simple commands, you should be able to transcode your audio from one codec to another (or switch containers). Hope this helps!