Skip to content

Commit a35ea51

Browse files
committed
Add initial support for ffmpeg renaming a bunch of their parameters, fix some stuff for mp4 title generation and other minor mp4 cleanup
1 parent 388881c commit a35ea51

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

trunk/export/ffmpeg.pm

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ package export::ffmpeg;
3838
sub init_ffmpeg {
3939
my $self = shift;
4040
my $audioonly = (shift or 0);
41+
# Temp var
42+
my $data;
4143
# Make sure we have ffmpeg
4244
my $ffmpeg = find_program('ffmpeg')
4345
or push @{$self->{'errors'}}, 'You need ffmpeg to use this exporter.';
@@ -46,7 +48,7 @@ package export::ffmpeg;
4648
or push @{$self->{'errors'}}, 'You need yuvdenoise (part of mjpegtools) to use this exporter.';
4749
# Check the yuvdenoise version
4850
if (!defined $self->{'denoise_vmaj'}) {
49-
my $data = `cat /dev/null | yuvdenoise 2>&1`;
51+
$data = `cat /dev/null | yuvdenoise 2>&1`;
5052
if ($data =~ m/yuvdenoise version (\d+(?:\.\d+)?)(\.\d+)?/i) {
5153
$self->{'denoise_vmaj'} = $1;
5254
$self->{'denoise_vmin'} = $2;
@@ -84,8 +86,16 @@ package export::ffmpeg;
8486
}
8587
# Audio only?
8688
$self->{'audioonly'} = $audioonly;
89+
# Load the parameter version?
90+
$data = `ffmpeg -h 2>&1`;
91+
if ($data =~ /bit_rate_tolerance/) {
92+
$self->{'ffmpeg_param_vers'} = 0;
93+
}
94+
else {
95+
$self->{'ffmpeg_param_vers'} = 1;
96+
}
8797
# Gather the supported codecs
88-
my $data = `$ffmpeg -formats 2>&1`;
98+
$data = `$ffmpeg -formats 2>&1`;
8999
my ($formats) = $data =~ /(?:^|\n\s*)File\sformats:\s*\n(.+?\n)\s*\n/s;
90100
my ($codecs) = $data =~ /(?:^|\n\s*)Codecs:\s*\n(.+?\n)\s*\n/s;
91101
if ($formats) {
@@ -114,6 +124,44 @@ package export::ffmpeg;
114124
|| $self->{'formats'}{$codec} && $self->{'formats'}{$codec} =~ /^.E/) ? 1 : 0;
115125
}
116126

127+
# ffmpeg keeps changing their parameter names... so we work around it.
128+
sub param {
129+
my $self = shift;
130+
my $param = lc(shift);
131+
my $value = shift;
132+
# Which version?
133+
if ($self->{'ffmpeg_param_vers'} == 1) {
134+
return param_pair('ac', $value) if ($param eq 'channels');
135+
return param_pair('ar', $value) if ($param eq 'sample_rate');
136+
return param_pair('b', $value * 1024) if ($param eq 'bit_rate');
137+
return param_pair('b_qfactor', $value) if ($param eq 'b_quant_factor');
138+
return param_pair('b_qoffset', $value) if ($param eq 'b_quant_offset');
139+
return param_pair('bf', $value) if ($param eq 'max_b_frames');
140+
return param_pair('bt', $value * 1024) if ($param eq 'bit_rate_tolerance');
141+
return param_pair('bufsize', $value) if ($param eq 'rc_buffer_size');
142+
return param_pair('bug', $value) if ($param eq 'bugs');
143+
return param_pair('error', $value) if ($param eq 'error_rate');
144+
return param_pair('g', $value) if ($param eq 'gop_size');
145+
return param_pair('i_qfactor', $value) if ($param eq 'i_quant_factor');
146+
return param_pair('i_qoffset', $value) if ($param eq 'i_quant_offset');
147+
return param_pair('maxrate', $value) if ($param eq 'rc_max_rate');
148+
return param_pair('mblmax', $value) if ($param eq 'mb_lmax');
149+
return param_pair('mblmin', $value) if ($param eq 'mb_lmin');
150+
return param_pair('mepc', $value) if ($param eq 'me_penalty_compensation');
151+
return param_pair('minrate', $value) if ($param eq 'rc_min_rate');
152+
return param_pair('qcomp', $value) if ($param eq 'qcompress');
153+
return param_pair('qdiff', $value) if ($param eq 'max_qdiff');
154+
return param_pair('qsquish', $value) if ($param eq 'rc_qsquish');
155+
return param_pair('rc_init_cplx', $value) if ($param eq 'rc_initial_cplx');
156+
return param_pair('skip_exp', $value) if ($param eq 'frame_skip_exp');
157+
return param_pair('skip_factor', $value) if ($param eq 'frame_skip_factor');
158+
return param_pair('skip_threshold', $value) if ($param eq 'frame_skip_threshold');
159+
return param_pair('threads', $value) if ($param eq 'thread_count');
160+
}
161+
# Unknown, just return the parameter
162+
return param_pair($param, $value);
163+
}
164+
117165
# Load default settings
118166
sub load_defaults {
119167
my $self = shift;

trunk/export/ffmpeg/MP4.pm

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,28 @@ package export::ffmpeg::MP4;
194194
$self->{'out_fps'} = ($self->{'width'} > 320 || $self->{'height'} > 288) ? 29.97 : 23.97;
195195
}
196196
# Embed the title
197-
my $safe_title = shell_escape($episode->{'show_name'}.' - '.$episode->{'title'});
197+
$safe_title = $episode->{'show_name'};
198+
if ($episode->{'title'} ne 'Untitled') {
199+
$safe_title .= ' - '.$episode->{'title'};
200+
}
201+
my $safe_title = shell_escape($safe_title);
198202
# Build the common ffmpeg string
199203
my $ffmpeg_xtra = ' -vcodec '.$self->{'mp4_codec'}
200-
.' -b ' .$self->{'v_bitrate'}
204+
.$self->param('bit_rate', $self->{'v_bitrate'})
201205
." -title $safe_title";
202206
# Options required for the codecs separately
203207
if ($self->{'mp4_codec'} eq 'h264') {
204-
$ffmpeg_xtra .= ' -vcodec h264'
205-
.' -level 13'
208+
$ffmpeg_xtra .= ' -level 13'
206209
.' -loop 1'
207210
.' -g 250 -keyint_min 25'
208-
.' -bit_rate_tolerance '.$self->{'v_bitrate'}
209-
.' -rc_max_rate 768 -rc_buffer_size 244'
210211
.' -sc_threshold 40'
211-
.' -i_quant_factor 0.71428572 -b_quant_factor 0.76923078'
212-
.' -max_b_frames 0'
213212
.' -rc_eq \'blurCplx^(1-qComp)\''
213+
.$self->param('bit_rate_tolerance', $self->{'v_bitrate'})
214+
.$self->param('rc_max_rate', 768)
215+
.$self->param('rc_buffer_size', 244)
216+
.$self->param('i_quant_factor', 0.71428572)
217+
.$self->param('b_quant_factor', 0.76923078)
218+
.$self->param('max_b_frames', 0)
214219
# These should match the defaults:
215220
#.' -me epzs' #(could do "full" but it's only marginally better for less than 1/3 the speed)
216221
;
@@ -224,8 +229,9 @@ package export::ffmpeg::MP4;
224229
}
225230
# Some shared options
226231
if ($self->{'multipass'} || $self->{'vbr'}) {
227-
$ffmpeg_xtra .= ' -qcompress 0.6'
228-
.' -qmax 51 -max_qdiff 4'
232+
$ffmpeg_xtra .= $self->param('qcompress', 0.6)
233+
.$self->param('qmax', 51)
234+
.$self->param('max_qdiff', 4)
229235
;
230236
}
231237
# Dual pass?

trunk/nuv_export/shared_utils.pm

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package nuv_export::shared_utils;
1919

2020
our @EXPORT = qw/ &min
2121
&clear &find_program &shell_escape
22+
&param_pair
2223
&wrap &wipe_tmpfiles &byteswap32
2324
&system &mkdir &byteswap64
2425
@Exporters @episodes $exporter
@@ -169,9 +170,19 @@ BEGIN {
169170

170171
# Escape a parameter for safe use in a commandline call
171172
sub shell_escape {
172-
$file = shift;
173-
$file =~ s/'/'\\''/sg;
174-
return "'$file'";
173+
$str = shift;
174+
$str =~ s/'/'\\''/sg;
175+
return "'$str'";
176+
}
177+
178+
# Return a parameter/value pair, properly escaped
179+
sub param_pair {
180+
my $param = shift;
181+
my $value = shift;
182+
if (defined $value) {
183+
$param .= ' '.shell_escape($value);
184+
}
185+
return ' -'.$param;
175186
}
176187

177188
#

0 commit comments

Comments
 (0)