[FFmpeg-user] Quick (I hope) CLI question

Eric Wilde ewilde at gntrains.com
Tue Jul 25 00:06:01 EEST 2017


At 04:24 PM 7/24/2017 -0400, you wrote:
>I want to preserve formatting for audio and video and apply
>normalization to the audio - in batch. Surely there must be a way to script
>this.



Here's the code that I use for both boosting and companding.  If $Compressor
is 1, it makes up parameters for mencoder.  If $Compressor is not 1, it
makes up parameters for ffmpeg.

#
# If the user wants the input volume boosted by a fixed amount, just set
# the boost volume to that amount.
#
if [ ${FixedBoost} -ne 0 ]; then
     BoostVol=${FixedBoost}
#
# Otherwise, if the user wants the input volume boosted so that the peak is
# near 0dB, we need to figure out how much boost is necessary.
#
else
     #
     # Note that we must use ffmpeg to detect the volume of the input file
     # because ffprobe doesn't offer this feature.  So, even if the
     # compressor being used is mencoder, ffmpeg is still required to use
     # this feature.  Also, we look at the first 300 seconds (five minutes)
     # of the input file to get an accurate picture of the maximum volume.
     # This takes about 20-30 seconds but gives a much better idea of what
     # the maximum volume really is.
     #
     # Once we have a boost volume amount, we can apply it with the volume
     # filter
     #
     if [[ ${AudioBoost} -ne 0 && -x ${CompressFFMpeg} ]]; then
         BoostVol=`${CompressFFMpeg} -i $2 -af "volumedetect" -to 300 
-nostats -f null -y /dev/null 2>&1 | grep -e '^\\[Parsed.*max_volume' | sed 
's/^.*max_volume:\\s-\\([0-9\\.]*\\).*$/\\1/g'`

         BoostTrue=`echo "${BoostVol} > 1" | bc`
         if [ ${BoostTrue} -ne 0 ]; then
             BoostVol=`echo "scale=1; ${BoostVol} - 1" | bc`
         else
             BoostVol=0
         fi
     else
         BoostVol=0
     fi
fi
      .
      .
      .
#
# If we are boosting the audio, set the audio filter to boost it by the
# amount that we pre-calculated.  Note that this has no effect on dynamic
# range, it just makes everything louder.
#
BoostTrue=`echo "${BoostVol} > 0" | bc`
if [ $BoostTrue -ne 0 ]; then
     if [ $Compressor == 1 ]; then
         AudioFilter="-af volume=+${BoostVol}dB"
     else
         AudioFilter="-af volume=${BoostVol}dB"
     fi
fi
#
# If we are normalizing the audio, set the audio filter to lessen the
# dynamic range.
#
# For mencoder, the volume normalization filter seems to work well (having
# tried it on quite a bit of recorded TV, which has much too high dynamic
# range in many cases).
#
# For ffmpeg, the compand filter with the parameters shown seems to work
# well also.  Here's the note from tugshank at forum.videohelp.com:
#
#      -af "aformat=channel_layouts=stereo,
#        compand=0 0:1 1:-90/-900 -70/-70 -30/-9 0/-3:6:0:0:0"
#
#      channel_layouts=stereo forces mono to stereo, just in case
#
#      compander (compressor/expander):
#
#           attack - 0
#           release - 1
#           -90 dB in, infinity out
#           -70 dB in, -70 dB out
#           -30 dB in, -9 dB out
#           0 dB in, -3 dB out,
#           soft knee 6
#           0 dB makeup gain
#           input level 0 dB
#           lookahead 0.
#
#      Specifically made for 'whisper+explosion' scenes.
#
# For publishing video, EBU R128 is currently the preferred standard.  To
# use EBU R128, one must build ffmpeg with libebur128.  Normally, this
# isn't the default so you must use "--enable-libebur128" when you run
# configure.  Then, the "loudnorm" audio filter can be used, as in
# "-af loudnorm".  To figure out if loudnorm is available, use the
# following:
#
#      ffmpeg -filters 2>/dev/null | grep loudnorm
#
if [ ${AudioNorm} -ne 0 ]; then
     if [ $Compressor == 1 ]; then
         AudioFilter="-af volnorm=2:0.75"
     else
         AudioFilter="-af \"aformat=channel_layouts=stereo,compand=0 0:1 
1:-90/-900 -70/-70 -30/-9 0/-3:6:0:0:0\""
     fi
fi




More information about the ffmpeg-user mailing list