summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/nuvexport/XviD.pm
blob: c4700f58b7151d1bb19ad376ea8fe800bb402ec2 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#
# ffmpeg-based XviD export module for nuvexport.
#
# @url       $URL: svn+ssh://xris@svn.mythtv.org/var/lib/svn/trunk/mythextras/nuvexport/export/ffmpeg/XviD.pm $
# @date      $Date: 2008-02-19 20:47:06 -0800 (Tue, 19 Feb 2008) $
# @version   $Revision: 16175 $
# @author    $Author: xris $
# @copyright Silicon Mechanics
#

package export::ffmpeg::XviD;
    use base 'export::ffmpeg';

# Load the myth and nuv utilities, and make sure we're connected to the database
    use nuv_export::shared_utils;
    use nuv_export::cli;
    use nuv_export::ui;
    use mythtv::recordings;

# Load the following extra parameters from the commandline
    add_arg('quantisation|q=i', 'Quantisation');
    add_arg('a_bitrate|a=i',    'Audio bitrate');
    add_arg('v_bitrate|v=i',    'Video bitrate');
    add_arg('multipass!',       'Enable two-pass encoding.');

    sub new {
        my $class = shift;
        my $self  = {
                     'cli'      => qr/\bxvid\b/i,
                     'name'     => 'Export to XviD',
                     'enabled'  => 1,
                     'errors'   => [],
                     'defaults' => {},
                    };
        bless($self, $class);

    # Initialize the default parameters
        $self->load_defaults();

    # Verify any commandline or config file options
        die "Audio bitrate must be > 0\n" unless (!defined $self->val('a_bitrate') || $self->{'a_bitrate'} > 0);
        die "Video bitrate must be > 0\n" unless (!defined $self->val('v_bitrate') || $self->{'v_bitrate'} > 0);
        die "Width must be > 0\n"         unless (!defined $self->val('width')     || $self->{'width'} =~ /^\s*\D/  || $self->{'width'}  > 0);
        die "Height must be > 0\n"        unless (!defined $self->val('height')    || $self->{'height'} =~ /^\s*\D/ || $self->{'height'} > 0);

    # VBR, multipass, etc.
        if ($self->val('multipass')) {
            $self->{'vbr'} = 1;
        }
        elsif ($self->val('quantisation')) {
            die "Quantisation must be a number between 1 and 31 (lower means better quality).\n" if ($self->{'quantisation'} < 1 || $self->{'quantisation'} > 31);
            $self->{'vbr'} = 1;
        }

    # Initialize and check for ffmpeg
        $self->init_ffmpeg();

    # Can we even encode xvid?
        if (!$self->can_encode('xvid') && !$self->can_encode('libxvid')) {
            push @{$self->{'errors'}}, "Your ffmpeg installation doesn't support encoding to xvid.\n"
                                      ."  (It must be compiled with the --enable-libxvid option)";
        }
        if (!$self->can_encode('mp3') && !$self->can_encode('libmp3lame')) {
            push @{$self->{'errors'}}, "Your ffmpeg installation doesn't support encoding to mp3 audio.";
        }

    # Any errors?  disable this function
        $self->{'enabled'} = 0 if ($self->{'errors'} && @{$self->{'errors'}} > 0);
    # Return
        return $self;
    }

# Load default settings
    sub load_defaults {
        my $self = shift;
    # Load the parent module's settings
        $self->SUPER::load_defaults();
    # Default bitrates and resolution
        $self->{'defaults'}{'a_bitrate'} = 128;
        $self->{'defaults'}{'v_bitrate'} = 960;
        $self->{'defaults'}{'width'}     = 624;
    }

