Using ffmpeg to convert audio files.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,498
Reaction score
3,234
Credits
31,331

Using ffmpeg in Linux for Audio File Manipulation​

Introduction​

ffmpeg is a powerful command-line tool used for processing audio and video files. It is widely used for converting, streaming, and recording audio and video. This article will guide you through installing ffmpeg, listening to audio files, converting audio formats, understanding audio properties, and retrieving encoding information.

Installing ffmpeg​

To install ffmpeg on your Linux distribution, use the following commands:
  • Ubuntu/Debian:
    Code:
    sudo apt updatesudo apt install ffmpeg
  • Fedora:
    Code:
    sudo dnf install ffmpeg
  • Arch Linux:
    Code:
    sudo pacman -S ffmpeg

Listening to Audio Files​

To listen to audio files using ffmpeg, you can use the ffplay command, which is part of the ffmpeg suite. For example, to play an MP3 file:
Code:
ffplay audiofile.mp3

Converting Audio Formats​

ffmpeg can convert between various audio formats. Here are some common conversions:
  • Convert MP3 to WAV:
    Code:
    ffmpeg -i input.mp3 output.wav
  • Convert M4A to MP3:
    Code:
    ffmpeg -i input.m4a output.mp3
  • Convert AAC to WAV:
    Code:
    ffmpeg -i input.aac output.wav
  • Convert FLAC to MP3:
    Code:
    ffmpeg -i input.flac output.mp3
  • Convert OGG to WAV:
    Code:
    ffmpeg -i input.ogg output.wav

Understanding Audio Properties​

When working with audio files, it's essential to understand various properties:
  • Audio Channels:Audio channels refer to the number of distinct audio signals. Common configurations include mono (1 channel) and stereo (2 channels).
  • Bit Rate:Bit rate is the amount of data processed per second, measured in kilobits per second (kbps). Higher bit rates generally mean better audio quality.
  • Frequency Rate (Sample Rate):The sample rate is the number of samples of audio carried per second, measured in hertz (Hz). Common sample rates include 44.1 kHz (CD quality) and 48 kHz (professional audio).
  • Sampling Rate:Sampling rate is synonymous with frequency rate. It determines how often the audio signal is sampled per second


    Here's a sample command to convert a WAV file to MP3 while explicitly setting the channels, bitrate, frequency, and sampling rate:

    Code:
     ffmpeg -i input.wav -ac 2 -b:a 192k -ar 44100 output.mp3

    Here's a breakdown of the options used:
    • -ac 2: Sets the number of audio channels to 2 (stereo).
    • -b:a 192k: Sets the audio bitrate to 192 kbps.
    • -ar 44100: Sets the audio sampling rate to 44.1 kHz.
    • This command converts input.wav to output.mp3 with the specified audio properties.

Retrieving Encoding Information​

To get detailed encoding information about an audio file, use the ffprobe command, which is part of the ffmpeg suite:
Code:
ffprobe -v error -showformat -showstreams inputfile.mp3
This command provides comprehensive information about the audio file, including codec, bit rate, sample rate, and more.

Conclusion​

ffmpeg is a versatile tool for managing audio files on Linux. Whether you're converting formats, playing audio, or analyzing file properties, ffmpeg offers robust solutions for all your audio processing needs.

 


Note: Increasing the bitrate or sampling rate, on a file you downloaded from the internet won't actually increase the audio quality of that file. However, using a higher bit rate or sampling rate doesn't hurt anything, except it does make the file slightly larger.

If you're in a band, or a home audio hobbyist, then it makes sense to encode your audio files with the highest settings.

