Objective
Learn the basics of audio manipulation and conversion with FFMPEG.
Distributions
FFMPEG is available for nearly all Linux distributions.
Requirements
A working Linux install with FFMPEG.
Difficulty
Easy
Conventions
- # – requires given linux commands to be executed with root privileges either directly as a root user or by use of
sudo
command - $ – requires given linux commands to be executed as a regular non-privileged user
Introduction
Audio formats are often easier to manage than video ones, but that doesn’t mean that they don’t come with their own headaches. For the most part, audio problems stem from DRM and losses in quality. While FFMPEG can’t always help with DRM, it can help you to convert your files without losing quality.
Converting Audio
First off, you need to learn the most basic way to convert an audio file.
$ ffmpeg -i song.mp3 song.ogg
FFMPEG uses the -i
flag to designate the beginning of input. After the input file(s), it looks for options and the output. In this case, only the output is present.
Retaining Quality
Not all audio formats are equal. Formats like flac
provide better quality audio than lossy formats like mp3
and ogg
. Regardless, you want to preserve as much quality as possible. To do that, include the -sameq
flag.
$ ffmpeg -i song.mp3 -sameq song.ogg
Set Bitrate
Audio compression decreases the quality of an audio file, but it also creates smaller audio files. The most common way of compressing audio files is decreasing the bitrate of the file.
To set the bitrate of an output file with FFMPEG, use the -ab
flag.
$ ffmpeg -i song.mp3 -ab 192 song.ogg
There are several common bitrates that are used for compression. You can use any number of them, depending on your goal.
$ ffmpeg -i song.mp3 -ab 128 song.ogg
Frequency
Frequency is another factor that determines the quality of the output file. Frequency refers to the sample rate. Higher sample rates help to prevent distortion.
You can set the sample rate with the -ar
flag.
$ ffmpeg -i song.mp3 -ab 192 -ar 44100 song.ogg
Specify Codec
If there is a particular codec that you’d prefer to use for encoding, you can specify that to FFMPEG with the -acodec
flag.
$ ffmpeg -i song.ogg -acodec libmp3lame song.mp3
FFMPEG obviously supports multiple codecs. Actually, it uses the output file type to guess them, but you can always explicitly specify them.
$ ffmpeg -i song.mp3 -acodec vorbis song.ogg
Strip Video
What happens if you want to strip the video out of a file and keep the audio? FFMPEG has you covered there too. Of course, you can use this in conjunction with another script like youtube-dl to truly automate the process.
$ ffmpeg -i video.mp4 -vn song.mp3
The -vn
flag removes the video as it transcodes.
Audio Capture
You can use FFMPEG to capture input from a microphone and save it to any output format that you like. By using the previous flags, you can control exactly how the file is saved.
$ ffmpeg -f alsa -i /dev/dsp -ar 44100 -ab 192 recording.flac
Before recording, check /dev
for the mount point of your microphone. You can also use other notations to specify the hardware device you’re using to record.
$ ffmpeg -f alsa -i hw:0 -ar 44100 -ab 192 recording.flac
Closing Thoughts
FFMPEG is an amazing tool for working with audio files. If you’re someone who is concerned with the quality of your music, FFMPEG can be an invaluable resource in ensuring that your are getting the absolute most of your music files.
If you’re interested in learning more about FFMPEG, check out our video guide to explore how to work with videos using FFMPEG.