transcoding audio with ffmpeg
In this chapter, we’ll be going to see how to transcode into audio with FFmpeg!
The general formula is:
Choosing a format
FFmpeg is quite smart, and by the extension, it can determine which codec to use. If you specify “audio.wav” or “audio.mp3” for example, FFmpeg will use the appropriate codec to do the encoding.
It is perfectly guessing most of the time. But if you want to specify the format manually, then the “-f” flag is your friend.
For this, you might want to consult the list of formats:
So, these three commands will do exactly the same, but the last two requires the -f flag.
Setting the bitrate
In most cases. you want to specify the target bitrate you expect from your codec to output. If you are unsure what bitrate is, please read this article’s audio bitrate section.
To specify the audio bitrate, use the “-b:a” option with a corresponding value, e.g.:
- -b:a 320k: For the mp3 codec this is considered high quality.
- -b:a 128k: Lower quality.
- -b:a 64k: Low quality.
For example:
Setting the sample rate
You may want to specify the sample rate to ensure quality or low output file size. Half the sample rate could mean half the output file size. If you are unsure what the sample rate is, please read the “audio sample rate” section of this article.
To specify the audio sample rate, use the “-ar” option with a corresponding value, e.g.:
- -ar 48000: For high quality.
- -ar 44100: For CD quality (still high).
- -ar 22500: A bit of a compromise, not recommended for music, but for speech, it might be enough.
- -ar 8000: Low quality, e.g. if you only want “understandable” speech.
For example:
Setting the channel count
Setting the channel count can be useful, for example, if you have a stereo recording of a single person’s speech. In that case, you might be content with just a mono output half the size of the original recording.
If you are unsure what an audio channel is, please read the “audio channels” section of this article.
To specify the channel count use the “-ac” option with a corresponding value, e.g.:
- -ac 1: For mono
- -ac 2: For stereo
- -ac 6: For 5.1
For example:
Complete command line for converting audio with FFmpeg
This is how you produce a high-quality output:
Check out this documentation about good quality audio transcoding too!.
Lossless formats
If you want to convert audio into a lossless format, here are a few choices for you:
It’s good if you know that flac results in a smaller file than WAV, as WAV doesn’t actually compress by default:
WAV is generally thought of as a lossless format, but keep in mind that the WAV container can contain lossy content too, but by default FFmpeg uses the pcm_s16le format, which is the 16 bit PCM, that could be understood as lossless.