Batch Handbrake Script

other-third-party

#1

Hey everyone,

So I took the time to learn a tiny amount of cmd line and figured I'd share my script in case there are others who could get some use out of it. It's a simple script; uses mediainfo cmd line to compare a numerical value to a variable such as bitrate or resolution, then encodes the files that fall within your parameters using handbrake. I'll attach the .bat files and the code in case you'd rather create the file yourself (for safety reasons). For the attached files, change extension to from .txt to .bat

My scripts are currently set up for 720p encoding, but most of the settings can be easily modified, just carefully look through the script and edit. Search google for handbrake & mediainfo cmd line variables for help on how to edit those.

I know it's not the cleanest, it's literally my first and only script I've ever made.

Some simple instructions:
1. Download MediaInfo cmd line & Handbrake cmd line. Rename them MediaInfo.exe & HandBrakeCLI.exe
2. Create folder to store cmd line & batch files.
3. Run batch script and follow on screen instructions.

1080p to 720p downconvert script:
@echo off
set mediainfo="%~dp0MediaInfo.exe"
set result="%~dp0width.txt"
set handbrake="%~dp0HandBrakeCLI.exe"
SETLOCAL ENABLEDELAYEDEXPANSION
Set /P a=Enter source path:
set a1=%a: =_%
set src=%a1:"=%
if not %src:~-1%==\ set src=%src%\
Set /P b=Enter destination for converted files:
set b1=%b: =_%
set dest=%b1:"=%
if not %dest:~-1%==\ set dest=%dest%\
for /R %a% %%j in (*.*) do (
Echo Filepath is: %%j
%mediainfo% --Inform=Video;%%Width%% "%%j" > %result%
set /P width=<%result%
del %result%
echo Width is: !width!
if !width! GTR 1280 (
set fpath=%%~dpj
set fpath=!fpath: =_!
set trg=!fpath:%src%=%dest%!
if not !trg:~-1!==\ (set trg="!trg:_= !\") else (set trg="!trg:_= !")
set fnl="!trg:"=!%%~nj.mkv"
if not exist !trg! mkdir !trg!
%handbrake% -i "%%j" -o !fnl! -f mkv -e x264 -q 20 --vfr -a 1,1 -E av_aac,ac3 -B 192 -w 1280 --keep-display-aspect -x level=4.1:vbv-bufsize=7000:vbv-maxrate=4500) else (echo No need to transcode.)
)
cmd /k

Bitrate downconvert script:
@echo off
set mediainfo="%~dp0MediaInfo.exe"
set result="%~dp0bitrate.txt"
set handbrake="%~dp0HandBrakeCLI.exe"
SETLOCAL ENABLEDELAYEDEXPANSION
Set /P a=Enter source path:
set a1=%a: =_%
set src=%a1:"=%
if not %src:~-1%==\ set src=%src%\
Set /P b=Enter destination for converted files:
set b1=%b: =_%
set dest=%b1:"=%
if not %dest:~-1%==\ set dest=%dest%\
for /R %a% %%j in (*.*) do (
Echo Filepath is: %%j
%mediainfo% --Inform=General;%%BitRate%% "%%j" > %result%
set /P bitrate=<%result%
del %result%
echo Bit Rate is: !bitrate!
if !bitrate! gtr 4500000 (
set fpath=%%~dpj
set fpath=!fpath: =_!
set trg=!fpath:%src%=%dest%!
if not !trg:~-1!==\ (set trg="!trg:_= !\") else (set trg="!trg:_= !")
set fnl="!trg:"=!%%~nj.mkv"
if not exist !trg! mkdir !trg!
%handbrake% -i "%%j" -o !fnl! -f mkv -e x264 -q 20 --vfr -a 1,1 -E av_aac,ac3 -B 192 -x level=4.1:vbv-bufsize=7000:vbv-maxrate=4500) else (echo No need to transcode.)
)
cmd /k