I have compiled now a script to batch-convert all my media to Airplay suitable format in a comfy script which also handles difficulties with dropped network shares. In my case I run the handbrake conversion on a Mac Mini and all the files reside on my Synology. Just wanted to share this as I think might be useful to others too.
The script handles:
- files in current directory (and subs)
- files in a fully qualified (local) path
- files in a subdir (and subs of this) of a pre-defined network share
Options:
- delete or rename original files
- set Handbrake conversion profile
The conversion is designed to replace the old file. At start you can opt to either actually delete or move to a ".old" extension the original file. For security, both the handbrake result "Conversion done" and the resulting filesize of the the converted file vs. original file is checked for validity. So, even if handbrake says it's done correctly, an original file won't be deleted if the converted file appears to be invalid because it's less then 40% smaller than the original file (or whatever threshold you set).
All variables that might need adjustment (like network share names, passwords etc) are setup at the beginning of the script for easy maintenance.
The actual conversion script is started by an an additional starter script which will then show the progress of the logfile. The batch conversion itself runs in background and won't stop if you exit the terminal session.
Here's the starter script "convert.sh":
Code: Select all
#!/bin/bash
###################################################
### convert downloaded avi/mkv/flv to mp4 files ###
###---------------------------------------------###
### pre: bash ###
### out: $1 = Scan folder in afp share location ###
### $2 = delete switch ###
### $3 = Handbrake preset ###
###---------------------------------------------###
### Auth: Kerry Kerschbaumer; kerry@kerry.de ###
### Vers: 1.0, Oct 5, 2013 ###
### www.kerry.de ###
###################################################
MCS="$HOME/Dev/mediaconvert.sh"
LOG="$HOME/Dev/MediaConv.log"
log() {
echo `date "+%Y-%m-%d %H:%M:%S"` "$1" >> $LOG
}
clear
echo "############################################"
echo "### CONVERT media to mp4 ###"
echo "############################################"
echo " "
myPath=""
read -p "Scan folder (blank for current OR subfolder name for standard server share OR fully qualified folder path): " myPath
if [ "$myPath" == "" ]
then
myPath="."
fi
CSW=""
while [ "$CSW" != "y" -a "$CSW" != "n" ]
do
read -p "Delete old file after conversion (y/n)? " CSW
done
PRE=""
read -p "Handbrake Preset (blank for Normal): " PRE
if [ "$PRE" == "" ]
then
PRE="Normal"
fi
nohup $MCS "$myPath" $CSW $PRE 2>>$LOG &
tail -f $LOG
exit
And here the actual conversion script "mediaconvert.sh"
Code: Select all
#!/bin/bash
###################################################
### convert downloaded avi/mkv/flv to mp4 files ###
###---------------------------------------------###
### pre: convert (bash) ###
### in : $1 = Scan folder in afp share location ###
### $2 = delete switch ###
### $3 = Handbrake preset ###
###---------------------------------------------###
### Auth: Kerry Kerschbaumer; kerry@kerry.de ###
### Vers: 1.0, Oct 5, 2013 ###
### www.kerry.de ###
###################################################
##### Settings and vars #########
AFPuser="[YourUserName]"
AFPpass="[YourPassword]"
AFPsrvr="[YourServer]" ### e.g. 192.168.0.2
AFPfldr="[YourShare]" ### e.g. Movies
if [ "$1" == "." -o "${1:0:1}" == "/" ]
then ### current dir or fully qualified folder
TDIR=$1
else ### subdir to AFPserver share
TDIR="/Volumes/$AFPfldr/$1"
fi
DSW=$2
PRE=$3
LOG="$HOME/Dev/MediaConv.log"
CLI="/usr/bin/HandBrakeCLI"
##### Function definitions #########
log() {
echo `date "+%Y-%m-%d %H:%M:%S"` "$1" >> $LOG
}
##### Main function #########
log "######### BEGIN of new Conversion Scan ################"
log "Dir = $TDIR"
log "Del = $DSW"
log "Pre = $PRE"
log "-------------------------------------------------------"
### Check for running instance
while [ `ps -ef | grep HandBrakeCLI | grep -v grep | wc -l` -gt 0 ]; do
log "Already running... wait...."
sleep 30
done
### Check availability of afp mount
if [ ! -d "$TDIR" ]; then
log "Mounting server folder..."
mkdir "/Volumes/$AFPfldr"
mount_afp "afp://$AFPuser:$AFPpass@$AFPsrvr/$AFPfldr/" "/Volumes/$AFPfldr"
else
log "OK: Server folder available"
fi
### Check availability of scan folder
if [ -d "$TDIR" ]
then
cd "$TDIR"
### Log files to be converted
log "Files found = $(find "$TDIR" -type f \( -name "*.avi" -o -name "*.flv" -o -name "*.wmv" -o -name "*.mkv" \) -print)"
### Main loop for file conversion
while IFS= read -d "" -r INFILE
do
### check if network mount is still available
if [ ! -d "$TDIR" ]; then
log "Mounting server folder..."
mkdir "/Volumes/$AFPfldr"
mount_afp "afp://$AFPuser:$AFPpass@$AFPsrvr/$AFPfldr/" "/Volumes/$AFPfldr"
fi
### generate target filename
OUTFILE="${INFILE%.*}.mp4"
log "$INFILE >> $OUTFILE"
### Conversion execute (echo "" required to stay in loop after finish of CLI)
echo "" | $CLI -i "$INFILE" -o "$OUTFILE" --preset=$PRE 2>&1 | grep "Encode done"
### Check validity of conversion before deleting/moving
if [ "$?" == "0" ]; then
log "OK: Encode done!"
INsize=$(du "$INFILE" | cut -f1)
OUTsize=$(du "$OUTFILE" | cut -f1)
myRatio=$(echo "scale=2;(($OUTsize - $INsize) / $INsize * 100)" | bc | cut -d. -f1)
log "Ratio = $myRatio % of old file size"
if [ $myRatio -lt -40 ]; then #### outfile more than 40% smaller than infile
log "ERROR: Output file does not appear valid: $myRatio % smaller!"
elif [ "$DSW" == "y" ]; then
log "Delete $INFILE"
rm "$INFILE"
else
log "Rename $INFILE to .old"
mv "$INFILE" "$INFILE.old"
fi
else
log "ERROR: Handbrake exited without finishing encoding!"
fi
done< <(find . -type f \( -name "*.avi" -o -name "*.flv" -o -name "*.wmv" -o -name "*.mkv" \) -print0)
else ## no scan folder
log "ERROR: scan folder not available!!"
fi
log "######### END of Conversion Scan ################"
exit