1 | # |
---|
2 | # $Date: 2008-02-16 19:16:13 -0600 (Sat, 16 Feb 2008) $ |
---|
3 | # $Revision: 16098 $ |
---|
4 | # $Author: xris $ |
---|
5 | # |
---|
6 | # export::mencoder::XviD |
---|
7 | # Copied from transcode.pm |
---|
8 | # and modified by Ryan Dearing <mythtv@mythtv.us> |
---|
9 | # |
---|
10 | |
---|
11 | package export::mencoder::XviD; |
---|
12 | use base 'export::mencoder'; |
---|
13 | |
---|
14 | # Load the myth and nuv utilities, and make sure we're connected to the database |
---|
15 | use nuv_export::shared_utils; |
---|
16 | use nuv_export::cli; |
---|
17 | use nuv_export::ui; |
---|
18 | use mythtv::recordings; |
---|
19 | |
---|
20 | # Load the following extra parameters from the commandline |
---|
21 | add_arg('quantisation|q=i', 'Quantisation'); |
---|
22 | add_arg('a_bitrate|a=i', 'Audio bitrate'); |
---|
23 | add_arg('v_bitrate|v=i', 'Video bitrate'); |
---|
24 | add_arg('multipass!', 'Enable two-pass encoding.'); |
---|
25 | |
---|
26 | sub new { |
---|
27 | my $class = shift; |
---|
28 | my $self = { |
---|
29 | 'cli' => qr/\bxvid\b/i, |
---|
30 | 'name' => 'Export to XviD (using mencoder)', |
---|
31 | 'enabled' => 1, |
---|
32 | 'errors' => [], |
---|
33 | 'defaults' => {}, |
---|
34 | }; |
---|
35 | bless($self, $class); |
---|
36 | |
---|
37 | # Initialize the default parameters |
---|
38 | $self->load_defaults(); |
---|
39 | |
---|
40 | # Verify any commandline or config file options |
---|
41 | die "Audio bitrate must be > 0\n" unless (!defined $self->val('a_bitrate') || $self->{'a_bitrate'} > 0); |
---|
42 | die "Video bitrate must be > 0\n" unless (!defined $self->val('v_bitrate') || $self->{'v_bitrate'} > 0); |
---|
43 | die "Width must be > 0\n" unless (!defined $self->val('width') || $self->{'width'} =~ /^\s*\D/ || $self->{'width'} > 0); |
---|
44 | die "Height must be > 0\n" unless (!defined $self->val('height') || $self->{'height'} =~ /^\s*\D/ || $self->{'height'} > 0); |
---|
45 | |
---|
46 | # VBR, multipass, etc. |
---|
47 | if ($self->val('multipass')) { |
---|
48 | $self->{'vbr'} = 1; |
---|
49 | } |
---|
50 | elsif ($self->val('quantisation')) { |
---|
51 | die "Quantisation must be a number between 1 and 31 (lower means better quality).\n" if ($self->{'quantisation'} < 1 || $self->{'quantisation'} > 31); |
---|
52 | $self->{'vbr'} = 1; |
---|
53 | } |
---|
54 | |
---|
55 | # Initialize and check for mencoder |
---|
56 | $self->init_mencoder(); |
---|
57 | |
---|
58 | # Any errors? disable this function |
---|
59 | $self->{'enabled'} = 0 if ($self->{'errors'} && @{$self->{'errors'}} > 0); |
---|
60 | # Return |
---|
61 | return $self; |
---|
62 | } |
---|
63 | |
---|
64 | # Load default settings |
---|
65 | sub load_defaults { |
---|
66 | my $self = shift; |
---|
67 | # Load the parent module's settings |
---|
68 | $self->SUPER::load_defaults(); |
---|
69 | # Not really anything to add |
---|
70 | } |
---|
71 | |
---|
72 | # Gather settings from the user |
---|
73 | sub gather_settings { |
---|
74 | my $self = shift; |
---|
75 | # Load the parent module's settings |
---|
76 | $self->SUPER::gather_settings(); |
---|
77 | # Audio Bitrate |
---|
78 | $self->{'a_bitrate'} = query_text('Audio bitrate?', |
---|
79 | 'int', |
---|
80 | $self->val('a_bitrate')); |
---|
81 | # VBR options |
---|
82 | if (!$is_cli) { |
---|
83 | $self->{'vbr'} = query_text('Variable bitrate video?', |
---|
84 | 'yesno', |
---|
85 | $self->val('vbr')); |
---|
86 | if ($self->{'vbr'}) { |
---|
87 | $self->{'multipass'} = query_text('Multi-pass (slower, but better quality)?', |
---|
88 | 'yesno', |
---|
89 | $self->val('multipass')); |
---|
90 | if (!$self->{'multipass'}) { |
---|
91 | while (1) { |
---|
92 | my $quantisation = query_text('VBR quality/quantisation (1-31)?', 'float', $self->val('quantisation')); |
---|
93 | if ($quantisation < 1) { |
---|
94 | print "Too low; please choose a number between 1 and 31.\n"; |
---|
95 | } |
---|
96 | elsif ($quantisation > 31) { |
---|
97 | print "Too high; please choose a number between 1 and 31\n"; |
---|
98 | } |
---|
99 | else { |
---|
100 | $self->{'quantisation'} = $quantisation; |
---|
101 | last; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | # Ask the user what audio and video bitrates he/she wants |
---|
107 | if ($self->{'multipass'} || !$self->{'vbr'}) { |
---|
108 | $self->{'v_bitrate'} = query_text('Video bitrate?', |
---|
109 | 'int', |
---|
110 | $self->val('v_bitrate')); |
---|
111 | } |
---|
112 | } |
---|
113 | # Query the resolution |
---|
114 | $self->query_resolution(); |
---|
115 | } |
---|
116 | |
---|
117 | sub export { |
---|
118 | my $self = shift; |
---|
119 | my $episode = shift; |
---|
120 | # Build the mencoder string |
---|
121 | my $params = " -ovc xvid -vf scale=$self->{'width'}:$self->{'height'}" |
---|
122 | #." -N 0x55" # make *sure* we're exporting mp3 audio |
---|
123 | |
---|
124 | #." -oac mp3lame -lameopts vbr=3:br=$self->{'a_bitrate'}" |
---|
125 | ; |
---|
126 | # unless ($episode->{'finfo'}{'fps'} =~ /^2(?:5|4\.9)/) { |
---|
127 | # $params .= " -J modfps=buffers=7 --export_fps 23.976"; |
---|
128 | # } |
---|
129 | # Dual pass? |
---|
130 | if ($self->{'multipass'}) { |
---|
131 | # Add the temporary file to the list |
---|
132 | push @tmpfiles, "/tmp/xvid.$$.log"; |
---|
133 | # Back up the path and use /dev/null for the first pass |
---|
134 | my $path_bak = $self->{'path'}; |
---|
135 | $self->{'path'} = '/dev/null'; |
---|
136 | # First pass |
---|
137 | print "First pass...\n"; |
---|
138 | $self->{'mencoder_xtra'} = " $params" |
---|
139 | ." -passlogfile /tmp/xvid.$$.log" |
---|
140 | ." -oac copy" |
---|
141 | ." -xvidencopts bitrate=$self->{'v_bitrate'}:pass=1:quant_type=mpeg:threads=2:keyframe_boost=10:kfthreshold=1:kfreduction=20 "; |
---|
142 | $self->SUPER::export($episode, '', 1); |
---|
143 | # Restore the path |
---|
144 | $self->{'path'} = $path_bak; |
---|
145 | # Second pass |
---|
146 | print "Final pass...\n"; |
---|
147 | $self->{'mencoder_xtra'} = " $params" |
---|
148 | ." -oac mp3lame -lameopts vbr=3:br=$self->{'a_bitrate'}" |
---|
149 | ." -passlogfile /tmp/xvid.$$.log" |
---|
150 | ." -xvidencopts bitrate=$self->{'v_bitrate'}:pass=2:quant_type=mpeg:threads=2:keyframe_boost=10:kfthreshold=1:kfreduction=20 "; |
---|
151 | } |
---|
152 | # Single pass |
---|
153 | else { |
---|
154 | $self->{'mencoder_xtra'} = " $params" |
---|
155 | ." -oac mp3lame -lameopts vbr=3:br=$self->{'a_bitrate'}"; |
---|
156 | if ($self->{'quantisation'}) { |
---|
157 | $self->{'mencoder_xtra'} .= " -xvidencopts fixed_quant=".$self->{'quantisation'}; |
---|
158 | } |
---|
159 | else { |
---|
160 | $self->{'mencoder_xtra'} .= " -xvidencopts bitrate=$self->{'v_bitrate'} "; |
---|
161 | } |
---|
162 | } |
---|
163 | # Execute the (final pass) encode |
---|
164 | $self->SUPER::export($episode, '.avi'); |
---|
165 | } |
---|
166 | |
---|
167 | 1; #return true |
---|
168 | |
---|
169 | # vim:ts=4:sw=4:ai:et:si:sts=4 |
---|