# Gather settings from the user
    sub gather_settings {
        my $self = shift;
    # Load the parent module's settings
        $self->SUPER::gather_settings();
    # Audio Bitrate
        $self->{'a_bitrate'} = query_text('Audio bitrate?',
                                          'int',
                                          $self->val('a_bitrate'));
    # VBR options
        if (!$is_cli) {
            $self->{'vbr'} = query_text('Variable bitrate video?',
                                        'yesno',
                                        $self->val('vbr'));
            if ($self->{'vbr'}) {
                $self->{'multipass'} = query_text('Multi-pass (slower, but better quality)?',
                                                  'yesno',
                                                  $self->val('multipass'));
                if (!$self->{'multipass'}) {
                    while (1) {
                        my $quantisation = query_text('VBR quality/quantisation (1-31)?',
                                                      'float',
                                                      $self->val('quantisation'));
                        if ($quantisation < 1) {
                            print "Too low; please choose a number between 1 and 31.\n";
                        }
                        elsif ($quantisation > 31) {
                            print "Too high; please choose a number between 1 and 31\n";
                        }
                        else {
                            $self->{'quantisation'} = $quantisation;
                            last;
                        }
                    }
                }
            } else {
                $self->{'multipass'} = 0;
            }
        # Ask the user what video bitrate he/she wants
            $self->{'v_bitrate'} = query_text('Video bitrate?',
                                              'int',
                                              $self->val('v_bitrate'));
        }
    # Query the resolution
        $self->query_resolution();
    }

    sub export {
        my $self    = shift;
        my $episode = shift;
    # Make sure we have the framerate
        $self->{'out_fps'} = $episode->{'finfo'}{'fps'};
    # Embed the title
        $safe_title = $episode->{'title'};
        if ($episode->{'subtitle'} ne 'Untitled') {
            $safe_title .= ' - '.$episode->{'subtitle'};
        }
        my $safe_title = shell_escape($safe_title);
    # Codec name changes between ffmpeg versions
        my $codec = $self->can_encode('libxvid') ? 'libxvid' : 'xvid';
    # Build the common ffmpeg string
        my $ffmpeg_xtra  = ' -vcodec '.$codec
                          .$self->param('bit_rate', $self->{'v_bitrate'})
                          .($self->{'vbr'}
                            ? $self->param('rc_min_rate', 32)
                             . $self->param('rc_max_rate', (2 * $self->{'v_bitrate'}))
                             . $self->param('bit_rate_tolerance', 32)
                             . ' -bufsize 65535'
                            : '')
                          .' -flags +mv4+loop+cgop'
                          .' -trellis 1'
			  ### .' -aic 1'
                          .' -mbd 1'
                          .' -cmp 2 -subcmp 2'
			  ### .' -cgop 1'
                           .$self->param('b_quant_factor',     150)
                           .$self->param('b_quant_offset',     100)
                           .$self->param('max_b_frames',       1)
                          ;
    # Dual pass?
        if ($self->{'multipass'}) {
        # Add the temporary file to the list
            push @tmpfiles, "/tmp/xvid.$$.log";
        # First pass
            print "First pass...\n";
            $self->{'ffmpeg_xtra'} = $ffmpeg_xtra
                                    ." -pass 1 -passlogfile '/tmp/xvid.$$.log'"
                                    .' -f avi';
            $self->SUPER::export($episode, '', 1);
        # Second pass
            print "Final pass...\n";
            $self->{'ffmpeg_xtra'} = $ffmpeg_xtra
                                   . " -pass 2 -passlogfile '/tmp/xvid.$$.log'";
        }
    # Single Pass
        else {
            $self->{'ffmpeg_xtra'} = $ffmpeg_xtra
                                    .($self->{'vbr'}
                                      ? ' -qmax 31 -qmin '.$self->{'quantisation'}
                                      : '');
        }
    # Don't forget the audio, etc.
        $self->{'ffmpeg_xtra'} .= ' -acodec '
                                 .($self->can_encode('libmp3lame') ? 'libmp3lame' : 'mp3')
                                 .' -async 1 '
                                 .$self->param('ab', $self->{'a_bitrate'})
                                 .' -f avi';
    # Execute the (final pass) encode
        $self->SUPER::export($episode, '.avi');
    }

1;  #return true

# vim:ts=4:sw=4:ai:et:si:sts=4