Skip to content

Instantly share code, notes, and snippets.

@ticean
Created December 10, 2010 04:22
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ticean/735774 to your computer and use it in GitHub Desktop.
Save ticean/735774 to your computer and use it in GitHub Desktop.
sabnzbd/sickbeard post-process script that runs both sabToSickBeard and mkv conversion for XBox360, using handbrake.
#!/bin/bash
############################################################
# Recursively peruses a directory and converts MKV files
# to MP4 for streaming to Xbox360.
#
# @author: Ticean Bennett
# @url: http://ticean.com
# @see: http://trac.handbrake.fr/wiki/CLIGuide#options
#
# @param file The complete file path.
# @param delete_mkv True if the original mkv will be deleted.
#
############################################################
function convert_mkv {
file="$1"
filepath="${file%\/*}"
filename=$(basename "$file")
name=$(echo "$filename" | sed 's/\.[^\.]*$//')
delete_mkv=1 #$2||0;
echo "File: $file"
echo "filepath: $filepath"
echo "filename: $filename"
echo "name: $name"
HandBrakeCLI -i "${filepath}/${name}.mkv" -o "${filepath}/${name}.mp4" -e x264 -b 2000 -a 1 -E faac -B 160 -R 48 -6 dpl2 -f mp4 -x level=40:ref=2:mixed-refs:bframes=3:weightb:subme=9:direct=auto:b-pyramid:me=umh:analyse=all:no-fast-pskip:filter=-2,-1
# @TODO Make sure that conversion was a success before deleting.
if [ -e "${filename}" -a ${deleted}=1 ]; then
echo "Deleting source file: ${filename}"
rm "${filename}"
fi
}
function walk_tree {
local directory="$1"
local extension="$2"
local i
for i in "$directory"/*;
do
echo "Checking ${i}"
if [ "$i" = . -o "$i" = .. ]; then
continue
elif [ -d "$i" ]; then
walk_tree "$i"
elif [ "${i##*.}" = "mkv" ]; then
echo "Found mkv file"
file="$(basename $i)"
filename=$(echo "$i" | sed 's/\.[^\.]*$//')
echo "Invoking convert_mkv ${directory} ${filename}"
#echo "via sed: ${fn}"
convert_mkv "${i}" 0
else
continue
fi
done
}
targetdir="$1"
extension="$2"
echo "Executing: convertMkv.sh ${targetdir} ${extension}"
walk_tree "$targetdir" "mkv"
#!/bin/bash
##########################################################
# sabnzbd script that runs both sabToSickbeard
# and Mkv conversion.
#
# @author: Ticean Bennett
# @see: http://ticean.com
# @see: http://wiki.sabnzbd.org/user-scripts
# @requires: sabToSickbeard.py
# @requires: convertMkv.sh
#
# @param $l directory of the job (full path)
# @param $2 The original name of the NZB file
# @param $3 Clean version of the job name (no path info and ".nzb" removed)
# @param $4 Indexer's report number (if supported)
# @param $5 User-defined category
# @param $6 Group that the NZB was posted in e.g. alt.binaries.x
# @param $7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+21
#
###########################################################
scriptpath="$(cd ${0%/*} && pwd -P)"
"${scriptpath}/convertMkv" "$1" "$2" "$3" "$4" "$5" "$6" "$7"
"${scriptpath}/sabToSickBeard.py" "$1" "$2" "$3" "$4" "$5" "$6" "$7"
@leighchallinor
Copy link

The mkv files remain after conversion, delete_mkv looks like it is set to true.
Where am I going wrong?

@ticean
Copy link
Author

ticean commented Mar 5, 2012

Hmmm.. I haven't used these scripts in quite a while. Converting the mkv's is a really heavy process so I switched to XBMC. It can play the mkv files directly. If you have the means, I'd highly recommend that you do the same.

That being said, I just eyeballed it, and it looks likes there's a typo. The defined var is delete_mkv but the conditional is checking deleted (line 30) so it will always fail. This should fix:

if [ -e "${filename}" -a ${delete_mkv}=1 ]; then

Let me know if that works. ;)

@wilkinsbrian
Copy link

This helped me 2 years later!

@apwelsh
Copy link

apwelsh commented Feb 26, 2016

Have you considered using ffmpeg to repackage the mkv without transcoding? (assuming the objective was just to repackage)

ffmpeg -i "${filepath}/${name}.mkv" -c:v copy -c:a copy "${filepath}/${name}.mp4"

And only calling HandBrake if the file size : width ratio exceeds a certain constant, then transcode with HandBrakeCLI?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment