/
export_DivX.pm
128 lines (120 loc) · 4.41 KB
/
export_DivX.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package export_DivX;
# Load the nuv utilities
use nuv_utils;
# Make sure we have pointers to the main:: namespace for certain variables
*Prog = *main::Prog;
*gui = *main::gui;
sub new {
my $class = shift;
my $self = {
'name' => 'Export DivX',
'enabled' => 1,
'errors' => undef,
'episode' => undef,
'savepath' => '.',
'outfile' => 'out.avi',
'fifodir' => "/tmp/fifodir.$$",
'use_cutlist' => 0,
'a_bitrate' => 64,
'v_bitrate' => 256,
'h_res' => 320,
'v_res' => 240,
'sql_file' => undef,
@_ #allows user-specified attributes to override the defaults
};
bless($self, $class);
# Return
return $self;
}
sub gather_data {
my $self = shift;
my $default_filename;
# Get the save path
$self->{savepath} = $gui->query_savepath();
# Ask the user for the filename
if($self->{episode}->{show_name} ne 'Untitled' and $self->{episode}->{title} ne 'Untitled')
{
$default_filename = $self->{episode}->{show_name}.' - '.$self->{episode}->{title};
}
elsif($self->{episode}->{show_name} ne 'Untitled')
{
$default_filename = $self->{episode}->{show_name};
}
elsif($self->{episode}->{title} ne 'Untitled')
{
$default_filename = $self->{episode}->{title};
}
$self->{outfile} = $gui->query_filename($default_filename, 'avi', $self->{savepath});
# Ask the user if he/she wants to use the cutlist
if ($self->{episode}->{cutlist} && $self->{episode}->{cutlist} =~ /\d/) {
$self->{use_cutlist} = $gui->query_text('Enable Myth cutlist?',
'yesno',
$self->{use_cutlist} ? 'Yes' : 'No');
}
else {
$gui->notify('No cutlist found. Hopefully this means that you already removed the commercials.');
}
# Ask the user what audio bitrate he/she wants
my $a_bitrate = $gui->query_text('Audio bitrate?',
'int',
$self->{a_bitrate});
$self->{a_bitrate} = $a_bitrate;
# Ask the user what video bitrate he/she wants
my $v_bitrate = $gui->query_text('Video bitrate?',
'int',
$self->{v_bitrate});
$self->{v_bitrate} = $v_bitrate;
# Ask the user what horiz res he/she wants
my $h_res = $gui->query_text('Horizontal resolution?', 'int', $self->{h_res});
$self->{h_res} = $h_res;
# Ask the user what vert res he/she wants
my $v_res = $gui->query_text('Vertical resolution?', 'int', $self->{v_res});
$self->{v_res} = $v_res;
}
sub execute {
my $self = shift;
# make sure that the fifo dir is clean
if (-e "$self->{fifodir}/vidout" || -e "$self->{fifodir}/audout") {
die "Possibly stale mythtranscode fifo's in fifodir.\nPlease remove them before running nuvexport.\n\n";
}
# Gather any necessary data
$self->{episode} = shift;
$self->gather_data;
# Load nuv info
my %nuv_info = nuv_info($self->{episode}->{filename});
# Set this to true so that the cleanup routine actually runs
$self->{started} = 1;
# Create a directory for mythtranscode's fifo's
unless (-d $self->{fifodir}) {
mkdir($self->{fifodir}, 0755) or die "Can't create $self->{fifodir}: $!\n\n";
}
# Here, we have to fork off a copy of mythtranscode
my $command = "nice -n 19 mythtranscode -p autodetect -c $self->{episode}->{channel} -s $self->{episode}->{start_time_sep} -f $self->{fifodir}";
$command .= ' --honorcutlist' if ($self->{use_cutlist});
push @{$self->{children}}, fork_command($command);
fifos_wait($self->{fifodir});
# Now we fork off a process to encode everything
$safe_outfile = shell_escape($self->{outfile});
$command = "nice -n 19 ffmpeg -y -f s16le -ar $nuv_info{audio_sample_rate} -ac 2 -i $self->{fifodir}/audout -f rawvideo -s $nuv_info{width}x$nuv_info{height} -r $nuv_info{fps} -i $self->{fifodir}/vidout -b $self->{v_bitrate} -ab $self->{a_bitrate} -s $self->{h_res}x$self->{v_res} $safe_outfile";
push @{$self->{children}}, fork_command($command);
# Wait for child processes to finish
1 while (wait > 0);
$self->{children} = undef;
}
sub cleanup {
my $self = shift;
return unless ($self->{started});
# Make sure any child processes also go away
if ($self->{children} && @{$self->{children}}) {
foreach my $child (@{$self->{children}}) {
kill('INT', $child);
}
1 while (wait > 0);
}
# Remove any temporary files
foreach my $file ("$self->{fifodir}/audout", "$self->{fifodir}/vidout") {
unlink $file if (-e $file);
}
rmdir $self->{fifodir} if (-e $self->{fifodir});
}
1; #return true