Search

Sunday, March 13, 2016

Poor Man’s Batch Encoding with HandBrake

I wanted to have a way to batch encode a directory (or even multiple directories) full of files without having to use the HandBrake interface to queue each directory. Ideally, I will do this in .NET in the long run but for now, here is a simple way to walk through all the directories and encode all files with the same HandBrake settings.

How it Works

Deletes any existing files with the names “_encode_info.log” and “_encode_progress.log”

Gets all the files in the directory and sub directories where it runs, ignoring itself

For each file it finds:

it gets the name of the directory the file is in, creates a directory with the same name under the “destinationPathRoot.”

Sets the name of the output file to be under the newly created directory under “destinationPathRoot”

If the file already exists, it sets it’s name to have the current date and time (to avoid overwriting)

Calls the EncodeVideo function with the source file path and destination file path

If the destination file path exists after the encoding, it deletes the source file path

Setup

handBrakeExecutable – Set the path to the HandBrakeCLI executable

destinationPathRoot – The root of the path where to put the encoded files

How to Use It

Copy the contents below and paste it in a .bat file. I name mine according to the HandBrake arguments I’m using. For example “_encode-Q25-1Audio,bat” (meaning using variable quality of 25 with 1 audio stream). Copy the .bat file in the root of the directory where your videos reside. Run it.

Code

@echo off
cls
setlocal enabledelayedexpansion enableextensions
rem Set the path to the Handbrake executable
set handBrakeExecutable=Path\To\HandBrakeCLI.exe
rem Set the root path where files will be encoded to
set destinationPathRoot=Path\To\Done\Videos
if exist "%CD%\_encode_info.log" del "%CD%\_encode_info.log"
if exist "%CD%\_encode_progress.log" del "%CD%\_encode_progress.log"
rem Get all the files in the directory and sub-directories
for /f "tokens=* delims=|" %%G in ('dir /s /a-d /b *.*') do if not %%~xG==.bat (
rem Set the source file path
set sourceFilePath=%%G
rem Get the current directory and remove the trialing slash
set currentDirectory=%%~dpG
set currentDirectoryNoTrailingSlash=!currentDirectory:~0,-1!
rem Get only the name of the current directory
for %%f in (!currentDirectoryNoTrailingSlash!) do set currentDirectoryName=%%~nxf
rem Create the path to the destantion path based on the current directory name
set destinationPath=!destinationPathRoot!\!currentDirectoryName!
rem if the destination doesn't exist, create it
if not exist !destinationPath! (
mkdir !destinationPath!
)
rem Process the file
echo File: %%G
rem Set the destination file based on the destination path and the current file name
set destinationFilePath=!destinationPath!\%%~nG.mp4
rem If the filename already exists, append the current date and time to it
if exist destinationFilePath (
rem Get the current date and time in MMDDYY_HH_MM_SS format
for /f "tokens=1-8 delims=:./ " %%G in ("%date%_%time%") do (
set currentDateTime=%%G%%H%%I_%%J_%%K
)
set destinationFilePath=!destinationPath!\%%~nG-!currentDateTime!.mp4
)
rem Call Handbrake to encode the file
call :EncodeFile "!sourceFilePath!", "!destinationFilePath!"
rem Delete the source file if the destination exists
if exist !destinationFilePath! (
del "!sourceFilePath!"
)
)
:EncodeFile
setlocal
rem echo Source: %~1
rem echo Destination: %~2
rem Call HandBrakeCLI to encode the file
"!handBrakeExecutable!" ^
-I "%~1" ^
-o "%~2" ^
-f mp4 ^
-O ^
--decomb="fast" ^
--crop 0:0:0:0 ^
--strict-anamorphic ^
--modulus 2 ^
-e x264 ^
-q 25.0 ^
--cfr ^
-a 1 ^
--encoder-level="4.0" ^
--encoder-profile=high ^
1> _encode_progress.log ^
2> _encode_info.log
endlocal