Script Porting - Help translate scripts for another platform

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
w33d5
Posts: 6
Joined: Wed Dec 01, 2010 2:03 am

Script Porting - Help translate scripts for another platform

Post by w33d5 »

I've seen a lot of interesting scripts on the boards and would love to try them out but the one problem with shell scripts is that they are platform specific. I was hoping use this thread for everyone to get help getting in having scripts translated/ported from one platform/language to another.
w33d5
Posts: 6
Joined: Wed Dec 01, 2010 2:03 am

Re: Script Porting - Help translate scripts for another plat

Post by w33d5 »

Can someone help in porting this windows batch to a mac shell script?

Code: Select all

@ECHO off

SET WorkingPath=%CD%
SET FileExt=*.mov   ::PICK YOUR POISON!
SET PathToHandbrake=C:\Program Files (x86)\Handbrake\HandBrakeCLI.exe
SET HandbrakeArgs=-f mp4 -X 624 --loose-anamorphic  -e x264 -q 20 -a 1,1 -E faac,ac3 -6 dpl2,auto -R 48,Auto -B 160,auto -D 0.0,0.0 -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subq=6:weightb=0 -v 1

FOR /R "%WorkingPath%" %%a IN (%FileExt%) DO (  
   IF EXIST %%a ( 
      REN "%%~dpa%%~nxa" "%%~nxa.processing"  ::TAKE FILE OUT OF QUEUE
      "%PathToHandbrake%" -i "%%~dpa%%~nxa.processing" -o "%%~dpa%%~na.m4v" %HandbrakeArgs%  ::ENCODE FILE
      REN "%%~dpa%%~nxa.processing" "%%~nxa.delete" ::RENAME FILE FOR LATER DELETION
      )
   )

PAUSE
mcmusic
Experienced
Posts: 97
Joined: Thu Sep 30, 2010 3:28 pm

Re: Script Porting - Help translate scripts for another plat

Post by mcmusic »

here is a similar script in bash - testing in Linux but untested on a Mac, should run with no real issues. At the very least its a start to get you on the right track. I have added comments, echos and loggings which you can use to adopt it for your own use.

Code: Select all

#!/bin/bash
# usage is ./handbrakeencode.sh /path/to/folder
# written by mcmusic http://videoscripts.wordpress.com
#  if you use this as a 'files newer than' script then add it to crontab with:
# echo "0 0 * * 6 root /path/to/thisfile/handbrakeencode.sh /folder/withfiles" >> /etc/crontab
# to run every saturday at midnight

# type in your path to handbrakecli
hbpath=/path/to/handbrake
# key in your file extension for files to process
ext=avi
#key in your target file extension
outext=m4v
# key in your query
query="-f mp4 -X 624 --loose-anamorphic  -e x264 -q 20 -a 1,1 -E faac,ac3 -6 dpl2,auto -R 48,Auto -B 160,auto -D 0.0,0.0 -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subq=6:weightb=0 -v 1"


# clean up incoming naming
O=$IFS
IFS=$(echo -en "\n\b")
# create some time vars for logging
DATE=$(date)

###########logging
# log it part 11 - this are commented out, uncomment if you want logging, change the paths to suit your own use
#echo "starting batch process at $DATE" >> /root/uw/ipodlog.txt
#rm -f /root/uw/encodecomplete
#touch /root/uw/encodein.progress
#echo "Starting encoding" | /bin/mail -s "Encoding Started" someemail@someemail.com -- -r "fromemail@fromemail.com"
# create a log of files to be done this is based on using the script in its 'newer than mode'
#for i in $(find $@ -mtime -6 -name '*.avi') ; do
#echo "$i" >> /root/uw/ipodlog.txt
#done
##########logging

# do the processing
# this one for all files in folder
for i in $(find $1 -name "*.$ext") ; do
# uncomment this one to process only files in last seven days last seven days
# for i in $(find $@ -mtime -6 -name '*.avi') ; do
#path="$(dirname $i)"

# split up the incoming name into file and source folder

path=`dirname "$i"`
filename=`basename "$i" .$ext`


#####useful echo statments for testing comment them in if you don't need them or simply delete
echo filename now is $filename
echo directory will be "$path"
echo output file will be "$path/$filename.$outext"
# log it 2
echo "now going to encode $filename.$outext" >> /root/uw/ipodlog.txt
echo Now I can process my encode
########################################

# now run the file renaming for processing file
chmod 777 $i
mv $i $path/$filename.processing

# now run the encode

