Hello people,

This is the Windows batch script file I use to convert MKV extractions from Blu-ray to high quality 720p x264 files for local storage. Initial extraction is carried out with MakeMKV, then the MKV file is dropped into the same folder as this batch file. A copy of ffmpeg.exe must also be present in the same folder.

Copy and paste the below code into Notepad and save out with the name BD-Conv-x264.bat. Double left-click your mouse on the file in File Explorer to launch the file.

Numerous user variables give a wide range of control over an assortment of features during the encoding process. Just change the ones you want to change then save the file before launching to apply the new settings.

It's nowhere near as advanced as the likes of Handbrake, but it may still be a useful educational tool for others with an interest in learning how to use FFmpeg via a batch file. It may also appeal to purists who like to know exactly what's being done to their video.

Note: If you plan to share the file with any modifications, please remove the version number. Otherwise, do with it as you please.


Code:
@ECHO OFF
MODE 112,27

TITLE High Quality 1080p/i MKV to 720p MP4 Transcoder v200921

:: With automatic SRT hard subtitling (.srt file provided by user in same folder)

:: NOTE: Requires ffmpeg.exe in same folder. Download from https://ottverse.com/ffmpeg-builds/


REM *USER VARIABLES*


REM Set 1 to 9-minute test encode mode (0 = off, 1 to 9 = 1 to 9 mins - use to test encoding quality and cropping)
SET tm=0

REM Set video encoding quality (17.0 to 20.0 - scale works backwards, lower number = higher quality larger file)
SET eq=18.5

REM Set AAC audio encoder channels (0 = audio copy, 2 for 2.0 channel, 5 for 5.1 channel, 7 for 7.1 channel)
SET aq=5

REM Set cropping aspect ratio (0 = off/16:9, 1 = 1.85:1, 2 = 2.20:1, 3 = 2.35:1, 4 = 2.40:1, 5 = 2.00:1)
SET cr=0

REM Set cropping y shift (-8 to 8 - use if source sits too high/low in frame for symmetrical cropping, -up, +down)
SET ys=0

REM Set deinterlacing (0 = off, 1 = fast/lower quality, 2 = slow/higher quality)
SET di=0

REM Set denoise level (0 = off, 1 to 9 = lowest to highest - use if bitrate bloats due to excessive noise/grain)
SET dn=1

REM Set debanding (0 = off, 1 = low, 2 = medium, 3 = high - adds fine temporal noise to help combat encoder banding)
SET db=1

REM Set encoder priority (0 = low, 1 = normal, 2 = high)
SET ep=0

REM Set shutdown PC at end of encoding (0 = no, 1 = yes)
SET sh=0

REM *END OF USER VARIABLES*


REM Set encoder priority by ep user variable
SET encPri=LOW
IF %ep% EQU 1 SET encPri=NORMAL
IF %ep% EQU 2 SET encPri=HIGH

REM Set x-minute test encode mode by tm user variable
SET test=
IF %tm% GTR 0 SET test=-t 00:0%tm%:00

REM Set cropping string by cr user variable
SET cropping=
IF %cr% EQU 1 SET cropping=crop=1920:1032:0:24+(%ys%*2),setdar=1.85/1,
IF %cr% EQU 2 SET cropping=crop=1920:864:0:108+(%ys%*2),setdar=2.20/1,
IF %cr% EQU 3 SET cropping=crop=1920:816:0:132+(%ys%*2),setdar=2.35/1,
IF %cr% EQU 4 SET cropping=crop=1920:792:0:144+(%ys%*2),setdar=2.40/1,
IF %cr% EQU 5 SET cropping=crop=1920:960:0:60+(%ys%*2),setdar=2.00/1,

REM Set deinterlacing mode by dm user variable
SET deint=
IF %di% GTR 0 SET deint=yadif=1:0,mcdeint=%di%-1:0:10,framestep=2,

REM Search for subtitles .srt file and enable hard subtitles if present
SET subtitles=
FOR %%a IN (*.srt) DO SET subtitles=subtitles=%%~nxa,

REM Set rescale string (spline36 with error diffusion dithering preserves fine detail with minimal ringing)
SET rescale=zscale=1280:-2:f=spline36:d=error_diffusion

REM Set nlmeans denoiser strength by dn variable (high quality noise reduction with minimal fine detail loss)
SET /A st=(%dn%*10)
SET denoise=
IF %dn% GTR 0 SET denoise=,nlmeans=(%st%/50)+0.8:7:5:3:3

REM Set debanding noise strength by db user variable
SET deband=
IF %db% GTR 0 SET deband=,noise=c0s=%db%:c0f=t

REM Set audio copy or AAC bitrate by aq user variable
SET audio=copy
IF %aq% EQU 2 SET aq=128
IF %aq% EQU 5 SET aq=384
IF %aq% EQU 7 SET aq=512
IF %aq% GTR 0 SET audio=aac -b:a %aq%k

REM Search for input filename and set output filename (plus error trapping)
FOR %%a IN (*.mkv) DO SET infilename=%%~nxa
IF "%infilename%"=="" ECHO. && ECHO   -^> NO SUITABLE INPUT FILE FOUND ^<- && ECHO    Place an MKV file in this folder && ECHO  (view BD-Conv-x264.bat for settings) && PING localhost -n 11 >NUL && EXIT
SET outfilename=%infilename:~0,-3%mp4

REM Run transcoding process
START "" /B /%encPri% /WAIT ffmpeg -i "%infilename%" %test% -preset slow -tune film -aq-mode 2 -vf %cropping%%deint%"%subtitles%"%rescale%%denoise%%deband% -c:v libx264 -crf %eq% -c:a %audio% "%outfilename%"

ECHO. && ECHO  JOB FINISHED && PING localhost -n 6 >NUL

REM Shutdown PC 1 minute after end of encoding by sd user variable
IF %sh% EQU 1 ECHO. && ECHO  SHUTTING DOWN PC && SHUTDOWN /s /t 60

EXIT
TTFN.