Tweaking and HD

So after successfully automating my perfect TV setup with minimal human interaction, I decided to take the next logical step and prepare for more HD content, seeing as how Lost is back, and I’m on the late shift so I always have to watch it after it has aired on cable. I cobbled together a script from a number of different scripts I had for dealing with various media files, and I also wrote a little while loop to prevent my server from having too many instances of ffmpeg running at once. Here is the script, below:

### Transcode 720p H264 mkv files for the Apple TV

if [ -z $1 ]; then
echo “Usage: `basename $0` filename”
exit 0
fi

# Wait while any other ffmpeg processes are running
while [ -n “$(ps -ef | grep ffmpeg | grep -v grep)” ];
do
echo -e “\n[$(date +%b\ %d\ %Y:\ %H:%M:%S)]\nffmpeg is currently working…”
sleep 300
done

# Get the beginning time from the date cmd.
START=$(date +%D\ %T)

# Email us that the next process has begun
echo -e “About to start transcoding $1 at $START” > /tmp/emailmessage.txt
/usr/local/bin/email.sh

# ffmpeg cmd
ffmpeg -y -i $1 -threads 2 -s 1280×720 -aspect 1280:720 -r ntsc-film -vcodec libx264 -g 150 -qmin 8 -b 4500k -level 31 -loop 1 -sc_threshold 40 -partp4x4 1 -rc_eq ‘blurCplx^(1-qComp)’ -refs 2 -qmax 51 -maxrate 4500k -bf 1 -keyint_min 40 -async 1 -acodec libfaac -ar 48000 -ac 2 -ab 384k $1.mp4

# Get the ending time of the transcode process from the date cmd.
END=$(date +%D\ %T)

# Inform us with an email
echo “Transcoding of $1 was started on $START and completed on $END.” > /tmp/emailmessage.txt
/usr/local/bin/email.sh
mv -v $1 /mkvs

HDstorageDir=/storage/HDTV

## Look for HD files and mv -v them over to the NFS Share on lunchbox
## Look for the shows we watch, and move files to the right place.

TranscodedFile=$1.mp4

if [[ “$TranscodedFile” =~ lost* ]] || [[ “$TranscodedFile” =~ Lost* ]]; then
LostFile=$(echo $TranscodedFile | sed ‘s/\(.*ost\).\([Ss][0-9]*[Ee][0-9]*\).mkv.*/\2./’)
echo “[*] A new episode of Lost (in HD!) is ready for your enjoyment: $LostFile” > /tmp/emailmessage.txt && sleep 2
mv -v “$TranscodedFile” $HDstorageDir/Lost/$LostFile
/usr/local/bin/email.sh
exit 0
elif [[ “$TranscodedFile” =~ Heroes* ]] || [[ “$TranscodedFile” =~ heroes* ]]; then
HeroesFile=$(echo $TranscodedFile | sed ‘s/\(.*oes\).\([Ss][0-9]*[Ee][0-9]*\).mkv./\2./’)
echo “[*] A new Episode of Heroes (in HD!) is ready for your enjoyment: $HeroesFile” > /tmp/emailmessage.txt && sleep 2
mv -v “$TranscodedFile” $HDstorageDir/Heroes/$HeroesFile
/usr/local/bin/email.sh
exit 0
elif [[ “$TranscodedFile” =~ Battlestar* ]] || [[ “$TranscodedFile” =~ battlestar* ]]; then
BattleFile=$(echo $TranscodedFile | sed ‘s/\(.*tica\).\([Ss][0-9]*[Ee][0-9]*\).mkv./\2./’)
echo “[*] A new Episode of Battlestar Galactica (in HD!) is ready for your enjoyment: $BattleFile” > /tmp/emailmessage.txt && sleep 2
mv -v “$TranscodedFile” $HDstorageDir/Battlestar\ Galactica/$BattleFile
/usr/local/bin/email.sh
exit 0
elif [[ “$TranscodedFile” =~ The.Office* ]] || [[ “$TranscodedFile” =~ the.office* ]]; then
OfficeFile=$(echo $TranscodedFile | sed ‘s/\(.*ffice\).\([Ss][0-9]*[Ee][0-9]*\).mkv./\2./’)
echo “[*] A new Episode of The Office (in HD!) is ready for your enjoyment: $OfficeFile” > /tmp/emailmessage.txt && sleep 2
mv -v “$TranscodedFile” $HDstorageDir/iThe\ Office/$OfficeFile
/usr/local/bin/email.sh
exit 0
elif [[ “$TranscodedFile” =~ 24* ]]; then
TwentyFourFile=$(echo $TranscodedFile | sed ‘s/\(.*4\).\([Ss][0-9]*[Ee][0-9]*\).mkv./\2./’)
echo “[*] A new Episode of 24 (in HD!) is ready for your enjoyment: $TwentyFourFile” > /tmp/emailmessage.txt && sleep 2
mv -v “$TranscodedFile” $HDstorageDir/24/$TwentyFourFile
/usr/local/bin/email.sh
exit 0
else
echo “[+] $1 does not match our TV shows, pushing it to /storage/HD Movies on lunchbox” > /tmp/emailmessage.txt
mv -v “$TranscodedFile” “/storage/HD\ Movies/$(echo $i | sed -e ‘s/\(.*\).720.*\(.mkv\)\(.mp4\)/\1\3/’)”
/usr/local/bin/email.sh
fi

exit 0

Hopefully someone finds this useful for their own media management purposes. Basically the script just waits until ffmpeg isn’t running, emails me that it is starting, transcodes the HD file for playback on the AppleTV, emails me when transcoding is done, and then uses sed (string editor) to chop of the bits of the filename I don’t want showing up, and places it in the correct directory depending on the name of the show. Also, if the filename doesn’t match any show, it goes to the HD Movies directory. In addition, when it’s all over, it emails me to let me know I’ve got some new media to enjoy!

Advertisements

Author: jayholler

A technology lover living in California with my wife and two children.

One thought on “Tweaking and HD”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s