@ECHO off

REM -----------------------------------------------------------------------------------------------------------------------------------------------------

REM                 Here you can force using 1-pass VBR or 2-pass VBR modes independently from the bitrate

REM                 Forced 1-pass VBR Values:    0=OFF    1=ON Normal Quality    2=ON Regular HQ
REM                 Forced 2-pass VBR Values:    0=OFF    1=ON Normal Quality    2=ON Regular HQ    3=ON Extreme HQ
 


SET Force 1-pass VBR=1
SET Force 2-pass VBR=0



REM -----------------------------------------------------------------------------------------------------------------------------------------------------




REM                 Select the desired Custom Quant Matrix for different video bitrates and set the thresholds
REM                 To use the ffmpeg built-in MPEG Standard Matrix select the "Matrix_xxx=" entry

REM                 You can also force the use of Mplex instead of FFmpeg for multiplexing

REM -----------------------------------------------------------------------------------------------------------------------------------------------------



REM -------------------------------
REM Select Matrix for High Bitrate:
REM -------------------------------

SET Matrix_High=Fox New
REM SET Matrix_High=MPEG Adapted
REM SET Matrix_High=Manono1
REM SET Matrix_High=Manono2
REM SET Matrix_High=


REM ---------------------------------
REM Select Matrix for Medium Bitrate:
REM ---------------------------------

REM SET Matrix_Medium=Fox New
SET Matrix_Medium=MPEG Adapted
REM SET Matrix_Medium=Manono1
REM SET Matrix_Medium=Manono2
REM SET Matrix_Medium=


REM ------------------------------
REM Select Matrix for Low Bitrate:
REM ------------------------------

REM SET Matrix_Low=Fox New
REM SET Matrix_Low=MPEG Adapted
SET Matrix_Low=Manono1
REM SET Matrix_Low=Manono2
REM SET Matrix_Low=


REM -----------------------------------------------------------------------------------------------------------------------------------------------------

SET MediumBitrate_threshold=7000
SET LowBitrate_threshold=3500
SET twopass_threshold=3500
SET UseMplex=1



REM =====================================================================================================================================================




SET FF_DRIVE=%~d0
SET FF_PATH=%~p0
SET ORIGINAL=%*
SET CMDLINE=
SET Input=
SET Output=
SET bitrate=
SET size=
SET Height=
SET hq=0
SET CQM=
SET inflag=0

%FF_DRIVE%
CD "%FF_PATH%"


REM -----------------------------------------------------------------------------------------------------------------------------------------------------


