Monday, April 9, 2012

Farmerdave's htpc components

Gigabyte ga-ma78gm-s2h
AMD Athlon X2 4850e
2GB DDR2 800MHz ram
MSI RX2600XT GPU (needed for the component TV-out – older tv) - removed

Asus GeForce GT430 Silent
Leadtek DTV2000H Tuner

Friday, March 30, 2012

My /etc/fstab for Root partition and Root Clone partition

ssdRoot /etc/fstab




# /etc/fstab: static file system information
#
# <file system> <dir> <type> <options> <dump> <pass>
tmpfs /tmp tmpfs nodev,nosuid 0 0
#
LABEL=htpcHome /home ext4 defaults 0 1
LABEL=ssdRoot / ext4 defaults 0 1
LABEL=ssdRootClone /mnt/ssdRootClone ext4 defaults 0 1
LABEL=grubPartition /boot/grub ext4 defaults 0 1
LABEL=ssdSwap swap swap defaults 0 0
LABEL=mythMovies /home/david/MythTV/Glenelg xfs defaults 0 1
# NFS client
#david-desktop:/ /mnt nfs4 rw,hard,intr,bg 0 0
#david-desktop:/music /home/david/Music nfs4 rw,hard,intr,bg 0 0
#david-desktop:/movies /home/david/Movies nfs4 rw,hard,intr,bg 0 0
# Samba
//david-desktop/movies /home/david/Movies cifs noatime,bg,username=david,password=david 0 0
//david-desktop/music /home/david/Music cifs noatime,bg,username=david,password=david 0 0
//david-desktop/tvseries        /home/david/TVseries    cifs    noatime,bg,username=david,password=david        0 0

ssdRootClone /etc/fstab

# /etc/fstab: static file system information
#
# <file system> <dir> <type> <options> <dump> <pass>
tmpfs /tmp tmpfs nodev,nosuid 0 0
#
LABEL=htpcHome /home ext4 defaults 0 1
LABEL=ssdRoot /mnt/ssdRoot ext4 defaults 0 1
LABEL=ssdRootClone / ext4 defaults 0 1
LABEL=grubPartition /boot/grub ext4 defaults 0 1
LABEL=ssdSwap swap swap defaults 0 0
LABEL=mythMovies /home/david/MythTV/Glenelg xfs defaults 0 1
# NFS client
#david-desktop:/ /mnt nfs4 rw,hard,intr,bg 0 0
#david-desktop:/music /home/david/Music nfs4 rw,hard,intr,bg 0 0
#david-desktop:/movies /home/david/Movies nfs4 rw,hard,intr,bg 0 0
# Samba
//david-desktop/movies /home/david/Movies cifs noatime,bg,username=david,password=david 0 0
//david-desktop/music /home/david/Music cifs noatime,bg,username=david,password=david 0 0
//david-desktop/tvseries        /home/david/TVseries    cifs    noatime,bg,username=david,password=david        0 0


Thursday, March 29, 2012

Move recordings from one MythTV folder to another - update the database

This script is built up from several transcode scripts and database scripts I have found elsewhere. 

It will take a recording from MythTV, give it a human readable filename, and then shift the recording to a new folder that you can specify. Finally, it will update the database to point to the new location.



#!/usr/bin/perl -w
# ============================================================================
# = NAME 
# move_myth_recordings.pl
#
# = PURPOSE
# Move recording from MythTV to a different folder, updating the database
# Hs the ability to change the filename to human readable
#
# = USAGE
my $usage = 'Usage:
move_myth_recordings.pl -j %JOBID%
move_myth_recordings.pl -f %FILE% 
';
# ============================================================================
use strict;
use MythTV;
use XML::Simple;
use File::Copy;


# What file are we copying/transcoding?
my $file  = '';
my $jobid = -1;


# do nothing?
my $noexec = 0;


# extra console output?
my $DEBUG = 1;


# some globals
my ($chanid, $command, $query, $ref, $starttime, $showtitle, $episodetitle);
my ($seasonnumber, $episodenumber, $episodedetails);
my ($newfilename, $newstarttime);
my $xmlparser = new XML::Simple;
my $xmlstring;


my $mt = '';
my $db = '';


# Set your desired output directory
# Make sure this folder is writable by your myth user
my $outputdir = "/home/david/Recordings/Sturt"; # Don't use a trailing slash


# ============================================================================
sub Reconnect()
{
    $mt = new MythTV();
    $db = $mt->{'dbh'};
}