Having said all of this, not all mp3 players are created equal. My electric drum set for example wants the mp3 files to be always at a specific bitrate. Sometimes I can't find a place to download that file with that bitrate. So I use ffmpeg to change the bitrate.
This doesn't really change the quality of the bitrate, it just re-encodes the bitrate to be compatible with my mp3 player.

  1. MP3:
    • 128 kbps: Standard quality, often used for streaming.
    • 192 kbps: Better quality, commonly used for higher-quality streaming.
    • 256 kbps: High quality, often used for digital music purchases.
    • 320 kbps: Very high quality, preferred by audiophiles for MP3 files.
  2. AAC (Advanced Audio Coding):
    • 128 kbps: Standard quality, similar to MP3 but more efficient.
    • 256 kbps: High quality, commonly used by services like Apple Music.
  3. WAV/AIFF:
    • 1411 kbps: Uncompressed CD-quality audio, used for professional audio work.
  4. FLAC (Free Lossless Audio Codec):
    • Variable bitrates, typically around 700-1000 kbps depending on the complexity of the audio.
  5. CD Audio:
    • 1411 kbps: Standard for CDs, providing high-quality uncompressed audio
 
FFMpeg is great !

I use it a lot (be it on Windows) to convert files between various format.
It can be a hassle if you have exotic files and formats, but for regular formats (MP4, MP3, ..) it works like a charm !
 
One thing I've used ffmpeg a lot for is muxing together separate audio and video files. You just need to make sure that the audio and video codecs match well with the container format.

ffmpeg -i path/to/video.*** -i path/to/audio.*** -map 0:0 -map 1:0 -c:v copy -c:a copy output.***
 
I love ffmpeg as I can script with it. I use it for batch-converting videos. But I'll avoid technique discussions, this isn't an anime or archivist forum.

You may want to add ffmpeg's usefulness at (batch) converting images (most people don't know this)... though maybe the video-/image-related items belong in their own article. Maybe and ffmpeg-specific.

Also a worthy mention the -q option and VBR for bitrate, and ffprobe. (Though ffmpeg is big, years and years of using it and I still haven't read the docs; they're a kinda reference for when I need to know something new or foget something old)
 
I fine these to be very good for what I need...
1736291816378.png


1736291843641.gif
 
Yup; agree with @Fanboi on this one. It is VERY easy to use ffmpeg in scripts. I've used it quite a lot for various small utilities:-

  • An app for using multiple USB webcams as a simple, home CCTV system.....either live streaming, or recording - with or without audio - and providing a live 'monitor' while doing so - CamRecord

  • A wee utility for chopping out sections of any video - SimpleVideoTrimmer

  • A simple voice recorder, with the option to select sound card for microphone input - EasyVoiceRecorder

  • A utility to allow indefinite looping of any video - VideoLooper

Strictly speaking, even though I script with ffmpeg, my 'apps' don't keep 100% within the terminal.......but it's still scripts that generate the actual functionality. With the exception of CamRecord - which by necessity is a bit more complex, with multiple options - all of the above are simple, YAD-powered, GUI-based utilities that follow the general Linux principle.....that of doing ONE thing, and doing it to the best of their ability. I'm trying to provide point'n'click functionality, in a simple form, for Linux (and Puppy) noobs.

We use John Van Sickle's statically-compiled ffmpeg packages in Puppy these days - big, yes, but full-featured.......and you can bung 'em into ANY distro and they just work (because being statically-compiled, all required dependencies are 'built-in' to the binaries).

This wouldn't have been popular with Puppy users, say, 12-15 years ago, as Puppy was always meant to keep elderly, low-resource hardware useful & functional - and there was still a LOT of old hardware around in those days - thus Puppy-native apps were always kept as small & tidy as possible. Seeing what's possible with limited resources has encouraged a LOT of ingenuity within our small community over the years; it's amazing what CAN be achieved with just a couple of gigs of RAM, along with a P4, a CoreDuo or a Core2Duo, and a small (ish) hard drive.

Many of us, however, even though we tend to use second-hand and/or refurbs a lot of the time, are now running far more powerful AND capable kit than we used to, since they gradually trickle down the equipment chain as hard-core 'geeks' upgrade. Include here that storage and RAM for older gear is still very reasonably priced ATM, and overall size of the binaries, etc, is a moot point nowadays.

I, too, have been playing about with ffmpeg on & off for years.....and I know I've barely even scratched the surface as to what you can do with it. I still keep finding new uses for it even now.


Mike. ;)
 
Last edited:


Members online


Latest posts

Top