FFmpeg is a powerful tool for manipulating audio and video files. One of the common tasks that you may want to perform with FFmpeg is to adjust the volume of an audio stream or a video file. It is very simple to do with FFmpeg which also includes tools to handle audio clipping, Dynamic Range Compression (DRC), Normalization, etc. of an audio file.
In this blog post, we will explain the concept of volume, how to adjust volume using FFmpeg, and some of the problems that you may encounter while doing so.
Table of Contents
Initial Setup: Installing FFmpeg
Prior to delving into file conversions, the first step is to ensure that you have FFmpeg installed on your machine. The official FFmpeg repository (FFmpeg Downloads) houses the latest version and you can pick up a static build from there for your operating system. However, OS-specific package managers are also available for obtaining the latest FFmpeg.
Utilize Chocolatey with:
choco install ffmpeg
Deploy apt-get via:
sudo apt-get install ffmpeg
- Mac OS X:
With Homebrew, simply execute:
brew install ffmpeg
Following the installation, access the terminal or command prompt and traverse to the directory containing your target files. For more examples and options for installing FFmpeg, go here.
After you’ve installed FFmpeg, you are ready to modify the volume of your audio files using FFmpeg. Let’s learn how!
What is volume? Let’s Understand This First!
Volume is a measure of how loud or soft a sound is.
It is usually expressed in decibels (dB), which are logarithmic units that represent the ratio of the sound pressure level to a reference level.
- The reference level for human hearing is 0 dB, which corresponds to the threshold of hearing.
- A sound that is 10 dB louder than another sound has 10 times more sound pressure.
- A sound that is 20 dB louder has 100 times more sound pressure.
The volume of an audio stream or a video file depends on two factors:
- the amplitude of the audio samples
- the gain of the playback device.
The amplitude of the audio samples is the numerical value that represents the magnitude of the sound wave at a given point in time. The gain of the playback device is the factor by which the amplitude of the audio samples is multiplied before being sent to the speakers or headphones. The gain can be controlled by the user through a volume knob or slider – this is the knob what you see in an audio player.
In the next section, we’ll learn how to adjust volume using FFmpeg.
How does FFmpeg handle Audio Volume?
FFmpeg provides a variety of filters and options for adjusting audio volume. Filters are operations that modify the audio or video data in some way.
FFmpeg has several filters that can be used to adjust the volume, such as
compand. The most commonly used filter for volume adjustment is the
volume filter. This filter can increase or decrease the audio level by a specified factor or set it to an absolute value.
To apply a filter to an input file, you need to use the
-af option for audio streams and you also need to specify the name and parameters of the filter in quotes.
Adjust Volume using FFmpeg – Instructions
Let’s dive into the practical aspects of adjusting volume with FFmpeg. Below, we’ll explore the basic syntax for using the
volume filter and provide explanations for each parameter.
The basic syntax for adjusting volume using the
volume filter is as follows:
ffmpeg -i input.mp4 -af "volume=<volume_level>" output.mp4
input.mp4: The input video file.
-af: Stands for audio filter. This option tells FFmpeg to apply an audio filter.
"volume=<volume_level>": This is where you specify the volume adjustment.
<volume_level>can be a positive or negative number, where a positive value increases the volume, and a negative value decreases it.
output.mp4: The output video file.
Examples of Adjusting Volume using FFmpeg
Increase volume of audio using FFmpeg
To increase the volume of an audio track by 3 dB, you can use the following command:
ffmpeg -i input.mp4 -af "volume=3dB" output.mp4
Decrease volume of audio using FFmpeg
To decrease the volume of an audio track by 6 dB, you can use the following command:
ffmpeg -i input.mp4 -af "volume=-6dB" output.mp4
Advanced Volume Adjustment
In addition to basic volume adjustments, FFmpeg offers more advanced options to fine-tune the process.
Dynamic Range Compression
Dynamic Range Compression (DRC) is a technique used to reduce the difference between the loudest and softest parts of audio. This can be helpful in situations where audio levels vary significantly. FFmpeg provides the
acompressor filter to achieve this. Here’s an example:
ffmpeg -i input.mp4 -af "acompressor=threshold=0.05:ratio=10:attack=200:release=1000" output.mp4
threshold: Specifies the level at which the compression starts.
ratio: Sets the compression ratio.
attack: Defines how quickly the compressor responds to changes in audio levels.
release: Determines how quickly the compressor stops compressing once the audio level drops below the threshold.
Normalization using FFmpeg’s loudnorm Filter
Normalization is the process of adjusting the audio volume to a specific target level. FFmpeg’s
loudnorm filter can be used for this purpose:
ffmpeg -i input.mp4 -af "loudnorm=I=-16:LRA=11" output.mp4
I: Specifies the target integrated loudness level in LUFS (Loudness Units Full Scale).
LRA: Sets the Loudness Range adjustment.
Common Issues and Solutions
While adjusting volume using FFmpeg, you may encounter some common issues. Here are a few problems and their solutions:
Avoid Clipping using
Clipping occurs when the audio volume is increased to a level where it exceeds the maximum allowable amplitude, resulting in distortion. To avoid clipping, use the
dynaudnorm filter to adjust audio levels dynamically without exceeding the maximum amplitude:
ffmpeg -i input.mp4 -af "dynaudnorm=p=1" output.mp4
p: Specifies the perceived loudness. Higher values result in stronger normalization.
Avoid Loss of Audio Quality using dynamics filter
Excessive volume adjustment can lead to a loss of audio quality. To mitigate this, consider using a lower
ratio in the
acompressor filter or use the
dynamics filter for more controlled adjustments:
ffmpeg -i input.mp4 -af "dynamics=gate=0.1" output.mp4
gate: Sets the level below which audio is considered silence.
If you need to adjust the volume of multiple files at once, you can use a simple Bash script to automate the process. Here’s an example script that processes all
.mp4 files in a directory:
#!/bin/bash for file in *.mp4; do ffmpeg -i "$file" -af "volume=3dB" "output_$file" done
This script will apply a 3 dB volume increase to all
.mp4 files in the current directory and save the processed files with a “output_” prefix. Read this article for more options (Windows, Linux, MacOS) in FFmpeg batch processing.
Adjusting audio volume is a fundamental task in video engineering and content creation. FFmpeg provides a powerful set of tools and filters, such as
loudnorm, to help you achieve precise volume adjustments.
Check out the rest of our audio-related FFmpeg tutorials on OTTVerse
Thank you and until next time, happy streaming!
Krishna Rao Vijayanagar
Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.
With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as Harmonic Inc., MediaMelon, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.