# ============================================================================
sub Die($)
{
    print STDERR "@_\n";
    exit -1;
}


# ============================================================================
# Parse command-line arguments, check there is something to do:
#
if ( ! @ARGV )
{   Die "$usage"  }
Reconnect;


while ( @ARGV && $ARGV[0] =~ m/^-/ )
{
    my $arg = shift @ARGV;


    if ( $arg eq '-d' || $arg eq '--debug' )
    {   $DEBUG = 1  }
    elsif ( $arg eq '-n' || $arg eq '--noaction' )
    {   $noexec = 1  }
    elsif ( $arg eq '-j' || $arg eq '--jobid' )
    {   $jobid = shift @ARGV  }
    elsif ( $arg eq '-f' || $arg eq '--file' )
    {   $file = shift @ARGV  }
    else
    {
        unshift @ARGV, $arg;
        last;
    }
}


if ( ! $file && $jobid == -1 )
{
    Die "No file or job specified. $usage";
}


# ============================================================================
# If we were supplied a jobid, lookup chanid
# and starttime so that we can find the filename
#
if ( $jobid != -1 )
{
    $query = $db->prepare("SELECT chanid, starttime " .
                          "FROM jobqueue WHERE id=$jobid;");
    $query->execute || Die "Unable to query jobqueue table";
    $ref       = $query->fetchrow_hashref;
    $chanid    = $ref->{'chanid'};
    $starttime = $ref->{'starttime'};
    $query->finish;


    if ( ! $chanid || ! $starttime )
    {   Die "Cannot find details for job $jobid"  }


    $query = $db->prepare("SELECT basename FROM recorded " .
                          "WHERE chanid=$chanid AND starttime='$starttime';");
    $query->execute || Die "Unable to query recorded table";
    ($file) = $query->fetchrow_array;
    $query->finish;


    if ( ! $file )
    {   Die "Cannot find recording for chan $chanid, starttime $starttime"  }


    if ( $DEBUG )
    {
        print "Job $jobid refers to recording chanid=$chanid,",
              " starttime=$starttime\n"
    }
}
else
{
    if ( $file =~ m/(\d+)_(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/ )
    {   $chanid = $1, $starttime = "$2-$3-$4 $5:$6:$7"  }
    else
    {
        print "File $file has a strange name. Searching in recorded table\n";
        $query = $db->prepare("SELECT chanid, starttime " .
                              "FROM recorded WHERE basename='$file';");
        $query->execute || Die "Unable to query recorded table";
        ($chanid,$starttime) = $query->fetchrow_array;
        $query->finish;


        if ( ! $chanid || ! $starttime )
        {   Die "Cannot find details for filename $file"  }
    }
}


# ============================================================================
# A commonly used SQL row selector:
my $whereChanAndStarttime = "WHERE chanid=$chanid AND starttime='$starttime'";


# ============================================================================
# Find the directory that contains the recordings, check the file exists
#
my $dir  = undef;
my $dirs = $mt->{'video_dirs'};


foreach my $d ( @$dirs )
{
 if ( ! -e $d )
 {   Die "Cannot find directory $dir that contains recordings"  }


 if ( -e "$d/$file" )
 {
  $dir = $d;
  last
 }
 else
 {   print "$d/$file does not exist\n"   }
}


if ( ! $dir )
{   Die "Cannot find recording"  }


# ============================================================================
# First, generate a new filename,
#


$query = $db->prepare("SELECT title FROM recorded $whereChanAndStarttime;");
$query->execute || Die "Unable to query recorded table";
$showtitle = $query->fetchrow_array;
$query->finish;


$query = $db->prepare("SELECT subtitle FROM recorded $whereChanAndStarttime;");
$query->execute || Die "Unable to query recorded table";
$episodetitle = $query->fetchrow_array;
$query->finish;


if ( $episodetitle ne "" ) 
{
  $seasonnumber = "";
  $episodenumber = "";
  $xmlstring = `/usr/share/mythtv/metadata/Television/ttvdb.py -N "$showtitle" "$episodetitle"`;
  if ( $xmlstring ne "" ) {
    $episodedetails =$xmlparser->XMLin($xmlstring);
    $seasonnumber = $episodedetails->{item}->{season};
    $episodenumber = $episodedetails->{item}->{episode};
  }
}
my ($year,$month,$day,$hour,$mins,$secs) = split m/[- :]/, $starttime;
my $oldShortTime = sprintf "%04d%02d%02d",
                   $year, $month, $day;
my $iter = 0;


do {
  if ( $episodetitle eq "" || $seasonnumber eq "" || $episodenumber eq "" )
  {
    $newfilename = sprintf "%s_%s.%s.%s", $showtitle, $month, $day, $year;
  } else {
    $newfilename = sprintf "%s_S%0sE%0s_%s", $showtitle, $seasonnumber, $episodenumber, $episodetitle;
  }
  $newfilename =~ s/\;/   AND   /g;
  $newfilename =~ s/\&/   AND   /g;
  $newfilename =~ s/\s+/ /g;
  $newfilename =~ s/\s/_/g;
  $newfilename =~ s/:/_/g;
  $newfilename =~ s/__/_/g;
  $newfilename =~ s/\(//g;
  $newfilename =~ s/\)//g;
  $newfilename =~ s/'//g;
  $newfilename =~ s/\!//g;
  $newfilename =~ s/\///g;
  if ( $iter != "0" ) 
  {  $newfilename = sprintf "%s_%d%s", $newfilename, $iter, ".mkv"  } else { $newfilename = sprintf "%s%s", $newfilename, ".mkv" }
  $iter ++;
  $secs = $secs + $iter;
  $newstarttime = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
                    $year, $month, $day, $hour, $mins, $secs;
} while  ( -e "$outputdir/$newfilename" );


$DEBUG && print "$outputdir/$newfilename seems unique\n";




# ============================================================================
# Next, move the file.
#
chdir $dir;
copy($file, "$outputdir/$newfilename") or die "File cannot be copied.";
#move($file, "$outputdir/$newfilename") or die "File cannot be moved.";




# ============================================================================
# Last, copy the existing recorded details with the new file name.
#
Reconnect;
$query = $db->prepare("SELECT * FROM recorded $whereChanAndStarttime;");
$query->execute ||  Die "Unable to query recorded table";
$ref = $query->fetchrow_hashref;
$query->finish;


$ref->{'starttime'} = $newstarttime;
$ref->{'basename'}  = $newfilename;
if ( $DEBUG && ! $noexec )
{
    print 'Old file size = ' . (-s "$dir/$file")        . "\n";
    print 'New file size = ' . (-s "$outputdir/$newfilename") . "\n";
}
$ref->{'filesize'}  = -s "$outputdir/$newfilename";


my $extra = 'Copy';




#
# The new recording file has no cutlist, so we don't insert that field
#
my @recKeys = grep(!/^cutlist$/, keys %$ref);


#
# Build up the SQL insert command:
#
$command = 'INSERT INTO recorded (' . join(',', @recKeys) . ') VALUES ("';
foreach my $key ( @recKeys )
{
    if (defined $ref->{$key})
    {   $command .= quotemeta($ref->{$key}) . '","'   }
    else
    {   chop $command; $command .= 'NULL,"'   }
}


chop $command; chop $command;  # remove trailing comma quote


$command .= ');';


if ( $DEBUG || $noexec )
{   print "# $command\n"  }


if ( ! $noexec )
{   $db->do($command)  || Die "Couldn't create new recording's record, but transcoded file exists $newfilename\n"   }


# ============================================================================
# Now, delete the old recording and png files, keeping the new transcoded recording
if ( -e "$outputdir/$newfilename" )
{
   $command = 'DELETE from recorded ' . join(',', $whereChanAndStarttime) . '; '; # Build up the SQL delete command:
    $db->do($command); # remove the mysql entry for the recording
    my $filepng = $file . join(',','.png'); # Create the file png filename
    my $filepng2 = $file . join(',','.-1.100x75.png'); # Create the file png filename
    unlink($file); # remove the original file
    unlink($filepng); # remove the file's png
    unlink($filepng2); # remove the file's png
}


# ============================================================================


$db->disconnect;
1;


# ============================================================================

Wednesday, March 28, 2012

Waiting for boot device

Waiting 30 seconds for device /dev/archiso ...
ERROR: boot device didn't show up after 30 seconds ...
Falling back to interactive prompt
You can try to fix the problem manually, logout when you are finished
ramfs$

The solution for me was to type the following at the ramfs$ prompt

ramfs$ udevadm trigger
ramfs$ exit


Tuesday, March 27, 2012

When /var/lib/mysql gets really large

I followed the instructions given here


In short,


Add this line to your /etc/mysql/my.cnf and it'll need to be in the 
[mysqld] section. 

expire_logs_days = 7 

Then log into Mysql (# mysql -u root -p mysql) and run this command to set the variable without 

having to restart Mysql. 

SET GLOBAL expire_logs_days=7; 

Immediately expire the old logs:

PURGE BINARY LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 7 DAY); 

Monday, March 12, 2012

XBMC configuration screenshots

Here is a screenshot of the settings used to get sound working in XBMC from the HDMI connection on my GPU (NVidia GT430).

To find the correct name for the card, I used the command

$aplay -l

**** List of PLAYBACK Hardware Devices ****
card 0: SB [HDA ATI SB], device 0: ALC889A Analog [ALC889A Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: SB [HDA ATI SB], device 1: ALC889A Digital [ALC889A Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: NVidia [HDA NVidia], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: NVidia [HDA NVidia], device 7: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: NVidia [HDA NVidia], device 8: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: NVidia [HDA NVidia], device 9: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
It was important for me to do it this way rather than simply a card number because these numbers can change on reboot.

Each card/device combination was tested using
$ aplay -D plughw:1,9 /usr/share/sounds/alsa/Front_Center.wav
until sound was heard. The important part in the above command is "plughw:1,9" which is the card number and device number from the ****List of PLAYBACK Hardware Devices****, respectively.

Once this was working, the card number can be replaced by the name of the card. For me, I replaced "1" with "Nvidia" to give the command
$ aplay -D plughw:NVidia,9 /usr/share/sounds/alsa/Front_Center.wav
I then used this information in my XBMC settings.

Notice the information is being used in the Custom audio device section.

High quality user job transcode script for MythTV using Handbrake

A high quality user job script for MythTV that transcodes a recording to .mkv format and gives the resulting filename a human readable name.

#!/usr/bin/perl -w
# ============================================================================
# = NAME 
# x264_transcode_high.pl
#
# = PURPOSE
# Convert mpeg2 file from myth to h264 with aac audio.
#
# = USAGE
my $usage = 'Usage:
x264_transcode_high.pl -j %JOBID% 
x264_transcode_high.pl -f %FILE% 
';

# ============================================================================

use strict;
use MythTV;
use XML::Simple;

# What file are we copying/transcoding?
my $file  = '';
my $jobid = -1;

# do nothing?
my $noexec = 0;

# extra console output?
my $DEBUG = 1;

# some globals
my ($chanid, $command, $query, $ref, $starttime, $showtitle, $episodetitle);
my ($seasonnumber, $episodenumber, $episodedetails);
my ($newfilename, $newstarttime);
my $xmlparser = new XML::Simple;
my $xmlstring;
# globals for stream and resolution mapping
my ($output, $videostream, $audiostreamsurround, $audiostreamstereo, $framerate);

# transcode options
my $videocodec = "x264";
my $videoquality = "22"; # target quality
my $videobitrate = "1500k"; # target bitrate - not used if using videoquality
my $audiostream = 1; # default audio channel
my $audiocodec = "copy:ac3";
my $audiobitrate = "256";
my $audiochannels = "auto"; 
my $audiosamplerate = "Auto";
my $audiodrc = "1.0";
my $ftype = "mkv";
my $detelecine = "--detelecine";
my $decomb = "--decomb";
my $looseanamorphic = "--loose-anamorphic";
my $chapters = "-m";

# x264 options
my $badapt = "b-adapt=2";
my $rclookahead = "rc-lookahead=50";
my $x264options = "$badapt:$rclookahead";
# ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0

my $mt = '';
my $db = '';

sub Reconnect()
{
    $mt = new MythTV();
    $db = $mt->{'dbh'};
}

# ============================================================================
sub Die($)
{
    print STDERR "@_\n";
    exit -1;
}
# ============================================================================
# Parse command-line arguments, check there is something to do:
#
if ( ! @ARGV )
{   Die "$usage"  }
Reconnect;

while ( @ARGV && $ARGV[0] =~ m/^-/ )
{
    my $arg = shift @ARGV;

    if ( $arg eq '-d' || $arg eq '--debug' )
    {   $DEBUG = 1  }
    elsif ( $arg eq '-n' || $arg eq '--noaction' )
    {   $noexec = 1  }
    elsif ( $arg eq '-j' || $arg eq '--jobid' )
    {   $jobid = shift @ARGV  }
    elsif ( $arg eq '-f' || $arg eq '--file' )
    {   $file = shift @ARGV  }
    else
    {
        unshift @ARGV, $arg;
        last;
    }
}

if ( ! $file && $jobid == -1 )
{
    Die "No file or job specified. $usage";
}

# ============================================================================
# If we were supplied a jobid, lookup chanid
# and starttime so that we can find the filename
#
if ( $jobid != -1 )
{
    $query = $db->prepare("SELECT chanid, starttime " .
                          "FROM jobqueue WHERE id=$jobid;");
    $query->execute || Die "Unable to query jobqueue table";
    $ref       = $query->fetchrow_hashref;
    $chanid    = $ref->{'chanid'};
    $starttime = $ref->{'starttime'};
    $query->finish;

    if ( ! $chanid || ! $starttime )
    {   Die "Cannot find details for job $jobid"  }

    $query = $db->prepare("SELECT basename FROM recorded " .
                          "WHERE chanid=$chanid AND starttime='$starttime';");
    $query->execute || Die "Unable to query recorded table";
    ($file) = $query->fetchrow_array;
    $query->finish;

    if ( ! $file )
    {   Die "Cannot find recording for chan $chanid, starttime $starttime"  }

    if ( $DEBUG )
    {
        print "Job $jobid refers to recording chanid=$chanid,",
              " starttime=$starttime\n"
    }
}
else
{
    if ( $file =~ m/(\d+)_(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/ )
    {   $chanid = $1, $starttime = "$2-$3-$4 $5:$6:$7"  }
    else
    {
        print "File $file has a strange name. Searching in recorded table\n";
        $query = $db->prepare("SELECT chanid, starttime " .
                              "FROM recorded WHERE basename='$file';");
        $query->execute || Die "Unable to query recorded table";
        ($chanid,$starttime) = $query->fetchrow_array;
        $query->finish;

        if ( ! $chanid || ! $starttime )
        {   Die "Cannot find details for filename $file"  }
    }
}


# A commonly used SQL row selector:
my $whereChanAndStarttime = "WHERE chanid=$chanid AND starttime='$starttime'";

# ============================================================================
# Find the directory that contains the recordings, check the file exists
#
my $dir  = undef;
my $dirs = $mt->{'video_dirs'};

foreach my $d ( @$dirs )
{
 if ( ! -e $d )
 {   Die "Cannot find directory $dir that contains recordings"  }

 if ( -e "$d/$file" )
 {
  $dir = $d;
  last
 }
 else
 {   print "$d/$file does not exist\n"   }
}

if ( ! $dir )
{   Die "Cannot find recording"  }

# ============================================================================
# Do you want to implement the deinterlace feature?
#my $dowedeinterlace = " ";
# ============================================================================
# Use ffmpeg to find if we need to deinterlace
my $deinterlace = " ";
#if ( $dowedeinterlace == '1')
# {
# $command = "ffmpeg -i $dir/$file ";
# open(FF_info, "$command 2>&1 |");
# while ( defined(my $line = ) ) {
#  chomp($line);
#  if ( $line =~ /^\s*Stream.*#(\S\.\S).*:\sVideo.*\s(\S*)\stbr/ )
#  {
#   $framerate = $2;
#   next;
#  }
# }
# if ( $framerate <= 30.0 ) { $deinterlace = "--deinterlace" } elsif ( $framerate > 30 && $framerate <= 60 ) { $deinterlace = "" }
#}

# ============================================================================
# First, generate a new filename,
#

$query = $db->prepare("SELECT title FROM recorded $whereChanAndStarttime;");
$query->execute || Die "Unable to query recorded table";
$showtitle = $query->fetchrow_array;
$query->finish;

$query = $db->prepare("SELECT subtitle FROM recorded $whereChanAndStarttime;");
$query->execute || Die "Unable to query recorded table";
$episodetitle = $query->fetchrow_array;
$query->finish;

if ( $episodetitle ne "" ) 
{
  $seasonnumber = "";
  $episodenumber = "";
  $xmlstring = `/usr/share/mythtv/metadata/Television/ttvdb.py -N "$showtitle" "$episodetitle"`;
  if ( $xmlstring ne "" ) {
    $episodedetails =$xmlparser->XMLin($xmlstring);
    $seasonnumber = $episodedetails->{item}->{season};
    $episodenumber = $episodedetails->{item}->{episode};
  }
}
my ($year,$month,$day,$hour,$mins,$secs) = split m/[- :]/, $starttime;
my $oldShortTime = sprintf "%04d%02d%02d",
                   $year, $month, $day;
my $iter = 0;

do {
  if ( $episodetitle eq "" || $seasonnumber eq "" || $episodenumber eq "" )
  {
    $newfilename = sprintf "%s_%s.%s.%s", $showtitle, $month, $day, $year;
  } else {
    $newfilename = sprintf "%s_S%0sE%0s_%s", $showtitle, $seasonnumber, $episodenumber, $episodetitle;
  }
  $newfilename =~ s/\;/   AND   /g;
  $newfilename =~ s/\&/   AND   /g;
  $newfilename =~ s/\s+/ /g;
  $newfilename =~ s/\s/_/g;
  $newfilename =~ s/:/_/g;
  $newfilename =~ s/__/_/g;
  $newfilename =~ s/\(//g;
  $newfilename =~ s/\)//g;
  $newfilename =~ s/'//g;
  $newfilename =~ s/\!//g;
  $newfilename =~ s/\///g;
  if ( $iter != "0" ) 
  {  $newfilename = sprintf "%s_%d%s", $newfilename, $iter, ".mkv"  } else { $newfilename = sprintf "%s%s", $newfilename, ".mkv" }
  $iter ++;
  $secs = $secs + $iter;
  $newstarttime = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
                    $year, $month, $day, $hour, $mins, $secs;
} while  ( -e "$dir/$newfilename" );

$DEBUG && print "$dir/$newfilename seems unique\n";


# ============================================================================
# Third, do the transcode
#
# $audiochannels = 6;
# $audiostream = $audiostreamsurround;
# if ( $audiostreamsurround eq "" )
# {
#   $audiochannels = 2;
#   $audiostream = $audiostreamstereo;
# } 

$command = "/usr/bin/HandBrakeCLI -i $file";
$command = "$command -o $newfilename";
$command = "$command -e $videocodec";
$command = "$command -q $videoquality";
$command = "$command -a $audiostream";
$command = "$command -E $audiocodec";
$command = "$command -B $audiobitrate";
$command = "$command -6 $audiochannels";
$command = "$command -R $audiosamplerate";
$command = "$command -D $audiodrc";
$command = "$command -f $ftype";
$command = "$command $detelecine";
$command = "$command $decomb";
$command = "$command $looseanamorphic";
$command = "$command $chapters";
$command = "$command $deinterlace";
$command = "$command -x $x264options";

$DEBUG && print "Executing: $command\n";

chdir $dir;
system $command;

if ( ! -e "$dir/$newfilename" )
{   Die "Transcode failed\n"  }


# ============================================================================
# Last, copy the existing recorded details with the new file name.
#
Reconnect;
$query = $db->prepare("SELECT * FROM recorded $whereChanAndStarttime;");
$query->execute ||  Die "Unable to query recorded table";
$ref = $query->fetchrow_hashref;
$query->finish;

$ref->{'starttime'} = $newstarttime;
$ref->{'basename'}  = $newfilename;
if ( $DEBUG && ! $noexec )
{
    print 'Old file size = ' . (-s "$dir/$file")        . "\n";
    print 'New file size = ' . (-s "$dir/$newfilename") . "\n";
}
$ref->{'filesize'}  = -s "$dir/$newfilename";

my $extra = 'Copy';


#
# The new recording file has no cutlist, so we don't insert that field
#
my @recKeys = grep(!/^cutlist$/, keys %$ref);

#
# Build up the SQL insert command:
#
$command = 'INSERT INTO recorded (' . join(',', @recKeys) . ') VALUES ("';
foreach my $key ( @recKeys )
{
    if (defined $ref->{$key})
    {   $command .= quotemeta($ref->{$key}) . '","'   }
    else
    {   chop $command; $command .= 'NULL,"'   }
}

chop $command; chop $command;  # remove trailing comma quote

$command .= ');';

if ( $DEBUG || $noexec )
{   print "# $command\n"  }

if ( ! $noexec )
{   $db->do($command)  || Die "Couldn't create new recording's record, but transcoded file exists $newfilename\n"   }

# Delete the old recording, keeping the new transcoded recording
if ( -e "$dir/$newfilename" )
{   $command = 'DELETE from recorded ' . join(',', $whereChanAndStarttime) . '; '; # Build up the SQL delete command:
    $db->do($command); # remove the mysql entry for the recording
    my $filepng = $file . join(',','.png'); # Create the file png filename
    unlink($file); # remove the original file
    unlink($filepng); # remove the file's png
}

# ============================================================================

$db->disconnect;
1;