Ticket #5855: nuvexport

File nuvexport, 8.5 KB (added by David Herman <dave@…>, 15 years ago)
Line 
1#!/usr/bin/perl -w
2#
3# Export MythTV recordings into a variety of formats.
4#
5# @url       $URL$
6# @date      $Date: 2008-03-11 20:17:59 -0400 (Tue, 11 Mar 2008) $
7# @version   $Revision: 16514 $
8# @author    $Author: xris $
9# @license   GPL
10#
11
12# Version
13    $VERSION = '0.5 0.20080311.svn';
14
15# Autoflush buffers
16    $|++;
17
18# Set up the signal handlers
19    BEGIN {
20        $SIG{'INT'}  = sub { print "\n"; exit; };
21        $SIG{'QUIT'} = sub { print "\n"; exit; };
22    # Annoy people running as root
23        if ($< < 1) {
24            print "You are running nuvexport as root -- this is not advised.\nPress ENTER if you really want to do this.\n";
25            <STDIN>;
26        }
27    }
28
29# Add a couple of include paths so we can load the various export and gui modules
30    use English;
31    use File::Basename;
32    use Cwd 'abs_path';
33    use lib dirname(abs_path($0 or $PROGRAM_NAME)), '/usr/share/nuvexport', '/usr/local/share/nuvexport';
34
35# Load the MythTV object
36    use MythTV;
37    our $Myth = new MythTV();
38
39# Assign this globally so that other modules can access it
40    $Main::Myth = $Myth;
41    1 if ($Main::Myth);     # Avoid a stupid "used only once" warning
42
43# Load the myth and nuv utilities, and connect to the database
44    use Date::Manip;
45    use nuv_export::shared_utils;
46    use nuv_export::cli;
47    use nuv_export::ui;
48    use mythtv::recordings;
49
50# Make sure that we have mythtranscode installed
51    find_program('mythtranscode')
52        or die "You need mythtranscode in order to use nuvexport.\n\n";
53
54# Load the exporters based on which suite was selected above
55    if ($export_prog eq 'transcode') {
56        find_program('transcode')
57            or die "You need transcode in order to use nuvexport in --transcode mode\n";
58    # transcode - doesn't seem to work for many MPEG recordings these days
59        require export::transcode::XviD;
60        require export::transcode::SVCD;
61        require export::transcode::VCD;
62        require export::transcode::DVCD;
63        require export::transcode::DVD;
64    }
65    elsif ($export_prog eq 'ffmpeg') {
66        find_program('ffmpeg')
67            or die "You need ffmpeg in order to use nuvexport in --ffmpeg mode\n";
68    # ffmpeg - seems to work better and is the default
69        require export::ffmpeg::XviD;
70        require export::ffmpeg::SVCD;
71        require export::ffmpeg::VCD;
72        require export::ffmpeg::DVCD;
73        require export::ffmpeg::DVD;
74        require export::ffmpeg::DivX;
75        require export::ffmpeg::ASF;
76        require export::ffmpeg::MP3;
77        require export::ffmpeg::PSP;
78        require export::ffmpeg::MP4;
79    }
80    elsif ($export_prog eq 'mencoder') {
81        find_program('mencoder')
82            or die "You need mencoder in order to use nuvexport in --mencoder mode\n";
83    # ffmpeg - seems to work better and is the default
84        require export::mencoder::XviD;
85        require export::mencoder::H264;
86
87    }
88
89# Load the other export modules
90    require export::NUV_SQL;
91
92# Load the ui
93    load_cli_args();
94
95# Now that we've loaded the cli args, we can define the exporters
96    if ($export_prog eq 'transcode') {
97        push @Exporters, export::transcode::XviD->new;
98        push @Exporters, export::transcode::SVCD->new;
99        push @Exporters, export::transcode::VCD->new;
100        push @Exporters, export::transcode::DVCD->new;
101        push @Exporters, export::transcode::DVD->new;
102    }
103    elsif ($export_prog eq 'ffmpeg') {
104        push @Exporters, export::ffmpeg::XviD->new;
105        push @Exporters, export::ffmpeg::SVCD->new;
106        push @Exporters, export::ffmpeg::VCD->new;
107        push @Exporters, export::ffmpeg::DVCD->new;
108        push @Exporters, export::ffmpeg::DVD->new;
109        push @Exporters, export::ffmpeg::DivX->new;
110        push @Exporters, export::ffmpeg::ASF->new;
111        push @Exporters, export::ffmpeg::MP3->new;
112        push @Exporters, export::ffmpeg::PSP->new;
113        push @Exporters, export::ffmpeg::MP4->new;
114    }
115    elsif ($export_prog eq 'mencoder') {
116        push @Exporters, export::mencoder::XviD->new;
117        push @Exporters, export::mencoder::H264->new;
118    }
119    push @Exporters, export::NUV_SQL->new;
120
121# Show the version?
122    if (arg('version')) {
123        print "nuvexport version:  $VERSION\n";
124        exit;
125    }
126
127# Print the help - for now, this is just perldoc
128    if (defined arg('help')) {
129        require nuv_export::help;
130    }
131
132# Load the recordings, and the $video_dir variable
133    load_recordings($Myth);
134
135# Which exporter to use
136    if (arg('only_save_info')) {
137        $exporter = export::NUV_SQL->new;
138    }
139    else {
140        $exporter = query_exporters($export_prog);
141    }
142
143# Load episodes from the commandline (and display/quit if this is search-only)
144    @episodes = load_cli_episodes();
145
146# Gather the episodes we want to transcode (unless we have them from the cli already)
147    @episodes = &load_episodes(@episodes) unless ($is_cli);
148
149# Gather data for this exporter
150    $exporter->gather_settings();
151
152# Encode right here?
153    if (arg('noserver')) {
154        foreach my $episode (@episodes) {
155        # Save the info?
156            if (arg('save_info') || arg('only_save_info')) {
157                my $outfile = $exporter->get_outfile($episode, '.txt');
158                open(DATA, ">$outfile") or die "Can't create $outfile:  $!\n";
159                print DATA join("\n", 'title:       '.$episode->{'title'},
160                                      'subtitle:    '.($episode->{'subtitle'}    eq 'Untitled'       ? '' : $episode->{'subtitle'}),
161                                      'description: '.($episode->{'description'} eq 'No Description' ? '' : $episode->{'description'}),
162                                      'starttime:   '.UnixDate("epoch $episode->{'starttime'}", '%O'),
163                                      'endtime:     '.UnixDate("epoch $episode->{'endtime'}", '%O'),
164                                      'category:    '.$episode->{'category'},
165                                      'chanid:      '.$episode->{'chanid'},
166                                      'channum:     '.$episode->{'channum'},
167                                      'callsign:    '.$episode->{'callsign'},
168                                      'channame:    '.$episode->{'channame'},
169                                      'airdate:     '.(UnixDate('epoch '.$episode->{'airdate'}, '%O') or $episode->{'airdate'}),
170                                      'stars:       '.(4 * $episode->{'stars'}),
171                                      'recgroup:    '.$episode->{'recgroup'},
172                                      'seriesid:    '.$episode->{'seriesid'},
173                                      'programid:   '.$episode->{'programid'},
174                                      ''
175                               );
176                close DATA;
177            # Skip ahead if we're not supposed to actually export recordings
178                next if (arg('only_save_info'));
179            }
180        # Keep track of when we started
181            if ($DEBUG) {
182                print "\n--------------------------------",
183                      "\nTo encode:  ", $episode->{'title'};
184                print ':  ', $episode->{'subtitle'} if ($episode->{'subtitle'});
185                print "\nUse the following commands:\n";
186            }
187            else {
188                print "\nNow encoding:  ", $episode->{'title'};
189                print ':  ', $episode->{'subtitle'} if ($episode->{'subtitle'});
190                print "\nEncode started:  ".localtime()."\n";
191            }
192            my $start = time();
193        # Encode
194            $exporter->export($episode);
195        # Remove tmpfiles
196            wipe_tmpfiles();
197        # Report how long the encode lasted
198            print "\nEncode finished:  ".localtime()."\n" unless ($DEBUG);
199            my $seconds = time() - $start;
200            my $timestr = '';
201        # How many hours?
202            my $hours = int($seconds / 3600);
203            $timestr .= $hours.'h ' if ($hours > 0);
204            $seconds  = $seconds % 3600;
205        # Minutes
206            my $minutes = int($seconds / 60);
207            $timestr .= $minutes.'m ' if ($minutes > 0);
208            $seconds  = $seconds % 60;
209        # Generate a nice time string
210            $timestr .= $seconds.'s' if ($seconds > 0 || $timestr eq '');
211        # Notify the user
212            print "Encode lasted: $timestr\n" unless ($DEBUG);
213        }
214    }
215# Store in the DB
216    else {
217    }
218
219# Exit gracefully
220    exit;
221
222# Escape a string for db insertion
223    sub mysql_escape {
224        $string = shift;
225        return 'NULL' unless (defined $string);
226        $string =~ s/'/\\'/sg;
227        return "'$string'";
228    }
229
230# vim:ts=4:sw=4:ai:et:si:sts=4