Wednesday, May 12, 2010

MythMobile: MythTV Streaming for Mobile Devices

I am a long time user of MythTV. I am also a recent user of Apple's iPad, iPhone and iPod Touch mobile devices. One of my personal projects has been to figure out how to watch my MythTV recordings on these and other such mobile devices.  For example it should work on Android based devices as well. After much research and trial and error, I have finally come up with a solution that seems to work well.

The solution is relatively straight forward.  I transcode the recordings and store them in a user friendly naming scheme in a folder that is shared via Apache web server and Samba file share.  For extra convenience, I copy the recordings via rsync to an Internet accessible web server so that I can watch my recordings from anywhere.

The biggest challenge to this project was getting the transcoding right. Here is a sample invocation of ffmpeg that works for me.
# nice ffmpeg -y -i "${input_file}" -s 480x352 -r 29.97 -g 300 \
   -vsync 1 -vcodec mpeg4 -b 1280k -maxrate 1536k \
   -async 1 -acodec libfaac -ab 1024 -ar 44100 -ac 2 \
   -flags +aic+mv4 -mbd 2 -cmp 2 -subcmp 2 -bufsize 2M \
   "${output_file}"
The three main video codecs that I recommend are MPEG-4 (e.g. mpeg4), DivX (e.g. libxvid), and H.264 (e.g. libx264).  The best video quality to me was a tie between MPEG-4 and DIVX.  You also can adjust the bitrate/maxbitrate to find the video quality/file size that best meets your needs.  A bitrate/maxrate combination of 1280k/1536k resulted in good video quality and about a 10x reduction in the file size from the native format (e.g. MythTV mpg) to mp4.

To simplify and automate my environment, I wrote two perl scripts that manage creating and presenting the mobile friendly video files.  The first perl script (e.g. MythTV2mp4.pl) transcodes, renames, and stores the video files in /var/www/MythMobile.  Note that Apache Web Server and Samba File Server have been configured to make /var/www/MythMobile available on my local network.  When running this script, you can specify via the optional flags the recordings directory with --r recordings_dir and the web directory with --w www_dir.  If no flags are specified, the default locations are /var/lib/mythtv/recordings for the recordings directory and /var/www/MythMobile for the web directory.

The second perl script (e.g. index.cgi) can be placed into a folder within your web server document root.  For example, I placed it in /var/www/MythMobile on my server.  This script facilitates presenting the video files in a Mobile device friendly format.  Note that I had to modify the Apache web server configuration (e.g. httpd.conf) with the following modifications.
  1. Add index.cgi to the DirectoryIndex.
  2. Add the .cgi handler by adding "AddHandler cgi-script .cgi".
  3. Add ExecCGI to the Options of the containing Directory.
With the installation of the index.cgi script and these three web server changes changes applied, the video files are presented with much more user friendly format as you can see from the pictures below.

Here are the vertical and horizontal views on an Apple iPhone.



Here is the vertical view on an Apple iPad.

Here is the horizontal view on an Apple iPad.

I bundled up the two perl scripts and a README and made them available as a zip file named MythMobile-1.0.zip.

Check it out and try it for yourself.

Enjoy!

Brad