Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.03 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Purpose: Use ffmpeg to normalize .m4a audio files to bring them up to max volume, if they at first have negative db volume. Doesn't process them if not. Keeps bitrate same as source files.
  4. # Parameters: $1 should be the name of the directory containing input .m4a files.
  5. #   $2 should be the output directory.
  6.  
  7. INPUTDIR=/Users/Hafiz/Movies/NORMALISING_FACTORY/TONORMALIZE
  8. OUTPUTDIR=/Users/Hafiz/Movies/NORMALISING_FACTORY/OUTPUTNORMALIZED
  9.  
  10. <<"COMMENT"
  11.  
  12. # For ffmpeg arguments http://superuser.com/questions/323119/how-can-i-normalize-audio-using-ffmpeg
  13. # and
  14. # https://kdecherf.com/blog/2012/01/14/ffmpeg-converting-m4a-files-to-mp3-with-the-same-bitrate/
  15. ffmpeg -i test.m4a -af "volumedetect" -f null /dev/null
  16.  
  17. ffmpeg -i test.m4a -af "volumedetect" -f null /dev/null 2>&1 | grep max_volume
  18. # output: max_volume: -10.3 dB
  19.  
  20. ffmpeg -i test.m4a -af "volumedetect" -f null /dev/null 2>&1 | grep 'max_volume\|Duration'
  21. # Output:
  22. #  Duration: 00:00:02.14, start: 0.000000, bitrate: 176 kb/s
  23. # [Parsed_volumedetect_0 @ 0x7f8531e011a0] max_volume: -10.3 dB
  24.  
  25. ffmpeg -i test.m4a -af "volumedetect" -f null /dev/null 2>&1 | grep max_volume | awk -F': ' '{print $2}' | cut -d' ' -f1
  26. # Output: -10.3
  27.  
  28. ffmpeg -i test.m4a 2>&1 | grep Audio
  29. # output: Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 170 kb/s (default)
  30.  
  31. ffmpeg -i test.m4a 2>&1 | grep Audio | awk -F', ' '{print $5}' | cut -d' ' -f1
  32. # output: 170
  33.  
  34. # This works, but I get a much smaller output file. The sound levels do appear normalized.
  35. ffmpeg -i test.m4a -af "volume=10.3dB" -c:v copy -c:a aac -strict experimental output.m4a
  36.  
  37. # Operates quietly.
  38. ffmpeg -i test.m4a -af "volume=10.3dB" -c:v copy -c:a aac -strict experimental -b:a 192k output.m4a -loglevel quiet
  39.  
  40. COMMENT
  41.  
  42. # $1 (first param) should be the name of a .m4a input file, with .m4a extension
  43. # $2 should be name of output file, with extension
  44. function normalizeAudioFile {
  45.     INPUTFILE=$1
  46.     OUTPUTFILE=$2
  47.  
  48.     DBLEVEL=`ffmpeg -i ${INPUTFILE} -af "volumedetect" -f null /dev/null 2>&1 | grep max_volume | awk -F': ' '{print $2}' | cut -d' ' -f1`
  49.  
  50.     # We're only going to increase db level if max volume has negative db level.
  51.     # Bash doesn't do floating comparison directly
  52.     COMPRESULT=`echo ${DBLEVEL}'<'0 | bc -l`
  53.     if [ ${COMPRESULT} -eq 1 ]; then
  54.         DBLEVEL=`echo "-(${DBLEVEL})" | bc -l`
  55.         BITRATE=`ffmpeg -i ${INPUTFILE} 2>&1 | grep Audio | awk -F', ' '{print $5}' | cut -d' ' -f1`
  56.  
  57.         # echo $DBLEVEL
  58.         # echo $BITRATE
  59.  
  60.         ffmpeg -i ${INPUTFILE} -af "volume=${DBLEVEL}dB" -c:v copy -c:a aac -strict experimental -b:a ${BITRATE}k ${OUTPUTFILE} -loglevel quiet
  61.  
  62.     else
  63.         echo "Already at max db level:" $DBLEVEL "just copying exact file"
  64.         cp ${INPUTFILE} ${OUTPUTFILE}
  65.     fi
  66. }
  67.  
  68. for inputFilePath in ${INPUTDIR}/*; do
  69.     inputFile=$(basename $inputFilePath)
  70.     echo "Processing input file: " $inputFile
  71.     outputFilePath=${OUTPUTDIR}/$inputFile
  72.     normalizeAudioFile ${inputFilePath} ${outputFilePath}
  73. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement