How to Change the Volume of a Media File using FFmpeg

7 November 2022 | 3 min read
Casper Kloppenburg

Prerequisites

FFmpeg is a free and open-source video editing tool capable of trimming, cropping, concatenating, muxing, and transcoding almost any type of media file you throw at it.

It's also a very robust solution for implementing video automation, as we use it extensively in our own video editing API. For this tutorial we'll use FFmpeg 5.1.2, but any recent version will do.

Changing the volume of a video file

As we are only changing the audio track, we do not need to re-encode the video. Here's how:

$ ffmpeg -i input.mp4 -af "volume=0.5" -c:v copy output.mp4
  • With the -af "volume=0.5" argument, we're applying a volume filter to the audio track. It accepts a value that indicates how much to increase the volume by. A value less than 1 decreases the volume, and a value greater than 1 increases it. We are applying a value of 0.5, which means we are halving the audio volume. It is also possible to specify a volume in decibels, so if we want to raise the volume by 3 decibels, we would use volume=3dB.
  • The -c:v copy argument tells FFmpeg not to re-encode the video stream. This is important because FFmpeg re-encodes the video by default even if only an audio filter is applied.

Changing the volume of an audio file

Changing the volume of an audio file is as easy as this:

$ ffmpeg -i input.mp3 -af "volume=0.5" output.mp3

Detecting the volume

With volumedetect, we can find out how much a track's volume can be increased:

$ ffmpeg -i input.mp3 -af "volumedetect" -vn -sn -dn -f null -

The -vn -sn -dn arguments specify that all non-audio streams can be ignored. By using -f null -, FFmpeg won't output to a file, but instead prints out the results:

1$ ffmpeg -i input.mp3 -af "volumedetect" -vn -sn -dn -f null -
2
3...
4[Parsed_volumedetect_0 @ 000002460e7c7600] n_samples: 1414656
5[Parsed_volumedetect_0 @ 000002460e7c7600] mean_volume: -20.9 dB
6[Parsed_volumedetect_0 @ 000002460e7c7600] max_volume: -5.3 dB
7[Parsed_volumedetect_0 @ 000002460e7c7600] histogram_5db: 34
8[Parsed_volumedetect_0 @ 000002460e7c7600] histogram_6db: 495
9[Parsed_volumedetect_0 @ 000002460e7c7600] histogram_7db: 1479
10

The most interesting lines have been highlighted above. According to FFmpeg, the audio's max_volume is about -5.3 dB, so we can safely increase the volume to 5 decibels before any significant audio clipping occurs. The histogram_ values give us an idea of how many samples are being clipped as decibels increase. For a volume increase of 5 dB, this would be 34. However, we shouldn't bump the volume by 7 dB as that will clip 1479 samples.

After we have decided by how many decibels we want to increase the audio, we can apply the volume gain using the commands mentioned above. Therefore, in this case, I'd use -af "volume=5.3dB" to boost the volume by 5.3 decibels.

Start automating today

Start with a full-featured trial with 50 credits, no credit card required.
Get started for free