~ubuntu-branches/ubuntu/precise/mythexport/precise

« back to all changes in this revision

Viewing changes to usr/bin/mythexport-daemon

  • Committer: Bazaar Package Importer
  • Author(s): John Baab
  • Date: 2010-08-13 01:53:54 UTC
  • mfrom: (1.1.16 upstream)
  • Revision ID: james.westby@ubuntu.com-20100813015354-h2w1q6caq1mlv5vo
Tags: 2.2.0-0ubuntu1
* FFe (LP: #617570).
* Added HTML5 streaming page to web interface.
* New device config method. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/perl
2
 
# mythexport-daemon v2.0
 
2
# mythexport-daemon v2.1
3
3
# By: John Baab
4
4
# Email: rhpot1991@ubuntu.com
5
5
# Purpose: daemon for exporting mythtv recordings into formats used by portable devices.
6
 
# Requirements: ffmpeg (with aac,xvid,h264 support), perl and the DBI & DBD::mysql modules, 
7
 
#                    MythTV perl bindings, AtomicParsley
 
6
# Requirements: perl and the DBI & DBD::mysql modules, MythTV perl bindings, AtomicParsley
8
7
#
9
8
# License:
10
9
#
41
40
use XML::Writer;
42
41
use IO::File;
43
42
 
 
43
use lib '/usr/share/mythexport';
 
44
use lib '/usr/share/mythexport/configs';
 
45
 
44
46
&startDaemon;
45
47
 
46
48
my $debug = "";
122
124
 
123
125
sub createXML($){
124
126
    my @params = split('&',$_[0]);
125
 
    my ($exportdir, @starttime, @chanid, $block, $sql_where) = "";
 
127
    my ($exportdir, @starttime, @chanid, $config, $sql_where) = "";
126
128
 
127
129
    foreach (@params){
128
130
        if ($_ =~ m/exportdir/) {
134
136
        elsif ($_ =~ m/chanid/) {
135
137
            @chanid = split('\|',(split(/\=/,$_))[1]);
136
138
        }
137
 
        elsif ($_ =~ m/block/) {
138
 
            $block = (split(/\=/,$_))[1];
 
139
        elsif ($_ =~ m/config/) {
 
140
            $config = (split(/\=/,$_))[1];
139
141
        }
140
142
    }
141
143
    
143
145
    logdebug("exportdir = $exportdir");
144
146
    logdebug("starttime = @starttime");
145
147
    logdebug("chanid = @chanid");
146
 
    logdebug("config = $block");
 
148
    logdebug("config = $config");
147
149
    
148
150
    $exportdir =~ s/\/$//;
149
151
    
150
152
    my $export_cfg = new Config::Simple();
151
153
    $export_cfg->read('/etc/mythtv/mythexport/mythexport_settings.cfg') || logerror("Cannot read config file: /etc/mythtv/mythexport/mythexport_settings.cfg");
 
154
 
 
155
    require "$config.pm";
 
156
 
 
157
    my $object = new $config();
 
158
    my $extension = $object->Extension;
152
159
    
153
 
    my $exportdevice = $export_cfg->param("$block.device") unless ref $export_cfg->param("$block.device") eq 'ARRAY';
154
 
    my $extension = $export_cfg->param("$block.extension") unless ref $export_cfg->param("$block.extension") eq 'ARRAY';
 
160
    #my $exportdevice = $export_cfg->param("$block.device") unless ref $export_cfg->param("$block.device") eq 'ARRAY';
 
161
    #my $extension = $export_cfg->param("$block.extension") unless ref $export_cfg->param("$block.extension") eq 'ARRAY';
155
162
    
156
163
    #test that the directory has the correct permissions
157
164
    -w $exportdir || logerror("ERROR: Directory $exportdir is not writeable.\n");
174
181
    logdebug("query = $query");
175
182
    $query_handle->execute() || logerror("Unable to query mythexport table");
176
183
 
177
 
    if ($extension eq ""){
178
 
        if ($exportdevice eq "mp3"){
179
 
            $extension .= ".mp3";
180
 
        }
181
 
        elsif ($exportdevice eq "archos"){
182
 
            $extension .= ".avi";
183
 
        }
184
 
        else{
185
 
            $extension .= ".mp4";
186
 
        }
187
 
    }
 
184
    #if ($extension eq ""){
 
185
    #    if ($exportdevice eq "mp3"){
 
186
     #       $extension .= ".mp3";
 
187
      #  }
 
188
       # elsif ($exportdevice eq "archos"){
 
189
        #    $extension .= ".avi";
 
190
        #}
 
191
        #else{
 
192
        #    $extension .= ".mp4";
 
193
        #}
 
194
    #}
188
195
    
189
196
    $log->notice("Creating xml file");
190
197
    while ( my ($title,$subtitle,$description,$syndicatedepisodenumber,$showtype,$programid,$basename,$currentchanid,$currentstarttime) = $query_handle->fetchrow_array() ) {
231
238
        $writer->endTag("item");
232
239
 
233
240
        $log->notice("Creating mysql dump");
234
 
        export("starttime=$currentstarttime&chanid=currentchanid&config=$block&otg=true");
 
241
        export("starttime=$currentstarttime&chanid=currentchanid&config=$config&otg=true");
235
242
    }
236
243
    $writer->endTag("channel");
237
244
    $writer->end();
242
249
 
243
250
sub createSQL($){
244
251
    my @params = split('&',$_[0]);
245
 
    my ($exportdir, @starttime, @chanid, $block, $sql_where) = "";
 
252
    my ($exportdir, @starttime, @chanid, $config, $sql_where) = "";
246
253
    
247
254
    foreach (@params){
248
255
        if ($_ =~ m/exportdir/) {
254
261
        elsif ($_ =~ m/chanid/) {
255
262
            @chanid = split('\|',(split(/\=/,$_))[1]);
256
263
        }
257
 
        elsif ($_ =~ m/block/) {
258
 
            $block = (split(/\=/,$_))[1];
 
264
        elsif ($_ =~ m/config/) {
 
265
            $config = (split(/\=/,$_))[1];
259
266
        }
260
267
    }
261
268
    
263
270
    logdebug("exportdir = $exportdir");
264
271
    logdebug("starttime = @starttime");
265
272
    logdebug("chanid = @chanid");
266
 
    logdebug("config = $block");
 
273
    logdebug("config = $config");
267
274
    
268
275
    $exportdir =~ s/\/$//;
269
276
    
323
330
 
324
331
sub export($){
325
332
    my @params = split("&",$_[0]);
326
 
    my ($debug,$starttime,$chanid,$block,$otg) = "";
 
333
    my ($debug,$starttime,$chanid,$config,$deleteperiod,$otg,$podcastname) = "";
327
334
    
328
335
    foreach (@params){
329
336
        if ($_ =~ m/starttime/) {
333
340
            $chanid = (split(/\=/,$_))[1];
334
341
        }
335
342
        elsif ($_ =~ m/config/) {
336
 
            $block = (split(/\=/,$_))[1];
 
343
            $config = (split(/\=/,$_))[1];
 
344
        }
 
345
        elsif ($_ =~ m/deleteperiod/) {
 
346
            $deleteperiod = (split(/\=/,$_))[1];
337
347
        }
338
348
        elsif ($_ =~ m/otg/) {
339
 
            $block = (split(/\=/,$_))[1];
 
349
            $otg = (split(/\=/,$_))[1];
 
350
        }
 
351
        elsif ($_ =~ m/podcastname/) {
 
352
            $podcastname = (split(/\=/,$_))[1];
340
353
        }
341
354
    }
342
355
 
343
 
    my $export_cfg = new Config::Simple();
344
 
    $export_cfg->read('/etc/mythtv/mythexport/mythexport_settings.cfg') || logerror("Cannot read config file: /etc/mythtv/mythexport/mythexport_settings.cfg");
345
 
    my $delete_period = 30;
346
 
 
347
 
    my $exportdevice = $export_cfg->param("$block.device") unless ref $export_cfg->param("$block.device") eq 'ARRAY';
348
 
    my $exportcodec = $export_cfg->param("$block.codec") unless ref $export_cfg->param("$block.codec") eq 'ARRAY';
349
 
    $delete_period = $export_cfg->param("$block.deletePeriod") unless ref $export_cfg->param("$block.deletePeriod") eq 'ARRAY';
350
 
    my $podcast_name = $export_cfg->param("$block.podcastName") unless ref $export_cfg->param("$block.podcastName") eq 'ARRAY';
351
 
    my $args = $export_cfg->param("$block.ffmpegArgs") unless ref $export_cfg->param("$block.ffmpegArgs") eq 'ARRAY';
352
 
    my $removeCommercials = $export_cfg->param("$block.removeCommercials") unless ref $export_cfg->param("$block.removeCommercials") eq 'ARRAY';
353
 
    my $extension = $export_cfg->param("$block.extension") unless ref $export_cfg->param("$block.extension") eq 'ARRAY';
354
 
 
355
 
    my @args = split('\|',$args);
 
356
    #my $export_cfg = new Config::Simple();
 
357
    #$export_cfg->read('/etc/mythtv/mythexport/mythexport_settings.cfg') || logerror("Cannot read config file: /etc/mythtv/mythexport/mythexport_settings.cfg");
 
358
    #my $delete_period = 30;
 
359
 
 
360
    #my $exportdevice = $export_cfg->param("$block.device") unless ref $export_cfg->param("$block.device") eq 'ARRAY';
 
361
    #my $exportcodec = $export_cfg->param("$block.codec") unless ref $export_cfg->param("$block.codec") eq 'ARRAY';
 
362
    #$delete_period = $export_cfg->param("$block.deletePeriod") unless ref $export_cfg->param("$block.deletePeriod") eq 'ARRAY';
 
363
    #my $podcast_name = $export_cfg->param("$block.podcastName") unless ref $export_cfg->param("$block.podcastName") eq 'ARRAY';
 
364
    #my $args = $export_cfg->param("$block.ffmpegArgs") unless ref $export_cfg->param("$block.ffmpegArgs") eq 'ARRAY';
 
365
    #my $removeCommercials = $export_cfg->param("$block.removeCommercials") unless ref $export_cfg->param("$block.removeCommercials") eq 'ARRAY';
 
366
    #my $extension = $export_cfg->param("$block.extension") unless ref $export_cfg->param("$block.extension") eq 'ARRAY';
 
367
 
 
368
    #my @args = split('\|',$args);
356
369
    
357
370
    my ($title, $subtitle, $description, $syndicatedepisodenumber, $showtype, $programid, $basename, $airdate) = "";
358
371
    
366
379
    logdebug("exportdir = $exportdir");
367
380
    logdebug("starttime = $starttime");
368
381
    logdebug("chanid = $chanid");
369
 
    logdebug("config = $block");
 
382
    logdebug("config = $config");
370
383
 
371
384
    #test that the directory has the correct permissions
372
385
    -w $exportdir || logerror("ERROR: Directory $exportdir is not writeable.\n");
439
452
    # move to the export directory incase any logs get written
440
453
    chdir $exportdir;
441
454
 
442
 
    my $symlinkCmd = "";
443
 
    my $APcommand = "";
 
455
    #my $symlinkCmd = "";
 
456
    #my $APcommand = "";
444
457
 
445
 
    my $transcodeTemp;
446
 
    if ($removeCommercials){
 
458
    # move this to the new modules???
 
459
    #my $transcodeTemp;
 
460
    #if ($removeCommercials){
447
461
        # First generate the cutlist from the flagged commercials
448
 
        logdebug("command = nice -n19 mythcommflag --chanid $chanid --starttime $starttime --gencutlist 2>&1");
449
 
        system("nice -n19 mythcommflag --chanid $chanid --starttime $starttime --gencutlist 2>&1") == 0 || logerror("ERROR: error generating cutlist for commercial removal");
 
462
    #    logdebug("command = nice -n19 mythcommflag --chanid $chanid --starttime $starttime --gencutlist 2>&1");
 
463
    #    system("nice -n19 mythcommflag --chanid $chanid --starttime $starttime --gencutlist 2>&1") == 0 || logerror("ERROR: error generating cutlist for commercial removal");
450
464
 
451
465
        # Now transcode using that cutlist
452
 
        logdebug("command = nice -n19 mythtranscode --mpeg2 --honorcutlist --showprogress --chanid $chanid --starttime $starttime 2>&1");
453
 
        system("nice -n19 mythtranscode --mpeg2 --honorcutlist --showprogress --chanid $chanid --starttime $starttime 2>&1") == 0 || logerror("ERROR: error removing commercials");
454
 
 
455
 
        $transcodeTemp = "$filename.tmp";
456
 
        $filename = $transcodeTemp;
457
 
    }
458
 
 
459
 
    if ($exportdevice eq "symlink"){
460
 
        $symlinkCmd = "ln -s $filename $newfilename";
461
 
    }
462
 
    elsif ($extension ne ""){
463
 
        $newfilename .= $extension;
464
 
        $webfilename .= $extension;
465
 
    }
466
 
    elsif ($exportdevice eq "mp3"){
467
 
        $newfilename .= ".mp3";
468
 
        $webfilename .= ".mp3";
469
 
    }
470
 
    elsif ($exportdevice eq "archos"){
471
 
        $newfilename .= ".avi";
472
 
        $webfilename .= ".avi";
473
 
    }
474
 
    else{
475
 
        $newfilename .= ".mp4";
476
 
        $webfilename .= ".mp4";
477
 
    }
 
466
    #    logdebug("command = nice -n19 mythtranscode --mpeg2 --honorcutlist --showprogress --chanid $chanid --starttime $starttime 2>&1");
 
467
    #    system("nice -n19 mythtranscode --mpeg2 --honorcutlist --showprogress --chanid $chanid --starttime $starttime 2>&1") == 0 || logerror("ERROR: error removing commercials");
 
468
 
 
469
    #    $transcodeTemp = "$filename.tmp";
 
470
    #    $filename = $transcodeTemp;
 
471
    #}
 
472
 
 
473
    #if ($exportdevice eq "symlink"){
 
474
    #    $symlinkCmd = "ln -s $filename $newfilename";
 
475
    #}
 
476
    #elsif ($extension ne ""){
 
477
    #    $newfilename .= $extension;
 
478
    #    $webfilename .= $extension;
 
479
    #}
 
480
    #elsif ($exportdevice eq "mp3"){
 
481
    #    $newfilename .= ".mp3";
 
482
    #    $webfilename .= ".mp3";
 
483
    #}
 
484
    #elsif ($exportdevice eq "archos"){
 
485
    #    $newfilename .= ".avi";
 
486
    #    $webfilename .= ".avi";
 
487
    #}
 
488
    #else{
 
489
    #    $newfilename .= ".mp4";
 
490
    #    $webfilename .= ".mp4";
 
491
    #}
478
492
    
479
 
    foreach (@args){
480
 
        if($_ =~ m/\/dev\/null/){
481
 
            $_ = "nice -n19 ffmpeg -i $filename $_";
482
 
        }
483
 
        else{
484
 
            $_ = "nice -n19 ffmpeg -i $filename $_ '$newfilename' 2>&1";
485
 
        }
486
 
    }
 
493
    #foreach (@args){
 
494
    #    if($_ =~ m/\/dev\/null/){
 
495
    #        $_ = "nice -n19 ffmpeg -i $filename $_";
 
496
    #    }
 
497
    #    else{
 
498
    #        $_ = "nice -n19 ffmpeg -i $filename $_ '$newfilename' 2>&1";
 
499
    #    }
 
500
    #}
487
501
 
488
502
    # Run the commands
489
 
    foreach (@args){
490
 
        $log->notice("Exporting $newfilename");
491
 
        logdebug("command = $_");
492
 
        system($_) == 0|| logerror("ERROR: $_ FAILED/");
493
 
    }
494
 
 
495
 
    if ($removeCommercials){
496
 
        unlink($transcodeTemp) || logerror("ERROR: cannot remove $transcodeTemp.");
497
 
    }
498
 
 
499
 
    # only continue if ffmpeg didn't fail
500
 
    if (-e $newfilename){
501
 
 
502
 
        if (!$debug && @args > 1){
 
503
    #foreach (@args){
 
504
    #    $log->notice("Exporting $newfilename");
 
505
    #    logdebug("command = $_");
 
506
    #    system($_) == 0|| logerror("ERROR: $_ FAILED/");
 
507
    #}
 
508
 
 
509
    #if ($removeCommercials){
 
510
    #    unlink($transcodeTemp) || logerror("ERROR: cannot remove $transcodeTemp.");
 
511
    #}
 
512
 
 
513
    require "$config.pm";
 
514
 
 
515
    my $object = new $config($filename, $newfilename);
 
516
    my $extension = $object->Extension();
 
517
    $object->export();
 
518
 
 
519
    my $returnCode = $object->checkOutput();
 
520
 
 
521
    # only continue if encoding didn't fail
 
522
    if ($returnCode == 1){
 
523
 
 
524
        if (!$debug){
503
525
            # clean up any log files left behind by ffmpeg
504
526
            system "rm *.log";
505
527
        }
506
528
 
507
529
        # Update mp4 info using AtomicParsley
508
530
        # need to fix this so movies actually show up as movies.
509
 
        if ($newfilename eq ".mp4" && $webfilename eq ".mp4"){
510
 
            $APcommand = "AtomicParsley '$newfilename' --genre \"TV Shows\" --stik \"TV Show\" --TVNetwork $channame --TVShowName \"$title_old\" --TVEpisode \"$programid\" --TVEpisodeNum $episodenumber --TVSeason $seasonnumber --description \"$description\" --title \"$subtitle_old\" 2>&1";
511
 
        }
 
531
        #if ($newfilename eq ".mp4" && $webfilename eq ".mp4"){
 
532
        #    $APcommand = "AtomicParsley '$newfilename' --genre \"TV Shows\" --stik \"TV Show\" --TVNetwork $channame --TVShowName \"$title_old\" --TVEpisode \"$programid\" --TVEpisodeNum $episodenumber --TVSeason $seasonnumber --description \"$description\" --title \"$subtitle_old\" 2>&1";
 
533
        #}
512
534
 
513
 
        if($APcommand){
514
 
           logdebug("command = $APcommand");
515
 
           system($APcommand) == 0 || logerror("ERROR: $APcommand FAILED");  
516
 
           system "rm $newfilename";
517
 
           system "mv $exportdir/*temp* $newfilename";
518
 
        }
519
 
        if($symlinkCmd){
520
 
            system($symlinkCmd) == 0 || logerror("ERROR: $symlinkCmd FAILED");    
521
 
        }
522
 
        (-e $newfilename) || logerror("ERROR: $newfilename is not available");    
 
535
        #if($APcommand){
 
536
        #   logdebug("command = $APcommand");
 
537
        #   system($APcommand) == 0 || logerror("ERROR: $APcommand FAILED");  
 
538
        #   system "rm $newfilename";
 
539
        #   system "mv $exportdir/*temp* $newfilename";
 
540
        #}
 
541
        #if($symlinkCmd){
 
542
        #    system($symlinkCmd) == 0 || logerror("ERROR: $symlinkCmd FAILED");    
 
543
        #}
 
544
        #(-e $newfilename) || logerror("ERROR: $newfilename is not available");    
523
545
        
524
546
        if($otg ne "true"){
525
547
            # Save Data
526
548
            $query = "INSERT into mythexport VALUES(NULL,?,?,?,?,NOW(),DATE_ADD(NOW(),INTERVAL ? DAY),?,?)";
527
549
            $query_handle = $connect->prepare($query);
528
550
            logdebug("query = $query");
529
 
            $query_handle->execute($webfilename,$title_old,$subtitle,$description,$delete_period,$airdate,$podcast_name) || logerror("ERROR: Unable to update table.");
 
551
            $query_handle->execute("$webfilename$extension",$title_old,$subtitle,$description,$deleteperiod,$airdate,$podcastname) || logerror("ERROR: Unable to update table.");
530
552
        }
531
553
    }
532
554
    else{