How to normalize audio using ffmpeg
Option 1: Built-in Normalization Filters
Current ffmpeg has two filters that can be directly used for normalization - although they are already quite advanced, so they do not simply apply gain to reach a peak level. Here they are:
loudnorm : loudness normalization according to EBU R128. You can set an integrated loudness target, a loudness range target, or maximum true peak. This is recommended for publishing audio and video and it is used by broadcasters all over the world.
dynaudnorm : "intelligent" loudness normalization without clipping, which applies normalization dynamically over windowed portions of the file. This may change the characteristics of the sound, so it should be applied with caution.
Also, the volume filter can be used to perform simple volume adjustments. See the Audio Volume Manipulation wiki entry for more.
The loudnorm filter can be used with one pass, but it is recommended to perform two passes, which enables more accurate linear normalization. This is a little hard to automate. Also, if you want a "simple" RMS-based or peak normalization to 0 dBFS (or any other target), read on.
Option 2: Use the ffmpeg-normalize tool
I created a Python program to normalize media files, available on PyPi as well. You simply:
download ffmpeg (choose a static build, version 3.1 or higher)
put the ffmpeg executable in your $PATH by either adding it in, for example, /usr/local/bin , or adding its directory to $PATH
Run pip install ffmpeg-normalize
Use ffmpeg-normalize
For example:
ffmpeg-normalize input.mp4 -o output.mp4 -c:a aac -b:a 192k
Or, to simply batch-normalize a number of audio files and write them as uncompressed WAV to an output folder:
ffmpeg-normalize *.m4a -of /path/to/outputFolder -ext wav
The tool supports EBU R128 (default), RMS and peak. Have a look at ffmpeg-normalize -h for more options and check the README for some examples.
Also, it supports re-encoding with other encoders (e.g., AAC or MP3), or automatic merging of the audio back into the video.
Option 3: Manual normalization
In ffmpeg you can use the volume filter to change the volume of a track. Make sure you download a recent version of the program.
This guide is for peak normalization, meaning that it will make the loudest part in the file sit at 0 dB instead of something lower. There is also RMS-based normalization which tries to make the average loudness the same across multiple files. To do that, do not try to push the maximum volume to 0 dB, but the mean volume to the dB level of choice (e.g. -26 dB).
Find out the gain to apply
First you need to analyze the audio stream for the maximum volume to see if normalizing would even pay off:
ffmpeg -i video.avi -af "volumedetect" -vn -sn -dn -f null /dev/null
Replace /dev/null with NUL on Windows. The
-vn ,
-sn , and
-dn
arguments instruct ffmpeg to ignore non-audio streams during this analysis. This drastically speeds up the analysis.
This will output something like the following:
[Parsed_volumedetect_0 @ 0x7f88c6b06d40] n_samples: 963072
[Parsed_volumedetect_0 @ 0x7f88c6b06d40] mean_volume: -27.2 dB
[Parsed_volumedetect_0 @ 0x7f88c6b06d40] max_volume: -13.2 dB
[Parsed_volumedetect_0 @ 0x7f88c6b06d40] histogram_13db: 8
[Parsed_volumedetect_0 @ 0x7f88c6b06d40] histogram_14db: 77
[Parsed_volumedetect_0 @ 0x7f88c6b06d40] histogram_15db: 365
[Parsed_volumedetect_0 @ 0x7f88c6b06d40] histogram_16db: 1370
As you can see, our maximum volume is -13.2 dB, so we can apply -13.2 dB gain. If you get a value of 0 dB, then you don't need to normalize the audio.
Apply the volume filter:
Now we apply the volume filter to an audio file. Note that applying the filter means we will have to re-encode the audio stream. What codec you want for audio depends on the original format, of course. Here are some examples:
ffmpeg -i input.wav -af "volume=13.2dB" output.mp3
MP4 format: With an MP4 container, you will typically find AAC audio. We can use ffmpeg's build-in AAC encoder.
ffmpeg -i video.mp4 -af "volume=13.2dB" -c:v copy -c:a aac -b:a 192k output.mp4
|