Convert all Dreambox recordings to iphone/ipad format automatically

This article describes a way to automatically backup your dreambox recordings to a linux machine and convert them into mp4 files, which can be used on the iphone,ipad and ipod.

My linux machine runs Ubuntu 10.04 LTS/Lucid Lynx. For the backup of the recordings i use lftp and for the convertion HandBrakeCLI.

1. Install needed tools

First of all we have to install this two programs:

root@dubai ~ # sudo aptitude install lftp handbrake-cli

On my ubuntu version handrake was broken, so i had to add the handbrake snapshot repository first:

root@dubai ~ # sudo add-apt-repository ppa:stebbins/handbrake-snapshots
root@dubai ~ # sudo aptitude update
root@dubai ~ # sudo aptitude install handbrake-cli

2. Create needed directories

Next we create the following directories:

  • /home/dream/scripts – for the scripts we need
  • /home/dream/movie – the backup directory for the recordings
  • /home/dream/archive – the directory for the converted mp4 files

3. Backup the dreambox recordings

Lftp is just perfect to mirror a remote ftp directory. The easiest way to get the recordings from your dreambox is by ftp. The following script is a ftp script you can pass to lftp. First it will connect to your dreambox (you have to change the ip in the first line), then it will login as user root with the password dream. Afterwards it mirrors the /hdd/movie directory to /home/dream/movie. With the parameter –older-than=now-2hours you can easily avoid that running recordings get transfered.

open 192.168.0.XXX
user root dream
mirror -e -v --verbose=2 --older-than=now-2hours /hdd/movie /home/dream/movie
bye

This script goes to /home/dream/scripts/backup_dreambox.ftp

Now you can run lftp -f /home/dream/scripts/backup_dreambox.ftp to backup your dreambox movies.

4. Convert the recordings to mp4 files

To convert the recordings to mp4 i use the Handbrake command line program. It was not that easy to find out the perfect settings but this ones work for me and do also handle 720p HD videos. The only problem i still have is, that my iphone/ipad don’t likes files bigger than 4GB.

I use the following script to handle the convertion. On the first lines you can define the settings like the output folder, the quality of the target file, the handbrake settings.

The script will convert the file you passed as command line argument. If the target file already exists, it will just skip that file. First the scripts scans the the audio tracks on the movie to create the audio parameters for the handbrake convertion command. Currently it supports up to 2 audio tracks. Than it start the convertion and creates the new file in the /home/dream/archive using the original filename without Date/Time.

#!/bin/bash
QUALITY=0.67
OUTPUT=/home/dream/archive
HANDBRAKE_SETTINGS="-4 -T -2 -e x264 --loose-anamorphic -4 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 -X 1280"

if [ $# -gt 0 ]
then
 OUTPUT_FILE=`echo $1|awk -F"-" '{ print $3 $4 $5}'|awk -F. '{ print $1 }'`.mp4
 TARGET=$OUTPUT/`echo $OUTPUT_FILE`
 echo $TARGET

 if [ -f "$TARGET" ]
 then
   exit 0
 fi

 AUDIO_TRACKS=`HandBrakeCLI -t 0 -i "$1" 2>&1|grep "scan: audio"|wc -l`
 AUDIO="-a 1"

 if [ "$AUDIO_TRACKS" -eq "2" ]
 then
   AUDIO="-a 1,2"
 fi

 HandBrakeCLI $HANDBRAKE_SETTINGS $AUDIO -q $QUALITY -i "$1" -o "$TARGET"
else
 echo "usage: dream2mp4 INPUT"
fi

This script goes to /home/dream/scripts/dream2mp4.sh

5. Glue everything together

Finally we create a script to first backup the dreambox and than convert all movies. You can just execute the script every day, hour or minute with a cronjob. It will first check if it’s already running.

#!/bin/bash
DREAM_MOVIES=/home/dream/movie
PID_SEARCH=`ps -fea|grep [b]ackup_dreambox.sh|grep bash`
let PID_COUNT=`ps -fea|grep [b]ackup_dreambox.sh|grep bash|wc -l`
if [ $PID_COUNT -lt 3 ]
then
 lftp -f /home/dream/scripts/backup_dreambox.ftp
 find $DREAM_MOVIES -name "*.ts" -exec /home/dream/scripts/dream2mp4.sh {} \;
else
 echo "backup is already/still running"
fi

This script goes to /home/dream/scripts/backup_dreambox.sh

The scripts: dream.tar.gz

posted in Development by david

Follow comments via the RSS Feed | Einen Kommentar hinterlassen | Trackback URL

Leave Your Comment