All pastes #635711 Raw Edit

Juski

public text v1 · immutable
#635711 ·published 2007-07-27 09:42 UTC
rendered paste body
#!/usr/bin/perl -w
# ipodexport.pl v2.0
# By Justin Hornsby 22 October 2006
# 
# A MythTV user job script for exporting MythTV recordings to Ipod & other xvid/aac formats
#
# Usage info will be output if the script is run with no arguments (or insufficient arguments)
#
# Contains elements of mythtv.pl by Nigel Pearson
# Initial idea from nuvexport
# 
# requirements: ffmpeg with aac & xvid support enabled, PERL and the DBI & DBD::mysql modules
#
# NOTE: May not work with SVN trunk storage groups
#

# PERL MODULES WE WILL BE USING
use DBI;
use DBD::mysql

# MythTV Database details - edit these to suit
$host     = '127.0.0.1';
$database = 'mythconverg';
$user     = 'mythtv';
$pass     = $user;
$hostname = 'mythback';

# Location of the recordings
$dir = '/video/videorec/';    # Where the recordings are

$exportdir = '/home/mythtv/';
$bitrate = '96';
$aspect = '4:3';
$size = '320x240';
$prefix = '/usr/bin';


$db = undef;
$connect = undef;
$debug = 0;
######################################
#                                    #
#        SUBROUTINES ARE HERE        #
#                                    #
######################################


# Try to find the database
#
sub findDatabase()
{
    my $found = 0;
    my @mysql = ('/usr/local/share/mythtv/mysql.txt',
        '/usr/share/mythtv/mysql.txt',
        '/etc/mythtv/mysql.txt',
        '/usr/local/etc/mythtv/mysql.txt',
        "$ENV{HOME}/.mythtv/mysql.txt",
        'mysql.txt'
    );
    foreach my $file (@mysql) {
        next unless (-e $file);
        $found = 1;
        open(CONF, $file) or die "Unable to open $file:  $!\n\n";
        while (my $line = <CONF>) {
            # Cleanup
            next if ($line =~ /^\s*#/);
            $line =~ s/^str //;
            chomp($line);

            # Split off the var=val pairs
            my ($var, $val) = split(/\=/, $line, 2);
            next unless ($var && $var =~ /\w/);
            if ($var eq 'DBHostName') {
                $host = $val;
            } elsif ($var eq 'DBUserName') {
                $user = $val;
            } elsif ($var eq 'DBName') {
                $database = $val;
            } elsif ($var eq 'DBPassword') {
                $pass = $val;
            }
        }
        close CONF;
    }

if ( ! $found )
    {   die "Unable to locate mysql.txt:  $!\n\n"  }
}


#  DB connect subroutine
# =======================
# openDatabase() - Connect or die
#

sub openDatabase()
{
    $db = DBI->connect("dbi:mysql:database=$database" .
                               ":host=$host",
                               $user, $pass)
              or die "Cannot connect to database: $!\n\n";

    return $db;
}

##################################
#                                #
#    Main code starts here !!    #
#                                #
##################################
#
#
# FIND THE DATABASE SETTINGS

$usage = "\nHow to use dvbradioexport.pl \n\nipodexport.pl exportdir=/foo/bar starttime=%STARTTIME%
chanid=%CHANID bitrate=x size=320x240 aspect=4:3 debug\n"
        ."\n%CHANID% = channel ID associated with the recording to export\n"
        ."%STARTTIME% = recording start time in either 'yyyy-mm-dd hh:mm:ss' or 'yyyymmddhhmmss' format\n"
        ."exportdir = dir to export completed MP3 files to (note the user the script runs as must have write permission on that dir\n"
        ."size = frame size of output file.  320x240 is the default value \n"
        ."aspect = aspect ratio of output file.  Valid values are 4:3 (default) and 16:9 \n"
        ."bitrate = audio bitrate in output file in kbps.  Default value is 96 \n"
        ."debug = enable debugging information - outputs which commands would be run etc\n";

findDatabase();

# get this script's ARGS
#

$num = $#ARGV + 1;

# if user hasn't passed enough arguments, die and print the usage info

if ($num le "2") {
              die "$usage";
            }

#
# Get all the arguments
#

foreach (@ARGV){
                if ($_ =~ m/debug/) {
                                  $debug = 1;
                                 }
                elsif ($_ =~ m/size/) {
                                 $size = (split(/\=/,$_))[1];
                                 }
                elsif ($_ =~ m/aspect/) {
                                 $aspect = (split(/\=/,$_))[1];
                                 }
                elsif ($_ =~ m/bitrate/) {
                                 $bitrate = (split(/\=/,$_))[1];
                                 }
                elsif ($_ =~ m/starttime/) {
                                  $starttime = (split(/\=/,$_))[1];
                                  }
                elsif ($_ =~ m/chanid/) {
                                  $chanid = (split(/\=/,$_))[1];
                                  }
                elsif ($_ =~ m/exportdir/) {
                                  $exportdir = (split(/\=/,$_))[1];
                                  }
    }

#
# connect to database
#
$connect = openDatabase(); 

# PREPARE THE QUERY
$query = "SELECT title, subtitle FROM recorded WHERE chanid=$chanid AND starttime='$starttime'";

$query_handle = $connect->prepare($query);


# EXECUTE THE QUERY
$query_handle->execute() || die "Cannot connect to database \n";

# BIND TABLE COLUMNS TO VARIABLES
$query_handle->bind_columns(undef, \$title, \$subtitle);

# LOOP THROUGH RESULTS
$query_handle->fetch();

# FIND WHERE THE RECORDINGS LIVE
$query = "SELECT data FROM settings WHERE value='RecordFilePrefix' AND hostname='$hostname'";

$query_handle = $connect->prepare($query);
$query_handle->execute()  || die "Unable to query settings table";

my ($dir) = $query_handle->fetchrow_array;

# FIND OUT THE CHANNEL NAME
$query = "SELECT name FROM channel WHERE chanid=$chanid";
$query_handle = $connect->prepare($query);
$query_handle->execute()  || die "Unable to query settings table";

my ($channame) = $query_handle->fetchrow_array;

# replace whitespace in channame with dashes
$channame =~ s/\s+/-/g;

# replace whitespace in title with underscores
$title =~ s/\W+/_/g;
# replace whitespace in subtitle with underscores
$subtitle =~ s/\W+/_/g;

#
# Remove non alphanumeric chars from $starttime & $endtime
#
 
$newstarttime = $starttime;

$newstarttime =~ s/[|^\W|\s|-|]//g;

$year = substr($newstarttime, 0, 4);

$filename = $dir."/".$chanid."_".$newstarttime.".mpg";

$newfilename = $exportdir."/".$channame."_".$title."_".$subtitle."_".$newstarttime.".mp4";

if ($debug) {
                          print "\n\n Source filename:$filename \nDestination filename:$newfilename\n \n";
                        }

#
# Now run ffmpeg to get ipod compatible video with a simple ffmpeg line

$command = $prefix."/nice -n19 ffmpeg -i $filename -vcodec xvid -b 300 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac -ab $bitrate -s $size -aspect $aspect '$newfilename' 2>&1";

if ($debug) {
                      print "\n\nUSING $command \n\n";
                    }

if (!$debug) { 
        system "$command";
    }