:assemble
IF [%1==[ GOTO doit
IF %1==-i GOTO in
IF "[%~x1"=="[.m2v" GOTO out
IF %1==-b:v GOTO bitrate
IF %1==-threads GOTO threads
IF %1==-vf GOTO filter
IF %1==-s GOTO size
IF %1==-aspect GOTO aspect
IF %1==-dc GOTO dc
IF %1==-top GOTO interlaced
IF %1==-minrate GOTO discard
IF %1==-maxrate GOTO discard
IF %1==-bufsize GOTO discard
IF %1==-mbd IF [%2==[rd SET hq=1
IF %1==-mbd IF [%2==[2 SET hq=1
SET CMDLINE=%CMDLINE% %1
SHIFT
GOTO assemble


:in
IF %inflag%==1 IF %UseMplex%==1 CALL :multiplex %ORIGINAL%
IF %inflag%==1 IF %UseMplex%==1 GOTO :EOF
SET CMDLINE=%CMDLINE% %1
SHIFT
SET CMDLINE=%CMDLINE% %1
set in=%1
SHIFT
SET inflag=1
GOTO assemble

:out
SET CMDLINE=%CMDLINE% %1
SET out=%1
SET out_path=%~dp1
SHIFT
GOTO assemble

:bitrate
SET CMDLINE=%CMDLINE% %1
SHIFT
SET CMDLINE=%CMDLINE% %1
SET bitrate=%1
SET /A bitrate=%bitrate:k=%
IF EXIST "%Matrix_Medium%.txt" SET /P CQM=<"%Matrix_Medium%.txt"
If %bitrate% GTR %MediumBitrate_threshold% IF EXIST "%Matrix_High%.txt" SET /P CQM=<"%Matrix_High%.txt"
IF %bitrate% LEQ %LowBitrate_threshold% IF EXIST "%Matrix_Low%.txt" SET /P CQM=<"%Matrix_Low%.txt"
SHIFT
GOTO assemble

:threads
SET CMDLINE=%CMDLINE% %1
SHIFT
SET CMDLINE=%CMDLINE% %1
set threads=-threads %1
SHIFT
GOTO assemble

:filter
SET CMDLINE=%CMDLINE% %1
SHIFT
SET CMDLINE=%CMDLINE% %1
SET FLT=%1
SHIFT
SET CMDLINE=%CMDLINE%=%1
SET filter=-vf %FLT%=%1
SHIFT
GOTO assemble

:size
SET CMDLINE=%CMDLINE% %1
SHIFT
SET CMDLINE=%CMDLINE% %1
set size=-s %1
SHIFT
GOTO assemble

:aspect
SET CMDLINE=%CMDLINE% %1
SHIFT
SET CMDLINE=%CMDLINE% %1
set aspect=-aspect %1
SHIFT
GOTO assemble

:dc
SET CMDLINE=%CMDLINE% %1
SHIFT
SET CMDLINE=%CMDLINE% 10
set dc=-dc 10
SHIFT
GOTO assemble

:interlaced
SET CMDLINE=%CMDLINE% %1
SHIFT
SET CMDLINE=%CMDLINE% %1
IF %1==1 SET interlaced=1
IF %1==0 SET bff=1
SHIFT
GOTO assemble

:discard
SHIFT
SHIFT
GOTO assemble


REM -----------------------------------------------------------------------------------------------------------------------------------------------------


:doit
IF [%bitrate%==[ GOTO dont_touch
REG QUERY HKEY_CURRENT_USER\Software\AVStoDVD /v VideoFFmpegHQCmd >"%out_path%$temp.txt"
FOR /F "usebackq tokens=2,*" %%a IN ("%out_path%$temp.txt") DO SET user_hq=%%b
IF EXIST "%out_path%$temp.txt" DEL "%out_path%$temp.txt"
IF NOT [%Force 1-pass VBR%==[0 GOTO 1pass
IF NOT [%Force 2-pass VBR%==[0 GOTO 2pass
REG QUERY HKEY_CURRENT_USER\Software\AVStoDVD /v VideoProfileLevel1 >"%out_path%$temp.txt"
FOR /F "usebackq tokens=3" %%a IN ("%out_path%$temp.txt") DO SET /A threshold=%%a
IF EXIST "%out_path%$temp.txt" DEL "%out_path%$temp.txt"

IF %bitrate% LEQ %twopass_threshold% IF %hq%==1 GOTO 2pass
IF %bitrate% GTR %threshold% GOTO CBR

:1pass
SET CMDLINE=%CMDLINE:!=_$$§§$$_%
SETLOCAL EnableDelayedExpansion
SET CMDLINE=!CMDLINE:-b:v %bitrate%k=-b:v %bitrate%k -maxrate 8000k -g 12 -bf 2 -qmin 1 -lmin 0.75 -mblmin 50 %CQM%!
IF %bitrate% GTR 7990 SET CMDLINE=!CMDLINE:-b:v %bitrate%k=-b:v 7990k!
IF %hq%==1 IF [%Force 1-pass VBR%==[1 SET CMDLINE=!CMDLINE: %user_hq%=!
SET user_hq=%user_hq:-bf 2 =%
IF %hq%==0 IF [%Force 1-pass VBR%==[2 SET CMDLINE=!CMDLINE: -mblmin 50 = -mblmin 50 %user_hq% ! 
SETLOCAL DisableDelayedExpansion
SET CMDLINE=%CMDLINE:_$$§§$$_=!%
_ffmpeg.exe%CMDLINE%
GOTO :EOF


REM -----------------------------------------------------------------------------------------------------------------------------------------------------


:CBR
SET Fixed_Rate=
IF "%size%"=="" GOTO AVSMeter
SET Height=%size:~-3%
GOTO Continue
:AVSMeter
..\AVSMeter\AVSMeter.exe %in% -i >"%out_path%$temp.txt"
FOR /f "usebackq tokens=2,3" %%a IN ("%out_path%$temp.txt") DO IF %%a==height: SET Height=%%b
IF EXIST "%out_path%$temp.txt" DEL "%out_path%$temp.txt"
:Continue
IF %Height%==288 IF %bitrate% GTR 7190 SET Fixed_Rate=7190
IF %Height%==240 IF %bitrate% GTR 6330 SET Fixed_Rate=6330
SET ORIGINAL=%ORIGINAL:!=_$$§§$$_%
SETLOCAL EnableDelayedExpansion
IF "%Fixed_Rate%"=="" GOTO NoFix
SET ORIGINAL=!ORIGINAL:-b:v %bitrate%k=-b:v %Fixed_Rate%k!
SET ORIGINAL=!ORIGINAL:minrate %bitrate%k=minrate %Fixed_Rate%k!
SET ORIGINAL=!ORIGINAL:maxrate %bitrate%k=maxrate %Fixed_Rate%k!

:NoFix
SET ORIGINAL=!ORIGINAL: -y "= -y %CQM% "!
SETLOCAL DisableDelayedExpansion
SET ORIGINAL=%ORIGINAL:_$$§§$$_=!%

:dont_touch
_ffmpeg.exe %ORIGINAL%
GOTO :EOF


REM -----------------------------------------------------------------------------------------------------------------------------------------------------


:2pass
IF %bitrate% GTR 7990 SET bitrate=7990
IF EXIST "%out_path%ffmpeg*.log" DEL "%out_path%ffmpeg*.log"
IF EXIST "%out_path%pass1.m2v" DEL "%out_path%pass1.m2v"
IF [%interlaced%==[1 SET il=-flags +ilme+ildct -alternate_scan 1 -top 1
IF [%bff%==[1 SET il=-flags +ilme+ildct -alternate_scan 1 -top 0
IF NOT [%Force 2-pass VBR%==[1 IF NOT [%Force 2-pass VBR%==[2 IF EXIST "MPEG Adapted.txt" SET /P CQM=<"MPEG Adapted.txt"
IF NOT [%Force 2-pass VBR%==[1 SET first_hq=-b_strategy 2 -brd_scale 2 -profile:v 4
SET second_hq=-pre_dia_size 5 -dia_size 5 -qcomp 0.7 -qblur 0 -preme 2 -me_method dia -sc_threshold 0 -sc_factor 4 -bidir_refine 4 -profile:v 4 -mbd rd -mbcmp satd -precmp satd -cmp satd -subcmp satd -skipcmp satd
SET user_hq=%user_hq:-bf 2 =%
IF [%Force 2-pass VBR%==[2 SET second_hq=%user_hq%
IF [%Force 2-pass VBR%==[1 SET second_hq=


_ffmpeg.exe -i %in% %aspect% %threads% %filter% %size% %dc% -g 12 -bf 2 -maxrate 8000k -q:v 2 %first_hq% %CQM% -pass 1 -passlogfile "%out_path%ffmpeg" -an -c:v mpeg2video %il% "%out_path%pass1.m2v"

_ffmpeg.exe -i %in% %aspect% %threads% %filter% %size% %dc% -g 12 -bf 2 -lmin 0.75 -mblmin 50 -qmin 1 -qmax 31 -maxrate 8000k -b:v %bitrate%k %second_hq% %CQM% -pass 2 -passlogfile "%out_path%ffmpeg" -an -c:v mpeg2video %il% %out%

IF EXIST "%out_path%ffmpeg*.log" DEL "%out_path%ffmpeg*.log"
IF EXIST "%out_path%pass1.m2v" DEL "%out_path%pass1.m2v"
GOTO :EOF


REM -----------------------------------------------------------------------------------------------------------------------------------------------------


:multiplex

:parse
IF [%1==[ GOTO done
IF "[%~x1"=="[.mpg" SET Output=%1
IF %1==-i GOTO GetInput
SHIFT
GOTO parse

:GetInput
SHIFT
SET Input=%Input% %1
SHIFT
GOTO parse

:done
mplex.exe -f 8 -o %Output%%Input%
