Windows batch file examples

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
Woodstock
Veteran User
Posts: 3790
Joined: Tue Aug 27, 2013 6:39 am

Windows batch file examples

Post by Woodstock »

Since I've mentioned doing a lot of batch files for the CLI elsewhere, I've been asked to post some examples. I figured the forum was the best place for that, rather than Github.

Most of my scripting is rather simplistic; I'm not trying to "find all files in folder X and process them", I'm simply repeating the same settings over multiple files, primarily for episodic animation and similar programs. So I'll start with a simple one, where everything is explicitly included in the command line:

Code: Select all

title %time% House S1 HD01 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\House\House S1 HD01.mkv" -t 1 -o "V:\tv series\House\Season1\House S1 HD01.m4v"  -f mp4  -O  -w 1920 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 2,2 -E ac3,copy:dts -6 dpl2,none -R Auto,Auto -B 160,0 -D 0,0 --gain 0,0 --audio-fallback ac3 --encoder-preset=medium  --encoder-profile=main  --verbose=1
title %time% House S1 HD02 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\House\House S1 HD02.mkv" -t 1 -o "V:\tv series\House\Season1\House S1 HD02.m4v"  -f mp4  -O  -w 1920 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 2,2 -E ac3,copy:dts -6 dpl2,none -R Auto,Auto -B 160,0 -D 0,0 --gain 0,0 --audio-fallback ac3 --encoder-preset=medium  --encoder-profile=main  --verbose=1
title %time% House S1 HD03 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\House\House S1 HD03.mkv" -t 1 -o "V:\tv series\House\Season1\House S1 HD03.m4v"  -f mp4  -O  -w 1920 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 2,2 -E ac3,copy:dts -6 dpl2,none -R Auto,Auto -B 160,0 -D 0,0 --gain 0,0 --audio-fallback ac3 --encoder-preset=medium  --encoder-profile=main  --verbose=1
title %time% House S1 HD04 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\House\House S1 HD04.mkv" -t 1 -o "V:\tv series\House\Season1\House S1 HD04.m4v"  -f mp4  -O  -w 1920 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 2,2 -E ac3,copy:dts -6 dpl2,none -R Auto,Auto -B 160,0 -D 0,0 --gain 0,0 --audio-fallback ac3 --encoder-preset=medium  --encoder-profile=main  --verbose=1
The start of the line, up to the &, simply puts a title on the Windows CMD title bar that says what file is being processed, and when it started. In this case, the episodes are in individual files, and were ripped by MakeMKV. The options used can be found by checking out the command line documentation.

A slightly more complicated one, where multiple episodes are within a single file, and I want them split out:

Code: Select all

title %time% Rascal HD01 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Rascal Does Not Dream\Rascal Does Not Dream HD01-04.mkv" -c 1-6   -o "V:\anime\subtitled\r\Rascal Does Not Dream\Rascal Does Not Dream HD01.mkv"  -f mkv  -w 1920 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 1,1 -E ac3,fdk_aac -6 dpl2,dpl2 -R Auto,48 -B 160,160 -D 0,0 --gain 0,0 --audio-fallback ac3 --subtitle 1 --markers="C:\Users\jeffb\AppData\Local\Temp\Rascal Does Not Dream HD01-1-chapters.csv" --verbose=1
title %time% Rascal HD02 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Rascal Does Not Dream\Rascal Does Not Dream HD01-04.mkv" -c 7-12  -o "V:\anime\subtitled\r\Rascal Does Not Dream\Rascal Does Not Dream HD02.mkv"  -f mkv  -w 1920 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 1,1 -E ac3,fdk_aac -6 dpl2,dpl2 -R Auto,48 -B 160,160 -D 0,0 --gain 0,0 --audio-fallback ac3 --subtitle 1 --markers="C:\Users\jeffb\AppData\Local\Temp\Rascal Does Not Dream HD01-1-chapters.csv" --verbose=1
title %time% Rascal HD03 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Rascal Does Not Dream\Rascal Does Not Dream HD01-04.mkv" -c 13-19 -o "V:\anime\subtitled\r\Rascal Does Not Dream\Rascal Does Not Dream HD03.mkv"  -f mkv  -w 1920 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 1,1 -E ac3,fdk_aac -6 dpl2,dpl2 -R Auto,48 -B 160,160 -D 0,0 --gain 0,0 --audio-fallback ac3 --subtitle 1 --markers="C:\Users\jeffb\AppData\Local\Temp\Rascal Does Not Dream HD01-1-chapters.csv" --verbose=1
title %time% Rascal HD04 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Rascal Does Not Dream\Rascal Does Not Dream HD01-04.mkv" -c 20-25 -o "V:\anime\subtitled\r\Rascal Does Not Dream\Rascal Does Not Dream HD04.mkv"  -f mkv  -w 1920 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 1,1 -E ac3,fdk_aac -6 dpl2,dpl2 -R Auto,48 -B 160,160 -D 0,0 --gain 0,0 --audio-fallback ac3 --subtitle 1 --markers="C:\Users\jeffb\AppData\Local\Temp\Rascal Does Not Dream HD01-1-chapters.csv" --verbose=1

