Skip to content

Instantly share code, notes, and snippets.

@kurumigi
Created October 18, 2009 14:39
Show Gist options
  • Save kurumigi/212695 to your computer and use it in GitHub Desktop.
Save kurumigi/212695 to your computer and use it in GitHub Desktop.
[Windows batch file]MP2Gain
@ECHO OFF
REM =============
REM MP2Gain : Apply "Replay Gain" to mp2 files.
REM =============
setlocal
REM ===SETTING===
set mpg123="%~d0\Program Files\mpg123\mpg123.exe"
set WaveGain="%~d0\Program Files\MP3Gain\WaveGain.exe"
set eyeD3=python "%~d0\Program Files\eyeD3\eyeD3" --no-color
set touch=touch -CM
REM =============
set extensions=.mp2;.mp1;.mp3
set tmpdir=%TMP%\mp2gain_%RANDOM%
REM ===SETTING===
REM ===MAIN ROUTINE===
REM Check Arguments.
IF '%1'=='' (
SET ERRORLEVEL=255
GOTO END
)
REM Check running mode.
FOR %%i IN (%*) DO (
REM Set "Album mode" flag.
FOR %%j IN (/a /at /ta) DO (
IF "%%i"=="%%j" (
SET album=1
)
)
REM Set "Test mode" flag.
FOR %%j IN (/t /at /ta) DO (
IF "%%i"=="%%j" (
SET test=1
)
)
)
REM Make temporary folder.
MKDIR %tmpdir%
REM Decode audio files.
SET tmpcounter=0
SET ERROR=1
FOR %%i IN (%*) DO (
FOR %%j IN (%extensions%) DO (
IF "%%~xi"=="%%j" (
IF EXIST "%%~i" (
SET ERROR=0
CALL :Decode "%%~i"
)
)
)
)
IF ERRORLEVEL 1 GOTO END
IF "%ERROR%"=="1" (
SET ERRORLEVEL=255
GOTO END
)
REM Calculate replay gain.
CALL :WaveGain
IF ERRORLEVEL 1 GOTO END
REM If running "Album mode", Calculate album gain.
IF DEFINED album CALL :GetAlbumGain
REM Output headers to the status data file.
CALL :OutputHeader
SET tmpcounter=0
FOR %%i IN (%*) DO (
FOR %%j IN (%extensions%) DO (
IF "%%~xi"=="%%j" (
IF EXIST "%%~i" (
REM Apply replay gain to mp2 files.
CALL :GetTrackGain
REM Unless running "Test mode", Apply replay gains.
IF NOT DEFINED test CALL :ApplyGain "%%~i"
REM Output track gain data to the status data file.
CALL :OutputTrackGain "%%~i"
)
)
)
)
IF ERRORLEVEL 1 GOTO END
REM If running "Album mode", output album gain data to the status data file.
IF DEFINED album CALL :OutputAlbumGain
REM Display the status data file.
CALL :DisplayStatusFile
:END
REM Delete temporary folder.
IF EXIST %tmpdir% RMDIR /Q /S %tmpdir%
REM If ERRORLEVEL is 255, print the usage message.
IF "%ERRORLEVEL%"=="255" CALL :Usage
EXIT /b %ERRORLEVEL%
REM ===MAIN ROUTINE===
REM ===SUBROUTINES===
:Decode
REM Set a counter.
SET /a tmpcounter+=1
REM Decode the mp2 file to a float format wave file.
ECHO Decoding %~1...
%mpg123% -q --float -w "%tmpdir%\%tmpcounter%.wav" "%~1"
IF ERRORLEVEL 1 EXIT /b %ERRORLEVEL%
REM Preserve time-stamps on original file.
IF NOT "%touch%"=="" %touch% -r "%~1" "%tmpdir%\%tmpcounter%.wav"
EXIT /b %ERRORLEVEL%
REM =================
:WaveGain
REM Calculate replay gain data.
ECHO Analyzing...
%WaveGain% -c -a -l -f "%tmpdir%\mp2gain.log" "%tmpdir%\*.wav" 2>NUL
EXIT /b %ERRORLEVEL%
REM =================
:GetAlbumGain
REM Get albun gain data from the log file.
FOR /f "usebackq skip=2 tokens=4" %%i IN (`find "Recommended Album Gain:" "%tmpdir%\mp2gain.log"`) DO (
SET albumGain=%%i
)
REM Calculate "replaygain_album_peak".
SET albumPeak=0
FOR /f "usebackq skip=3 tokens=4" %%i IN (`find ".wav" "%tmpdir%\mp2gain.log"`) DO (
CALL :GetAlbumPeak %%i
)
CALL :Div %albumPeak% 32767 6
SET albumPeak=%Div_S%
EXIT /b
:GetAlbumPeak
IF %1 GTR %albumPeak% SET albumPeak=%1
EXIT /b %ERRORLEVEL%
REM =================
:GetTrackGain
REM Set a counter.
SET /a tmpcounter+=1
REM Get track gain data from the log file.
FOR /f "usebackq skip=2 tokens=1,4" %%i IN (`find " %tmpcounter%.wav" "%tmpdir%\mp2gain.log"`) DO (
SET trackGain=%%i
SET trackPeak=%%j
)
REM Calculate "replaygain_track_peak".
CALL :Div %trackPeak% 32767 6
SET trackPeak=%Div_S%
EXIT /b %ERRORLEVEL%
REM =================
:ApplyGain
REM Set replay gain tags.
ECHO Apply ReplayGain to %~1...
%eyeD3% --no-tagging-time-frame --set-user-text-frame="replaygain_track_gain:" --set-user-text-frame="replaygain_track_peak:" --set-user-text-frame="replaygain_album_gain:" --set-user-text-frame="replaygain_album_peak:" "%~1" >NUL 2>&1
IF DEFINED album (
%eyeD3% --no-tagging-time-frame --set-user-text-frame="replaygain_track_gain:%trackGain% dB" --set-user-text-frame="replaygain_track_peak:%trackPeak%" --set-user-text-frame="replaygain_album_gain:%albumGain% dB" --set-user-text-frame="replaygain_album_peak:%albumPeak%" "%~1" >NUL
) ELSE (
%eyeD3% --no-tagging-time-frame --set-user-text-frame="replaygain_track_gain:%trackGain% dB" --set-user-text-frame="replaygain_track_peak:%trackPeak%" "%~1" >NUL
)
IF ERRORLEVEL 1 EXIT /b %ERRORLEVEL%
REM Restore preserved time-stamps.
IF NOT "%touch%"=="" %touch% -r "%tmpdir%\%tmpcounter%.wav" "%~1"
EXIT /b %ERRORLEVEL%
REM =================
:OutputHeader
REM Output headers to the status data file.
ECHO. >"%tmpdir%\mp2gain.txt"
ECHO ^ ^ Gain ^| Scale ^| Track >>"%tmpdir%\mp2gain.txt"
ECHO ^ -------------------------------- >>"%tmpdir%\mp2gain.txt"
Exit /b 0
REM =================
:OutputTrackGain
REM Output track gain data to the status data file.
ECHO ^ ^ %trackGain% dB ^| %trackPeak% ^| %~1 >>"%tmpdir%\mp2gain.txt"
Exit /b 0
REM =================
:OutputAlbumGain
REM Output album gain data to the status data file.
ECHO ^ -------------------------------- >>"%tmpdir%\mp2gain.txt"
ECHO ^ ^ %albumGain% dB ^| %albumPeak% ^| ^<ALBUM^> >>"%tmpdir%\mp2gain.txt"
EXIT /b 0
REM =================
:DisplayStatusFile
REM Display the status data file.
TYPE "%tmpdir%\mp2gain.txt"
Exit /b 0
REM =================
:Usage
REM Print the usage message.
ECHO Usage: mp2gain [/a] [/t] ^<infile^> [^<infile 2^> ...]
ECHO.
ECHO OPTIONS
ECHO /a : Calculate album gain.
ECHO /t : Test only. Do NOT apply gains.
EXIT /b 0
REM =================
:Div
REM Division
SET Div_S=
SET Div_X=%1
SET Div_Y=%2
for /L %%i IN (0,1,%3) DO (
CALL :Div_LOOP
)
EXIT /b 0
:Div_LOOP
SET /a "Div_D=Div_X/Div_Y"
SET /a "Div_M=(Div_X-(Div_D*Div_Y))*10"
IF "%Div_S%"=="" (
SET Div_S=%Div_D%.
) ELSE (
SET Div_S=%Div_S%%Div_D%
)
SET /a "Div_X=Div_M"
EXIT /b 0
REM ===SUBROUTINES===
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment