#!/usr/bin/perl # the IP address of your TiVO (must be running tyserver) $tivoAddress = "10.1.2.3"; # database settings $dbhost = "localhost"; $dbname = "tivo2podcast"; $dbuser = "tivo2podcast"; $dbpass = "password"; # the path where tyls, tyget, tydemux, mplex and ffmpeg live $tyls = "/usr/local/bin/tyls"; $tyget = "/usr/local/bin/tyget"; $tydemux = "/usr/local/bin/tydemux"; $mplex = "/usr/local/bin/mplex"; $ffmpeg = "/usr/local/bin/ffmpeg"; $outputPath = "/usr/local/apache/htdocs/tivo"; ######################################################################### use DBI; use File::stat; $dbh1 = DBI->connect( "dbi:mysql:dbname=$dbname", $dbuser, $dbpass ) or die "Cannot connect to database: $DBI::errstr"; $dbh2 = DBI->connect( "dbi:mysql:dbname=$dbname", $dbuser, $dbpass ) or die "Cannot connect to database: $DBI::errstr"; $sth1 = $dbh1->prepare ( "select p.name, e.name, e.airdate, e.fsid, e.programid, e.episodeid " . "from episodes e " . " left join programs p on e.programid = p.programid " . "where p.podcastify = 't' " . " and e.grabbed = 'f'" ) or die "Cannot prepare open SQL: $DBI::errstr"; $sth1->execute or die "Cannot execute open SQL: $DBI::errstr"; while ( @row1 = $sth1->fetchrow_array ) { $program = $row1[0]; $episode = $row1[1]; $airdate = $row1[2]; $fsid = $row1[3]; $programid = $row1[4]; $episodeid = $row1[5]; $airdate =~ s/\//-/g; $airdate =~ s/ /_/g; print "$tyget -t $tivoAddress -o /tmp/file.ty -f $fsid\n"; qx{$tyget -t $tivoAddress -o /tmp/file.ty -f $fsid}; print "$tydemux -s 2 -i /tmp/file.ty -a /tmp/file.audio -v /tmp/file.video\n"; $output = qx{$tydemux -s 2 -i /tmp/file.ty -a /tmp/file.audio -v /tmp/file.video}; print "rm /tmp/file.ty\n"; qx{rm /tmp/file.ty}; $output =~ /V Sync Offset: (\d+)ms/; $syncOffset = $1; if ( $syncOffset eq "" ) { $syncOffset = 0; } print "$mplex -v 0 -o /tmp/file.mpg -f 3 -b 300 -O -$syncOffset -V /tmp/file.audio /tmp/file.video\n"; qx{$mplex -v 0 -o /tmp/file.mpg -f 3 -b 300 -O -$syncOffset -V /tmp/file.audio /tmp/file.video}; print "rm /tmp/file.audio /tmp/file.video\n"; qx{rm /tmp/file.audio /tmp/file.video}; print "$ffmpeg -threads 4 -i /tmp/file.mpg -s 320x240 -r 23.98 -pix_fmt yuv420p -vcodec xvid -deinterlace -g 300 -qmin 5 -b 1200 -async 1 -acodec aac /tmp/file.mp4\n"; qx{$ffmpeg -threads 4 -i /tmp/file.mpg -s 320x240 -r 23.98 -pix_fmt yuv420p -vcodec xvid -deinterlace -g 300 -qmin 5 -b 1200 -async 1 -acodec aac /tmp/file.mp4}; print "rm /tmp/file.mpg\n"; qx{rm /tmp/file.mpg}; if ( -e "/tmp/file.mp4" ) { $st = stat( "/tmp/file.mp4" ) or die "No /tmp/file.mp4: $!\n"; $length = $st->size; if ( $length > 0 ) { $file = fsescape( $program ) . "-" . fsescape( $episode ) . "-$fsid.mp4"; print "mv /tmp/file.mp4 $outputPath/$file\n"; qx{mv /tmp/file.mp4 $outputPath/$file}; $sth2 = $dbh2->prepare ( "update episodes set grabbed = 't', file = '$file' where episodeid = $episodeid" ) or die "Cannot prepare open SQL: $DBI::errstr"; $sth2->execute or die "Cannot execute open SQL: $DBI::errstr"; } ### if ( $length > 0 ) } #### if ( -e "/tmp/file.mp4" ) print "\n\n\n\n\n"; } sub fsescape { my $text = pop @_; $text =~ s/ /_/g; $text =~ s/\W//g; return $text; }