Handbrake batch converting with CLI assistance

handbrake

Using the GUI tool ALWAYS crashes for me so I've come up with a command to convert my files to my needs. It takes a preset that I created in the GUI and converts the file.

I have many videos that I need converted and would like to know if it's possible to batch convert using some sort of script that reads all of the .MOV videos in a folder and converts them to .mp4 using the command I have provided.

I'm running Windows 10.

Here's my command, I'm using nightly builds:

HandBrakeCLI.exe --preset-import-file "preset-name.json" -Z "preset-name" -i video.MOV -o video.mp4

Edited with a clearer request.

Best Answer

OK, I guess you have a bunch of files like

ant.mov
bug.mov
cat.mov
dog.mov
   ︙

in a directory (a.k.a. folder), and you want to do

HandBrakeCLI.exe  (blah blah blah)  -i ant.MOV -o ant.mp4
HandBrakeCLI.exe       …    …       -i bug.MOV -o bug.mp4
HandBrakeCLI.exe       …    …       -i cat.MOV -o cat.mp4
HandBrakeCLI.exe       …    …       -i dog.MOV -o dog.mp4
                         ︙

for all the *.mov files.  This is fairly straightforward:

for %F in (*.mov) do HandBrakeCLI.exe  (blah blah blah)  -i "%F" -o "%~nF.mp4"

The for %F in (*.mov) runs a loop in which %F takes on every applicable *.mov name.  Then the command following the do is executed for each file, with %F obviously being replaced by the .mov file name.  (Note: the F is case sensitive.)  (You might be able to get by without the quotes — i.e., say just %F, and not "%F" — but then it will fail if you have any filenames with space(s) or other special characters in them.)  %~nF is replaced by the name portion of the filename (not including the extension), so %~nF.mp4 becomes your output filename.

You may want to break this into multiple lines, because your actual command is so long, and/or because you want to do other things in the loop.  You can do this with parentheses:

for %F in (*.mov) do (
    HandBrakeCLI.exe  (blah blah blah)  -i "%F" -o "%~nF.mp4"
    (other command(s) featuring "%F", "%~nF", etc.)
)

Type for /? to see a list of all the ~ codes.  (For example, %~dF gives you the drive letter, and %~pF gives you the path.  The documentation is not clear about the fact that you can combine the modifiers; e.g., %~dpnF gives you the drive, the path, and the name, but not the extension — as if you had typed %~dF%~pF%~nF.)

You can also break long commands into multiple lines with ^:

for %F in (*.mov) do (
    HandBrakeCLI.exe --preset-import-file "preset-name.json" ^
                      -Z "preset-name" -i "%F" -o "%~nF.mp4"
   (other command(s) featuring "%F", "%~nF", etc.)
)

All of the above works at the Command Prompt (command line).  The same thing works in a batch script, except you must replace each % with %%:

for %%F in (*.mov) do (
    HandBrakeCLI.exe --preset-import-file "preset-name.json" ^
                      -Z "preset-name" -i "%%F" -o "%%~nF.mp4"
   (other command(s) featuring "%%F", "%%~nF", etc.)
)

This is tested on Windows 7, but I’d be very surprised if it didn’t work on Windows 10.

Related Question