$hbpath/HandBrakeCLI -i "$path/$filename.processing" -o "$path/$filename.$outext" "$query"

# this echo just in for testing
#echo $hbpath/HandBrakeCLI -i "$path/$filename.processing" -o "$path/$filename.$outext" "$query"

# now take the processing file and rename it as a delete file for later removal
chmod 777 $path/$filename.processing
mv $path/$filename.processing $path/$filename.delete

############more logging should you need it
# log it 3
#echo "Finished encoding $filename.$outext on $DATE" >> /root/uw/ipodlog.txt

# next file
done
w33d5
Posts: 6
Joined: Wed Dec 01, 2010 2:03 am

Re: Script Porting - Help translate scripts for another plat

Post by w33d5 »

thanks this is great
mcmusic wrote:here is a similar script in bash - testing in Linux but untested on a Mac, should run with no real issues. At the very least its a start to get you on the right track. I have added comments, echos and loggings which you can use to adopt it for your own use.

Code: Select all

#!/bin/bash
# usage is ./handbrakeencode.sh /path/to/folder
# written by mcmusic http://videoscripts.wordpress.com
#  if you use this as a 'files newer than' script then add it to crontab with:
# echo "0 0 * * 6 root /path/to/thisfile/handbrakeencode.sh /folder/withfiles" >> /etc/crontab
# to run every saturday at midnight

# type in your path to handbrakecli
hbpath=/path/to/handbrake
# key in your file extension for files to process
ext=avi
#key in your target file extension
outext=m4v
# key in your query
query="-f mp4 -X 624 --loose-anamorphic  -e x264 -q 20 -a 1,1 -E faac,ac3 -6 dpl2,auto -R 48,Auto -B 160,auto -D 0.0,0.0 -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subq=6:weightb=0 -v 1"


# clean up incoming naming
O=$IFS
IFS=$(echo -en "\n\b")
# create some time vars for logging
DATE=$(date)

###########logging
# log it part 11 - this are commented out, uncomment if you want logging, change the paths to suit your own use
#echo "starting batch process at $DATE" >> /root/uw/ipodlog.txt
#rm -f /root/uw/encodecomplete
#touch /root/uw/encodein.progress
#echo "Starting encoding" | /bin/mail -s "Encoding Started" someemail@someemail.com -- -r "fromemail@fromemail.com"
# create a log of files to be done this is based on using the script in its 'newer than mode'
#for i in $(find $@ -mtime -6 -name '*.avi') ; do
#echo "$i" >> /root/uw/ipodlog.txt
#done
##########logging

# do the processing
# this one for all files in folder
for i in $(find $1 -name "*.$ext") ; do
# uncomment this one to process only files in last seven days last seven days
# for i in $(find $@ -mtime -6 -name '*.avi') ; do
#path="$(dirname $i)"

# split up the incoming name into file and source folder

path=`dirname "$i"`
filename=`basename "$i" .$ext`


#####useful echo statments for testing comment them in if you don't need them or simply delete
echo filename now is $filename
echo directory will be "$path"
echo output file will be "$path/$filename.$outext"
# log it 2
echo "now going to encode $filename.$outext" >> /root/uw/ipodlog.txt
echo Now I can process my encode
########################################

# now run the file renaming for processing file
chmod 777 $i
mv $i $path/$filename.processing

# now run the encode

$hbpath/HandBrakeCLI -i "$path/$filename.processing" -o "$path/$filename.$outext" "$query"

# this echo just in for testing
#echo $hbpath/HandBrakeCLI -i "$path/$filename.processing" -o "$path/$filename.$outext" "$query"

# now take the processing file and rename it as a delete file for later removal
chmod 777 $path/$filename.processing
mv $path/$filename.processing $path/$filename.delete

############more logging should you need it
# log it 3
#echo "Finished encoding $filename.$outext on $DATE" >> /root/uw/ipodlog.txt

# next file
done
Dalton63841
Posts: 20
Joined: Tue Jan 11, 2011 9:54 pm

Re: Script Porting - Help translate scripts for another plat

Post by Dalton63841 »

You did an excellent job of porting this script, and I give you props!

However, I did notice one flaw. Currently I have 2 Windows PC's and 2 Mac's encoding vids on a network drive. At first I thought your script was working perfectly, but upon further investigation I saw that the Mac's on several occasion would be encoding the exact same ".processing" file.

It turns out that it was a simple fix. With your script, if the file was already out of queue, it would skip the "mv" command, and move on to encode the ".processing" file, instead of skipping to the next file.

