Skip to content
SumGuy's Ramblings
Go back

FFmpeg: Comprehensive Audio Processing Techniques

FFmpeg is an immensely versatile multimedia framework that allows users to record, convert, and stream audio and video files. Renowned for its robustness and wide array of features, FFmpeg is a preferred choice for professionals and hobbyists alike for manipulating multimedia content. Developed under the LGPL or GPL license, it is available for Linux, Windows, and Mac OS X.

What FFmpeg Can Be Used For

FFmpeg can be employed for various tasks including but not limited to:

Working with Audio Files Using FFmpeg

Basic Syntax

Before diving into complex commands, it’s essential to understand the basic syntax of FFmpeg:

ffmpeg [global_options] {[input_file_options] -i input_file} ... {[output_file_options] output_file} ...

Advanced Usage

FFmpeg is not limited to simple conversions. For instance, combining audio files, adding effects, and handling batch operations are part of its extensive capabilities.

Combining Audio Files You can combine multiple audio files into a single file while maintaining the original quality:

ffmpeg -i "concat:input1.mp3|input2.mp3" -acodec copy output_combined.mp3

The above examples of FFmpeg demonstrate just a fraction of what it is capable of. This powerful tool, when mastered, can perform virtually any audio-visual processing task. Combining its functionality with scripting can automate routine tasks, making it a valuable asset in any multimedia professional’s toolkit.

Some more examples designed to work with multiple files.

1. Convert All WAV Files to MPL3 with Specified Quality

This script will convert all .wav files in the current directory to .mp3 format using the libmp3lame encoder with a quality scale of 2.

for f in *.wav; do ffmpeg -i "$f" -codec:a libmp3lame -qscale:a 2 "${f%.wav}.mp3"; done

2. Optimize All MP3 Files to a Lower Bitrate

To reduce the file size of all .mp3 files, this script re-encodes them at a bitrate of 128k.

for f in *.mp3; do ffmpeg -i "$f" -b:a 128k "${f%.mp3}_optimized.mp3"; done

3. Convert All Mono Audio Files to Stereo

This command duplicates the mono track to create a fake stereo track in all .mp3 files.

for f in *.mp3; do ffmpeg -i "$foregroundColor" -ac 2 "${f%.mp3}_stereo.mp3"; done

4. Reduce the Volume of All MP3 Files by 50%

This script decreases the volume of all .mp3 files by 50%.

for f in *.mp3; do ffmpeg -i "$f" -filter:a "volume=0.5" "${f%.mp3}_quieter.mp3"; done

5. Extract Audio from All MP4 Video Files

This one-liner pulls the audio from .mp4 videos without re-encoding, preserving the original audio codec.

for f in *.mp4; do ffmpeg -i "$f" -vn -acodec copy "${f%.mp4}.aac"; done

6. Combine Multiple MP3 Files into One

If you have multiple .mp3 files and need to concatenate them into a single file, the following can be used. Note: This method requires that all input files have the same audio codec and bitrate.

ffmpeg -i "concat:$(ls *.mp3 | tr '\n' '|' | sed 's/|$//')" -acodec copy output_combined.mp3

7. Convert All MP3 Files to WAV Format

This example script converts all .mp4 files in the directory to uncompressed .wav format.

for f in *.mp4; do ffmpeg -i "$f" "${f%.mp4}.wav"; done

8. Batch Normalize Audio Levels of MP3 Files

To normalize the volume level of all .mp3 files, ensuring a consistent audio experience:

for f in *.mp3; do ffmpeg -i "$f" -filter:a loudnorm "${f%.mp3}_normalized.mp3"; done

These examples harness FFmpeg’s power to efficiently process multiple audio files with a single command. Adjust the parameters as needed to fit various use cases or audio formats.

9. Convert Audio to a Different Format with Custom Sampling Rate

This script converts all .wav files to .mp3 format, setting a specific sampling rate of 44.1 kHz which is standard for high quality audio:

for f in *.wav; do ffmpeg -i "$f" -ar 44100 "${f%.wav}.mp3"; done

10. Apply Audio Fade-in and Fade-out Effects

This command applies a 5-second fade-in and a 5-second fade-out effect to all .mp3 files:

for f in *.mp3; do ffmpeg -i "$f" -af "afade=t=in:ss=0:d=5,afade=t=out:st=$(ffmpeg -i "$f" 2>&1 | grep Duration | awk '{print $2}' | tr -d ','):d=5" "${f%.mp3}_fade.mp3"; done

11. Extract a Specific Audio Segment

To extract a 30-second clip starting 60 seconds into the audio file:

for f in *.mp3; do ffmpeg -i "$f" -ss 60 -t 30 "${f%.mp3}_clip.mp3"; done

12. Batch Normalize Audio Files Based on EBU R128 Standard

Normalization based on the EBU R128 standard is performed to handle loudness variations among multiple audio tracks:

for f in *.mp3; do ffmpeg -i "$f" -filter:a loudnorm=I=-23:LRA=7:TP=-2 "${f%.mp3}_normalized.mp3"; done

13. Creating an Audio Spectrogram

Generate a spectrogram image from an audio file:

for f in *.mp3; do ffmpeg -i "$f" -lavfi showspectrumpic=s=hd1080 "${f%.mp3}_spectrogram.png"; done

14. Combine Multiple Audio Files of Different Formats into One

Concatenate audio files while converting them into a uniform format:

ffmpeg -f concat -safe 0 -i <(for f in *.wav *.mp3; do echo "file '$(pwd)/$f'"; done) -c copy output_combined.mp3

Share this post on:

Previous Post
Executing Commands with Asterisks in Docker
Next Post
Mastering Vim: Essential Commands and Tips