HandbrakeCLI bash batch script not working.

2

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

J4204144

Posted 2014-05-05T04:22:23.907

Reputation: 144

One issue: PRESET appears in single quotes. That means that it won't be evaluated until the bash -c command is executed. You need to make PRESET to that subshell. Therefore: PRESET=... ==> export PRESET=... – John1024 – 2014-05-05T04:38:27.137

@John1024 That is beyond my scope. I am very new to bash scripting. – J4204144 – 2014-05-05T05:47:23.200

Answers

1

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\" && mv \"${name}.tmp\" \"$name\"" \;

This applies to other lines as well.

Updated: Ah sorry. I misread your question.

Here the revised script. Instead using exec, I use while-do to looping

#!/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' | while read name; do HandBrakeCLI -i "${name}" -o "${name}.tmp" ${PRESET} && rm "${name}" && mv "${name}.tmp" "${name}"; done

find $SRC -type f -name '*.wmv' | while read name; do HandBrakeCLI -i "${name}" -o "${name%.wmv}.mp4" ${PRESET} && rm "${name}"; done

find $SRC -type f -name '*.mov' | while read name; do HandBrakeCLI -i "${name}" -o "${name%.mov}.mp4" ${PRESET} && rm "${name}"; done

exit

masegaloeh

Posted 2014-05-05T04:22:23.907

Reputation: 877

I am getting Missing input device. Run HandBrakeCLI --help for syntax. – J4204144 – 2014-05-05T05:46:24.480

Ah, I misread your question. Please look my updated answer. – masegaloeh – 2014-05-05T06:32:15.597

That worked perfectly. Huge thanks for helping me. – J4204144 – 2014-05-05T07:40:21.473