3

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.

JK81
  • 31
  • (1) From the fact that you say “batch” and .exe, I guess that you’re talking about Windows, but it’s good to say so explicitly. (2) Also, be more explicit about what you need. You show one command. What do you need to batch?    Please do not respond in comments; [edit] your question to be clearer and more complete. – Scott - Слава Україні Oct 25 '17 at 01:47
  • Updated.... Bumping – JK81 Oct 25 '17 at 23:01
  • Just checking in..... – JK81 Nov 02 '17 at 17:15

3 Answers3

2

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.

0

I would like to add a little to this in the event that you are converting the same file type from a raw mkv file (MakeMKV rip) to a compressed mkv file. The code above is great for looking at one file, then converting to another. Other code I found was similar and am copying it from their site:

FOR /F "tokens=*" %%G IN ('DIR /B /S *.avi') DO "C:\Program Files\Handbrake\HandBrakeCLI" -i "%%G" -o "%%G".mp4 --preset="iPad"

However this didn't help when converting .mkv --> .mkv. I did find through trial and error that you need to remove the /S in the directory command so that it only records the filename and not the whole path to %%G.

Here is the code that I am using to pull a user profile and also send the encode to a different folder:

FOR /F "tokens=*" %%G IN ('DIR /B *.mkv') DO "C:\Program Files\Handbrake\HandBrakeCLI" --preset-import-gui -Z "name of user preset" -i "%%G" -o "D:\Users\jeff\Videos\Path\To\File\%%G"
pause

Things to note:

  1. Name of the Preset is Case Sensitive whether it's yours or a built in one.
  2. If using a built in preset, you do not need to have --preset-import-gui in the code
  3. Make sure the variable has quotes after the -i like so -i "%%G". This will, as a comenter stated above, allow for files with spaces in the name to be used without error.

Copy the code that you need into a text file and then save the file as a .bat file in the directory of the movies you want to convert.

I hope this helps you save time when converting lots of files at once with different names.

  • FOR /F "tokens=" %G IN ('DIR Camera/.mp4') DO "C:\Program Files\HandBrake\CLI\HandBrakeCLI.exe" -Z "Very Fast 720p30" -i "%G" -o "%G" how to make it work in all subfolders that have folder Camera and all mp4 files in all Camera folders? – Kangarooo Oct 20 '23 at 21:59
0

All Subfolders export in folder where you run the programm

FOR /R %G IN (*.mp4) DO "C:\Program Files\HandBrake\CLI\HandBrakeCLI" -Z "Very Fast 720p30" -i "%~G" -o "%~nG-resized.mp4"

All Subfolders in each find mp4 and export in same folder with -resized

FOR /R %G IN (*.mp4) DO (
    "C:\Program Files\HandBrake\CLI\HandBrakeCLI" -Z "Very Fast 720p30" -i "%~G" -o "%~dpnG-resized.mp4"
)

All Subfolders in each same folder only if in Camera folder

for /r /d %i in (Camera) do (
    for %j in ("%i\*.mp4") do (
        "C:\Program Files\HandBrake\CLI\HandBrakeCLI" -Z "Very Fast 720p30" -i "%~j" -o "%~dpnj-resized.mp4"
    )
)

All Subfolders in each same folder only if in Camera folder skip -resized

for /r /d %i in (Camera) do (
    for %j in ("%i\*.mp4") do (
        echo "%~nj" | find /i "-resized" > nul || (
            "C:\Program Files\HandBrake\CLI\HandBrakeCLI" -Z "Very Fast 720p30" -i "%~j" -o "%~dpnj-resized.mp4"
        )
    )
)
Kangarooo
  • 161