title %time% Rascal  D01 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Rascal Does Not Dream\Rascal Does Not Dream HD01-04.mkv" -c 1-6   -o "V:\anime\tablet\subtitled\r\Rascal Does Not Dream\Rascal Does Not Dream D01.m4v"  -f mp4  -O  -w 1280 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 22 --vfr -a 1,1 -E ac3,fdk_aac -6 dpl2,dpl2 -R Auto,48 -B 128,160 -D 0,0 --gain 0,0 --audio-fallback ac3 --subtitle 1 --subtitle-burned=1 --verbose=1
title %time% Rascal  D02 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Rascal Does Not Dream\Rascal Does Not Dream HD01-04.mkv" -c 7-12  -o "V:\anime\tablet\subtitled\r\Rascal Does Not Dream\Rascal Does Not Dream D02.m4v"  -f mp4  -O  -w 1280 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 22 --vfr -a 1,1 -E ac3,fdk_aac -6 dpl2,dpl2 -R Auto,48 -B 128,160 -D 0,0 --gain 0,0 --audio-fallback ac3 --subtitle 1 --subtitle-burned=1 --verbose=1
title %time% Rascal  D03 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Rascal Does Not Dream\Rascal Does Not Dream HD01-04.mkv" -c 13-19 -o "V:\anime\tablet\subtitled\r\Rascal Does Not Dream\Rascal Does Not Dream D03.m4v"  -f mp4  -O  -w 1280 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 22 --vfr -a 1,1 -E ac3,fdk_aac -6 dpl2,dpl2 -R Auto,48 -B 128,160 -D 0,0 --gain 0,0 --audio-fallback ac3 --subtitle 1 --subtitle-burned=1 --verbose=1
title %time% Rascal  D04 & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Rascal Does Not Dream\Rascal Does Not Dream HD01-04.mkv" -c 20-25 -o "V:\anime\tablet\subtitled\r\Rascal Does Not Dream\Rascal Does Not Dream D04.m4v"  -f mp4  -O  -w 1280 --crop 0:0:0:0 --loose-anamorphic  --modulus 2 -e x264 -q 22 --vfr -a 1,1 -E ac3,fdk_aac -6 dpl2,dpl2 -R Auto,48 -B 128,160 -D 0,0 --gain 0,0 --audio-fallback ac3 --subtitle 1 --subtitle-burned=1 --verbose=1
In this case, I have to specify which chapters to put in each episode file (the -c parameter). I am also encoding each file twice, as a full-definition MKV with subtitle track, and as a lower-definition MP4 with burned-in subtitles, for use on my tablets.

And I figured out the chapter numbers by playing the MKV in VLC and advancing through to each episode's opening sequence, so no fancy insight there.

If I have the entire series, I'll usually go with the explicit list like this. If it is something I'm getting over a longer period of time, I'll create a "called" batch file, that does the encoding:

Code: Select all

title %time% "%~n1 MP4" & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Star Trek\The Next Generation\%~n1.mkv" -t 1 -o "V:\TV Series\Star Trek\The Next Generation\%~n1.m4v"  -f mp4  -O  -w 1440 --crop 0:0:240:240 --loose-anamorphic  --modulus 2 -e x264 -q 22 --vfr -a 1,1,3 -E fdk_haac,copy:dtshd,copy:ac3 -6 dpl2,none,none -R Auto,Auto,Auto -B 128,0,0 -D 0,0,0 --gain 0,0,0 --audio-fallback ac3 --subtitle 1 --subtitle-forced=1 --subtitle-burned=1  --verbose=1
title %time% "%~n1 MKV" & "C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "Z:\Star Trek\The Next Generation\%~n1.mkv" -t 1 -o "V:\TV Series\Star Trek\The Next Generation\%~n1.mkv"  -f mkv  -w 1440 --crop 0:0:240:240 --loose-anamorphic  --modulus 2 -e x264 -q 20 --vfr -a 1,1,3 -E ac3,copy:dtshd,copy:ac3 -6 dpl2,none,none -R Auto,Auto,Auto -B 160,0,0 -D 0,0,0 --gain 0,0,0 --audio-fallback ac3 --subtitle 1,1 --subtitle-forced=1 --markers="C:\Users\jeffb\AppData\Local\Temp\Star Trek TNG S1 E01-02 Encounter at Farpoint-1-chapters.csv" --verbose=1
The calling batch file provides the file name, which has the extension stripped off with the %-n1 Windows CMD macro. The calling batch file looks like:

Code: Select all

call z:\STTNG.bat "Star Trek TNG S1 E01-02 Encounter at Farpoint.mkv"
call z:\STTNG.bat "Star Trek TNG S1 E03 The Naked Now.mkv"
So there isn't any fancy conditional coding involved in my usage, and the batch jobs are constructed using copy/paste in a text editor. Having an editor with "column mode" is helpful for this, though.
musicvid
Veteran User
Posts: 3717
Joined: Sat Jun 27, 2009 1:19 am

Re: Windows batch file examples

Post by musicvid »

Thank you for this! I will bookmark and refer to these regularly.
Woodstock
Veteran User
Posts: 3790
Joined: Tue Aug 27, 2013 6:39 am

Re: Windows batch file examples

Post by Woodstock »

Everyone is welcome. I'd put some examples in other topics, but...

I did forget to add a link to the actual command line documentation: https://handbrake.fr/docs/en/latest/cli ... rence.html

I should work out the bash and maybe csh equivalents of these one day.
Post Reply