Monday, February 07, 2022
Windows 10: Bulk Normalize Audio of mp4 Videos
I've seen a Windows solution like what I've produced here, created by someone else many years ago - but I don't know what it was called, nor where to get it anymore. I also think it was only for AVI files. I've resorted to writing my own from scratch to handle MP4's. I'd like to share this with anyone and everyone who are interested as this site has helped me a lot to get me to this point.
I've written a DOS batch script in my Windows 10 that can take multiple *mp4 files (and yes - with spaces in the name) and normalize the volume using mp3gain. (I tried MP4GAIN, but the trial period ended in like 1 hour). This one will process much quicker than MP4GAIN, I found out. I do this because I can specifically control ffmpeg and mp3gain every step of the way and do things that mp4gain or ffmpeg-normalize might not be able to.
By all means, it's not fool-proof, nor is it optimized. Rather it can be a springboard for someone to tune/tweak for their own purposes. I extract the audio as mp3 VBR and let mp3gain do its magic on it and then put it back. I find I can process 5 concurrent normalizations, but it's subject to the available hard disk space if you want to try more at once.
Create the folders - D:\Vids\bin can be anywhere, but have the original, working and completed folders inside.
D:\Vids\bin
D:\Vids\bin\original
D:\Vids\bin\working
D:\Vids\bin\completed
Download the latest - they're free as of 9/1/2020 that I've checked. I'm using the following:
D:\Vids\bin\ffmpeg.exe ( ffmpeg version git-2020-08-28-ccc7120 )
D:\Vids\bin\mp3gain.exe ( mp3gain version 1.4.6 )
Create a D:\Vids\bin\normalize.bat and put this in there:
echo off
setlocal enableextensions enabledelayedexpansion
for %%I in (original\*.mp4) DO (
echo Creating working\%%~nI.bat
echo echo off > "working\%%~nI.bat"
echo echo Unpacking audio and video from %%I . >> "working\%%~nI.bat"
echo echo ..\ffmpeg.exe -v 0 -i "..\%%I" -q:a 0 -map a ".\%%~nI_audio_only.mp3" -vcodec copy -map 0:v ".\%%~nI_video_only.mp4" >> "working\%%~nI.bat"
echo ..\ffmpeg.exe -v 0 -i "..\%%I" -q:a 0 -map a ".\%%~nI_audio_only.mp3" -vcodec copy -map 0:v ".\%%~nI_video_only.mp4" >> "working\%%~nI.bat"
echo echo . >> "working\%%~nI.bat"
echo echo Normalizing extracted audio with Mp3Gain. >> "working\%%~nI.bat"
echo echo ..\mp3gain.exe ".\%%~nI_audio_only.mp3" >> "working\%%~nI.bat"
echo ..\mp3gain.exe ".\%%~nI_audio_only.mp3" >> "working\%%~nI.bat"
echo echo Reconstructing normalized video from component parts. >> "working\%%~nI.bat"
echo echo .\ffmpeg.exe -v 0 -i ".\%%~nI_video_only.mp4" -i ".\%%~nI_audio_only.mp3" -codec copy "..\completed\%%~nI%%~xI" >> "working\%%~nI.bat"
echo ..\ffmpeg.exe -v 0 -i ".\%%~nI_video_only.mp4" -i ".\%%~nI_audio_only.mp3" -codec copy "..\completed\%%~nI%%~xI" >> "working\%%~nI.bat"
echo echo Post-process clean up. >> "working\%%~nI.bat"
echo del ".\%%~nI_audio_only.mp3" >> "working\%%~nI.bat"
echo del ".\%%~nI_video_only.mp4" >> "working\%%~nI.bat"
echo echo Done. >> "working\%%~nI.bat"
echo echo on >> "working\%%~nI.bat"
)
pause
echo on
Place the original mp4's into your D:\Vids\bin\original folder and run normalize.bat. I do it from the cmd.exe DOS prompt, but you can double-click the batch file in Windows Explorer too. It will search the original folder for mp4's and create individual batch files in D:\Vids\bin\working. You can kick off one, two or four of these batch files in D:\Vids\bin\working at once.
Each original mp4 will be unpacked into a mp4 with no audio, and an mp3 VBR with no video.
The video and audio extractions are done at the same time, thinking this will likely produce two files that can be merged back together later without the video or audio being shorter than the other. The video and audio extracts go into D:\Vids\bin\working.
Then mp3gain.exe will be run against the extracted mp3 and then the audio and video streams will be merged again with ffmpeg and placed into the D:\Vids\bin\completed folder.
The temporary unpacked mp4 and mp3's will be deleted once the process completes.
=======
LIMITATIONS:
The filename of the mp4's cannot contain certain special characters (like ! or ♫) but characters such as spaces ( ) [ ] # ' @ - _ are OK. If you find that the generated script fails or exits much too quickly and doesn't process the video file - try renaming the original file.
I don't have a check for ffprobe to see if the embedded audio is already an mp3, and I found that not all aac audio can be normalized by aacgain.exe. Hence I force everything to come out as mp3 VBR at the highest quality. If the original audio of sufficient quality, I don't mind doing this.
REQUEST
If people can extend this script further to make it aware of the various audio types and do an audio copy if it is already mp3, it would be helpful. Or recognize that the AAC audio needs to be converted to mp3 or left alone as AAC.
Make it handle other video file types (Avi, mkv, wmv, etc.)
Check if a failure occurred after each step and exit the script, and allow for a resume feature so that re-extractions do not need to occur.
Add flags to enable compressing the video file using libx264 -crf 18 -preset veryslow to save some space - but this will make things run very slow.
Any other enhancements you can think of.
~Lum