This guide explains how to use a Perl script to transcode selected recordings into MP4 files with the help of Handbrake. It also details how to add the necessary iTunes metadata using AtomicParsley. The script can also include artwork (also known as cover art) for each show, automatically added via AtomicParsley from an artwork directory.

Steps to Use the Script:

  • Save the provided script below into a file named '/usr/local/bin/ipadtranscode.pl'.
  • Within the script, create and designate the destination directory and the artwork directory.
  • Add your chosen cover art to the artwork directory. The script will recognize a jpg file named identically to the show, with all non-alphanumeric characters replaced by underscores. For instance, the cover art for 'The Walking Dead' should be 'The_Walking_Dead.jpg', and 'Marvel’s Agents of S.H.I.E.L.D.' should be 'Marvel_s_Agents_of_S_H_I_E_L_D_.jpg'. A handy source for iTunes compatible cover art is Ben Dodson's iTunes Artwork Finder.
  • In MythTV, add the following command as a custom user job: '/usr/local/bin/ipadtranscode.pl starttime=%STARTTIMEUTC% chanid=%CHANID%'. You may label this job 'iPad Transcode'.
  • Ensure that 'HandBrakeCLI' and 'AtomicParsley' are installed on your system.

Here's the Perl script for 'ipadtranscode.pl':

#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
use MythTV;
use Time::ParseDate;
use Date::Format;

my $destination_directory = "/destination/directory/";
my $artwork_directory = "/artwork/directory/";
my $connect;
my ($starttime, $chanid);

# Assign values to $starttime and $chanid from @ARGV
for (@ARGV) {
    if (/starttime/) {
        $starttime = (split /=/)[1];
    }
    elsif (/chanid/) {
        $chanid = (split /=/)[1];
    }
}

my $myth = MythTV->new();
$connect = $myth->{'dbh'};

my $query = "SELECT title, subtitle, description, basename, season, episode FROM recorded WHERE chanid=$chanid AND starttime='$starttime'";
my $query_handle = $connect->prepare($query);
$query_handle->execute() or die "Cannot connect to database \n";
$query_handle->bind_columns(undef, \my($title, $subtitle, $description, $basename, $season, $episode));
$query_handle->fetch();

my $filetitle = $title =~ s/\W+/_/gr;
my $subtitle_file = $subtitle =~ s/\W+/_/gr;

my $source = "/var/lib/mythtv/recordings/" . $basename;
my $working = "/tmp/" . $chanid . "_" . $starttime . ".m4v";
my $destination = $destination_directory . $filetitle . "_" . $starttime . ".m4v";
my $sort_date = join "-", unpack "A4A2A2", $starttime;
my $art = $artwork_directory . $filetitle . ".jpg";

# Set subtitle to recording date if it's empty
if (!$subtitle) {
    my $sort = $sort_date;
    $subtitle = time2str("%B %e, %Y", parsedate($sort_date));
} else {
    my $sort = $subtitle;
}

# Set flags for description and artwork
my $description_flag = $description ? " --description \"$description\"" : "";
my $art_flag = -e $art ? " --artwork \"$art\"" : "";

# Build commands
my $hb_command = "HandBrakeCLI -i $source -o $working --preset=\"iPad\" 2>&1";
my $ap_command = "AtomicParsley \"$working\" --TVShowName \"$title\" --artist \"$title\" --title \"$subtitle\" --TVSeasonNum \"$season\" --TVEpisodeNum \"$episode\" --sortOrder name \"$sort\"$description_flag$art_flag --stik \"TV Show\" --overWrite 2>&1";
my $mv_command = "mv $working $destination";

# Execute commands
system $hb_command;
system $ap_command;
system $mv_command;

exit 0;

Guide to iTunes Encoding with MythTV via a Perl Script

This guide describes using a Perl script for iTunes encoding in MythTV. It details transcoding recordings to MP4, adding metadata with AtomicParsley, and incorporating show cover art. The script and implementation steps are provided.