After some trial and error, I have the script working perfectly:

Code: Select all

#!/bin/bash
# usage is ./handbrakeencode.sh /path/to/folder
# written by mcmusic http://videoscripts.wordpress.com
#  if you use this as a 'files newer than' script then add it to crontab with:
# echo "0 0 * * 6 root /path/to/thisfile/handbrakeencode.sh /folder/withfiles" >> /etc/crontab
# to run every saturday at midnight

# type in your path to handbrakecli
hbpath=/path/to/handbrake
# key in your file extension for files to process
ext=avi
#key in your target file extension
outext=m4v
# key in your query
query="-f mp4 -X 624 --loose-anamorphic  -e x264 -q 20 -a 1,1 -E faac,ac3 -6 dpl2,auto -R 48,Auto -B 160,auto -D 0.0,0.0 -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subq=6:weightb=0 -v 1"


# clean up incoming naming
O=$IFS
IFS=$(echo -en "\n\b")
# create some time vars for logging
DATE=$(date)

###########logging
# log it part 11 - this are commented out, uncomment if you want logging, change the paths to suit your own use
#echo "starting batch process at $DATE" >> /root/uw/ipodlog.txt
#rm -f /root/uw/encodecomplete
#touch /root/uw/encodein.progress
#echo "Starting encoding" | /bin/mail -s "Encoding Started" someemail@someemail.com -- -r "fromemail@fromemail.com"
# create a log of files to be done this is based on using the script in its 'newer than mode'
#for i in $(find $@ -mtime -6 -name '*.avi') ; do
#echo "$i" >> /root/uw/ipodlog.txt
#done
##########logging

# do the processing
# this one for all files in folder
for i in $(find $1 -name "*.$ext") ; do
# uncomment this one to process only files in last seven days last seven days
# for i in $(find $@ -mtime -6 -name '*.avi') ; do
#path="$(dirname $i)"

# this makes sure the file hasn't been taken out of the queue
if [ -f "$i" ]; then

# split up the incoming name into file and source folder

path=`dirname "$i"`
filename=`basename "$i" .$ext`


#####useful echo statments for testing comment them in if you don't need them or simply delete
echo filename now is $filename
echo directory will be "$path"
echo output file will be "$path/$filename.$outext"
# log it 2
echo "now going to encode $filename.$outext" >> /root/uw/ipodlog.txt
echo Now I can process my encode
########################################

# now run the file renaming for processing file & encode
# This forces HandbrakeCLI to begin encoding the file it just renamed.
chmod 777 $i
mv $i $path/$filename.processing && $hbpath/HandBrakeCLI -i "$path/$filename.processing" -o "$path/$filename.$outext" "$query"

# this echo just in for testing
#echo $hbpath/HandBrakeCLI -i "$path/$filename.processing" -o "$path/$filename.$outext" "$query"

# now take the processing file and rename it as a delete file for later removal
chmod 777 $path/$filename.processing
mv $path/$filename.processing $path/$filename.delete

else

# This is for files that it looks for, but have been taken out of the queue
echo "File is no longer in Queue, Moving on..."

fi

############more logging should you need it
# log it 3
#echo "Finished encoding $filename.$outext on $DATE" >> /root/uw/ipodlog.txt

# next file
done
You will notice the if statement, that ensures the file is still in the queue, and also the && that forces Handbrake to encode the file it just renamed, instead of encoding a file just because it has the right name.
mcmusic
Experienced
Posts: 97
Joined: Thu Sep 30, 2010 3:28 pm

Re: Script Porting - Help translate scripts for another plat

Post by mcmusic »

Nice catch! Cheers
Dalton63841
Posts: 20
Joined: Tue Jan 11, 2011 9:54 pm

Re: Script Porting - Help translate scripts for another plat

Post by Dalton63841 »

EDIT: NVM
ggeremy
Posts: 2
Joined: Thu May 05, 2011 8:11 am

Re: Script Porting - Help translate scripts for another plat

Post by ggeremy »

Hello,

I have a question about the script.
What do the following lines do ?

Code: Select all

O=$IFS
IFS=$(echo -en "\n\b")
What is the IFS variable ?
Thank you

Geremy
ggeremy
Posts: 2
Joined: Thu May 05, 2011 8:11 am

Re: Script Porting - Help translate scripts for another plat

Post by ggeremy »

I've found an answer on google !
If you try to process a for loop on file name with spaces in them you are going to have some problem. For loop uses $IFS variable to determine what the field separators are. By default $IFS is set to the space character.
Post Reply