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: 4614
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.
Deleted User 13735

Re: Windows batch file examples

Post by Deleted User 13735 »

Thank you for this! I will bookmark and refer to these regularly.
Woodstock
Veteran User
Posts: 4614
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.
AJSG1969
Posts: 20
Joined: Mon Jul 19, 2021 6:59 pm

Re: Windows batch file examples

Post by AJSG1969 »

This makes sense, however I want to run say only 10 at a time rather than scan a 1000 folders. To do this I will use excel to concatenate the command line for each DVD folder structure.

Question is: How to a run a sequence of commands, by this I mean "Encode Structure 1", wait till complete, then "Encode Structure 2".

Where "Encode Structure 1" is the command string used in your first code set above. Or does CLI process one command at a time anyway?

Thanks.
Woodstock
Veteran User
Posts: 4614
Joined: Tue Aug 27, 2013 6:39 am

Re: Windows batch file examples

Post by Woodstock »

The CLI generally will only process one file at a time, and will do it sequentially. The Windows GUI has the option of scheduling multiple encodes, based on available resources. You could construct shell scripts to do that, too. A variation of the called batch script would work, combined with text files that contain lists of files to process.

CMD.EXE does have looping constructs that can act on a list of files, or the contents of a directory. I don't use them for handbrake, because you have to be careful that it doesn't "slip up" and run a file multiple times, or not at all, if you are updating the directory while the encode is going on.
AJSG1969
Posts: 20
Joined: Mon Jul 19, 2021 6:59 pm

Re: Windows batch file examples

Post by AJSG1969 »

Understood, I don't have too many options though as my Linux machines are on purpose on a physically separate network so am tied to Windows. I'm kind of ok with it if I can solve my current two stumbling blocks:

-cannot save a file to a separate folder (needs to go into the same folder where it scanned for TS files)
-log file, tried --json but no file was saved

Cheers.

Andrew.
Post Reply