Batch video conversion shell script, how to add existing file check?

Hi guys!

I came accross a really useful script here (http://derrekyoung.com/2012/01/08/script-for-recursive-batch-encode-on-mac-os-x-linux-too/) which searches a volume for files with specific extension, and starts a HandBrakeCLI job to transcode it.

I need the script to check if there is already a result file with the in_file name but with the output_file_type. If it exists, skip to the next check.
Because at the moment, every time you run the script it just goes through and over-writes everything it did previously.

I’m new to apple scripting/shell scripts, but with my limited knowledge it seems like a couple of lines in the ‘for’ loop should do the trick, no?

Thank you for any help or steer in the right direction!

This is the code:



#!/bin/sh

###############################################################################
#
# Script to recursively search a directory and batch convert all files of a given
# file type into another file type via HandBrake conversion.
#
# To run in your environment set the variables:
#   hbcli - Path to your HandBrakeCLI
#	
#   dirs - Array of starting directories for recursive search
#	
#   input_file_type - Input file type to search for
#	
#   output_file_type  - Output file type to convert into
#
#
# Change log:
# 2012-01-08: Initial release.  Tested on Mac OS X Lion.
# 2012-02-09: Added ability to process multiple directories.
# 2012-02-12: Added default system beep to signal end of all processing. 
#
###############################################################################

hbcli=/Applications/HandBrakeCLI
input_file_type="mov"
output_file_type="mp4"

dirs=( '/Volumes/CS 2TB 8' )

echo "# Using HandBrakeCLI at "$hbcli
echo "# Converting "$input_file_type" to "$output_file_type

# Convert from one file to another
convert() {
	# The beginning part, echo "" | , is really important.  Without that, HandBrake exits the while loop.
	echo ""# | $hbcli -i "$1" -o "$2" -e x264 --x264-preset slow -q 22 -B 128;
}

# Iterate over the array of directories
for i in "${dirs[@]}"
do
  echo "Processing source directory: " $i

  # Find the files and pipe the results into the read command because the read command properly handles spaces in directories and files names.
  find "$i" -name *.$input_file_type | while read in_file
  do
        echo "Processing file..."
	echo ">Input  "$in_file

	# Replace the file type
	out_file=$(echo $in_file|sed "s/\(.*\.\)$input_file_type/\1$output_file_type/g")
	echo ">Output "$out_file

	# Convert the file
	convert "$in_file" "$out_file"

	if [ $? != 0 ]
        then
            echo "$in_file had problems" >> handbrake-errors.log  
        fi

	echo ">Finished "$out_file "\n\n"
  done
done

echo "DONE CONVERTING FILES"
echo -en "\007"
echo -en "\007"
echo -en "\007"


Hello.

I have made a little block of code for you to paste into a copy of the script, and check to see that it works properly. (It really should, but you should test it anyway!)

[code]# …

Replace the file type

out_file=$(echo $in_file|sed “s/(.*.)$input_file_type/\1$output_file_type/g”)

Modifications here …

if [ ! -f $(out_file) ] ; then
echo ">Output "$out_file

Convert the file

convert “$in_file” “$out_file”
else
echo >/dev/null
fi

until here

if [ $? != 0 ]
then
echo “$in_file had problems” >> handbrake-errors.log
fi

…[/code]

Edit:

I have gone back and make the if test more robust to spaces in filenames.

Thank you for your quick reply!

Unfortunately it seems to be skipping through them with this result:

"
Processing file…

Input /Volumes/CS 2TB 8/SS ACCOMODATION SECTOR/Appartment project/SIGN OFF OUTPUTS/Appartments4.mov
/Users/elliottcranmer/Documents/hbrake.sh: line 55: [: too many arguments
Finished /Volumes/CS 2TB 8/SS ACCOMODATION SECTOR/Appartment project/SIGN OFF OUTPUTS/Appartments4.mp4
"

this is the code after I pasted in your new section:


#!/bin/sh

###############################################################################
#
# Script to recursively search a directory and batch convert all files of a given
# file type into another file type via HandBrake conversion.
#
# To run in your environment set the variables:
#   hbcli - Path to your HandBrakeCLI
#	
#   dirs - Array of starting directories for recursive search
#	
#   input_file_type - Input file type to search for
#	
#   output_file_type  - Output file type to convert into
#
#
# Change log:
# 2012-01-08: Initial release.  Tested on Mac OS X Lion.
# 2012-02-09: Added ability to process multiple directories.
# 2012-02-12: Added default system beep to signal end of all processing. 
#
###############################################################################

hbcli=/Applications/HandBrakeCLI
input_file_type="mov"
output_file_type="mp4"

dirs=( '/Volumes/CS 2TB 8' )

echo "# Using HandBrakeCLI at "$hbcli
echo "# Converting "$input_file_type" to "$output_file_type

# Convert from one file to another
convert() {
	# The beginning part, echo "" | , is really important.  Without that, HandBrake exits the while loop.
	echo ""# | $hbcli -i "$1" -o "$2" -e x264 --x264-preset slow -q 22 -B 128;
}

# Iterate over the array of directories
for i in "${dirs[@]}"
do
  echo "Processing source directory: " $i

  # Find the files and pipe the results into the read command because the read command properly handles spaces in directories and files names.
  find "$i" -name *.$input_file_type | while read in_file
  do
        echo "Processing file..."
	echo ">Input  "$in_file

	# Replace the file type
	out_file=$(echo $in_file|sed "s/\(.*\.\)$input_file_type/\1$output_file_type/g")

	# Modifications here ...
	if [ ! -f $out_file ] ; then 
	echo ">Output "$out_file

 	# Convert the file
	convert "$in_file" "$out_file"
fi
# until here
 if [ $? != 0 ]
then
echo "$in_file had problems" >> handbrake-errors.log 
fi

echo ">Finished "$out_file "\n\n"
done
done

echo "DONE CONVERTING FILES"
echo -en "\007"
echo -en "\007"
echo -en "\007"

Hello!

I edited the code in post #2 to take care of that, and I have edited it yet again, so the if test won’t be the last test that are seen for the test further down the script, that checks that there were no errors.

Please try with the new code block. :slight_smile:

Thanks, but now I’m getting this “command not found” on line 55

"

Input /Volumes/CS 2TB 8/SS FINANCE SECTOR/WEBSITE PLACEHOLDER TEMPLATE 1.mov
/Users/elliottcranmer/Documents/hbrake.sh: line 55: out_file: command not found
Finished /Volumes/CS 2TB 8/SS FINANCE SECTOR/WEBSITE PLACEHOLDER TEMPLATE 1.mp4
"

any ideas? thanks for your help!

Hello.

Try

if [ ! -f "$out_file" ] ; then

instead of

if [ ! -f $(out_file) ] ; then

IT works!! I think the quotes did it!.

I’ll give it more testing and report, but for now - thank you kind sir!! :slight_smile:

I am glad that it works for you, and you are welcome. :slight_smile: