Skip to content

Instantly share code, notes, and snippets.

@buchnema
Last active November 4, 2022 02:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buchnema/f00579dc0a64b1ee0f2ab2b9c7746e41 to your computer and use it in GitHub Desktop.
Save buchnema/f00579dc0a64b1ee0f2ab2b9c7746e41 to your computer and use it in GitHub Desktop.
Loudness normalization for mp4 videos
::===============================================================
:: This drag-n-drop script for windows uses ffmpeg's loudnorm
:: filter to correct the perceived loudness (not technical loudness)
:: of mp4 videos without video reencoding or changing metadata.
:: This will make the audio of your videos more even and you won't
:: have to adjust playback volume when watching.
::
:: loudness-part from https://superuser.com/a/1637581
::
::===============================================================
if [%1]==[] goto :eof
:loop
for %%f in (%1) do set filename=%%~nf
ffmpeg -i "%~d1%~p1%~n1%~x1" -vn -acodec copy %1-audio.aac
ffmpeg -i %1-audio.aac -filter_complex "[0:a]loudnorm=I=-14:TP=-2:LRA=11:print_format=summary" -f null x 2>%1.txt
@for /f "tokens=3" %%a in ('findstr /C:"Input Integrated" %1.txt') do (set II=%%a)
echo %II% is the Input Integrated
@for /f "tokens=4" %%a in ('findstr /C:"Input True Peak" %1.txt') do (set ITP=%%a)
echo %ITP% is the Input True Peak
@for /f "tokens=3" %%a in ('findstr /C:"Input LRA" %1.txt') do (set ILRA=%%a)
echo %ILRA% is the Input LRA
@for /f "tokens=3" %%a in ('findstr /C:"Input Threshold" %1.txt') do (set IT=%%a)
echo %IT% is the Input Threshold
@for /f "tokens=3" %%a in ('findstr /C:"Output Integrated" %1.txt') do (set OI=%%a)
echo %OI% is the Output Integrated
@for /f "tokens=4" %%a in ('findstr /C:"Output True Peak" %1.txt') do (set OTP=%%a)
echo %OTP% is the Output True Peak
@for /f "tokens=3" %%a in ('findstr /C:"Output LRA" %1.txt') do (set OLRA=%%a)
echo %OLRA% is the Output LRA
@for /f "tokens=3" %%a in ('findstr /C:"Output Threshold" %1.txt') do (set OT=%%a)
echo %OT% is the Output Threshold
@for /f "tokens=3" %%a in ('findstr /C:"Target Offset" %1.txt') do (set TO=%%a)
echo %TO% is the Target Offset
ffmpeg -i "%~d1%~p1%~n1%~x1" -af loudnorm=linear=true:I=-14:LRA=11:tp=-2:measured_I=%II%:measured_LRA=%ILRA%:measured_tp=%ITP%:measured_thresh=%IT%:offset=%TO%:print_format=summary -c:a aac -b:a 320k %1-normalized.aac
ffmpeg -i "%~d1%~p1%~n1%~x1" -i %1-normalized.aac -c copy -map 0:v:0 -map 1:a:0 -map_metadata 0 -movflags use_metadata_tags %1-normalized.mp4
for %%f in (%1) do set filename=%%~nf
del %1.txt
del %1-audio.aac
del %1-normalized.aac
del %1
ren %1-normalized.mp4 "%filename%.mp4"
shift
if not [%1]==[] goto loop
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment