HandbrakeCLI bash batch script not working.

Posted on

Problem :

I am trying to batch encode a bunch of files. Looking over the internet I have found just a couple scripts and none of them worked for me. I started to piece together something and it is close to working minus one problem

The PRESET variable isn’t working. I even tried removing the variable and put the code in and still didn’t work. I am not sure I am allowed to use this type of command in find. I am sure there is a simpler more elegant way to do this.

#!/bin/bash

SRC="/home/usr/temp/encode"
PRESET="-e x264 -q 20.0 -E faac -B 128 -6 dpl2 -w 1280 --loose-crop --loose-anamorphic --x264-preset veryfast --h264-profile high --h264-level 4.1"    

find $SRC -type f -name '*.mp4' -exec bash -c 'name="{}"; HandBrakeCLI -i "$name" -o "${name}.tmp" $PRESET && rm "$name" && mv "${name}.tmp" "$name"' ;

find $SRC -type f -name '*.wmv' -exec bash -c 'name="{}"; HandBrakeCLI -i "$name" -o "${name%.wmv}.mp4" $PRESET && rm "$name"' ;

find $SRC -type f -name '*.mov' -exec bash -c 'name="{}"; HandBrakeCLI -i "$name" -o "${name%.mov}.mp4" $PRESET && rm "$name"' ;

exit

If I was to do this file by file from the command line it would look like this

HandBrakeCLI -i "filename.mov" -o "filename.mp4" -e x264 -q 20.0 -E faac -B 128 -6 dpl2 -w 1280 --loose-crop --loose-anamorphic --x264-preset veryfast --h264-profile high --h264-level 4.1

If I was to resize an mp4 file I would do this so I wouldn’t overwrite the file as it is encoding. That is why I have the extra stuff on the first find.

HandBrakeCLI -i "filename.mp4" -o "filename.NEW.mp4" -e x264 -q 20.0 -E faac -B 128 -6 dpl2 -w 1280 --loose-crop --loose-anamorphic --x264-preset veryfast --h264-profile high --h264-level 4.1

Solution :

Variable substitution doesn’t work in single quotes. Use double quotes and escape-character to put double quotes inside double quotes.

find $SRC -type f -name '*.mp4' -exec bash -c "name="{}""

HandBrakeCLI -i ""$name"" -o ""${name}.tmp"" $PRESET &

&

rm ""$name"" &

Leave a Reply

Your email address will not be published. Required fields are marked *