Normalize an audio file second by second in Bash, with FFmpeg

You usually don't need a script like the following. I just finished writing it. Its use makes sense for old video recordings with considerable volume changes.

The audio is normalized second by second instead of all at once. The script uses a maximum amplification value (35dB) to prevent every silence from becoming a din.

For this script to work, you must have "ffmpeg" and "sox" installed.

Happy hacking!

#!/bin/bash

input=input.mkv
output=output.mp4
audio=audio.wav
newaudio=combined.wav

ffmpeg -i "$input" -vn -ar 44100 -ac 2 "$audio"
ffmpeg -i "$audio" -f segment -segment_time 1 -c copy out%06d.wav
for f in out*.wav
do
    # detects volume in decibel
    MAX=$(ffmpeg -hide_banner -i "$f" -map 0:a -filter:a volumedetect -f null /dev/null 2>&1 | grep 'max_volume' | awk '{print $5}')
    # removes the minus sign (only if $MAX starts with a "-" (wildcard matching))
    if [[ $MAX == -* ]]; then MAX="${MAX:1}"; fi
    # set a maximum volume amplification
    if (( $(echo "$MAX > 35.0" | bc -l) )); then MAX="35.0"; fi
    echo $f" -> "$MAX
    ffmpeg -i $f -af "volume="$MAX"dB" max$f
done

# Before merging the audio files with sox, we need to set up an high max number of files to be concatenated
ulimit -n 16384 # https://www.spinics.net/lists/sox-users/msg00167.html
sox maxout*.wav $newaudio

rm *out*.wav
rm $audio

# now we replace the old audio with the new audio (https://superuser.com/a/1137613)
ffmpeg -i "$input" -i "$newaudio" -c:v copy -map 0:v:0 -map 1:a:0 "$output"
rm $newaudio

Being there

The more I extend my attention to the breath during the days, the more I realize that the meaning of existence is simply "being there".

Expressions like "high self-esteem" or "low self-esteem" lose their meaning; they are not even thinkable anymore, we could remove them from the vocabulary.

It's a different way of being in the world.

Being there (Francesco Galgani's art, November 13, 2021)

(November 13, 2021, go to the art gallery)

Al Nero

Iniziai, dunque, a provar una gioia di sapor nostalgico per ciò che perdevo ogni giorno e ritenevo importante e vero...
 
La notte era nera come non mai,
permetteva però  di ri-veder le stelle!
 
Alessandro Pacenti , dal “Diario di un Iniziato”.
 
11/11/2021
 

Pages

Subscribe to Informatica Libera - Francesco Galgani's Blog RSS