source: gs2-extensions/video-and-audio/trunk/src/perllib/plugins/AudioConverter.pm@ 25346

Last change on this file since 25346 was 25346, checked in by davidb, 12 years ago

Updates to code that take account of changes in the central PM modules

File size: 7.0 KB
Line 
1###########################################################################
2#
3# AudioConverter - helper plugin that does audio conversion
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2008 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26package AudioConverter;
27
28use MultimediaConverter;
29
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
33
34use gsprintf 'gsprintf';
35
36BEGIN {
37 @AudioConverter::ISA = ('MultimediaConverter');
38}
39
40
41my $arguments = [
42 ];
43
44my $options = { 'name' => "ImageConverter",
45 'desc' => "{ImageConverter.desc}",
46 'abstract' => "yes",
47 'inherits' => "yes",
48 'args' => $arguments };
49
50sub new {
51 my ($class) = shift (@_);
52 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
53 push(@$pluginlist, $class);
54
55 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
56 push(@{$hashArgOptLists->{"OptList"}},$options);
57
58 my $self = new MultimediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
59
60
61 return bless $self, $class;
62
63}
64
65# Discover the characteristics of a audio file.
66
67# Equivalent step to that in ImagePlugin that uses ImageMagicks's
68# 'indentify' utility
69
70# Here we use 'ffmpeg' for audio but for consistency keep the Perl
71# method name the same as before
72
73
74sub identify {
75 my ($audio, $outhandle, $verbosity) = @_;
76
77 # Use the ffmpeg command to get the file specs
78 my $command = "ffmpeg -i \"$audio\"";
79
80 print $outhandle " $command\n" if ($verbosity > 2);
81 my $result = '';
82 $result = `$command 2>&1`;
83 print $outhandle " $result\n" if ($verbosity > 4);
84
85 # Read the type, width, and height etc.
86 my $vtype = 'unknown';
87 my $vcodec = 'unknown';
88 my $width = 'unknown';
89 my $height = 'unknown';
90 my $fps = 'unknown';
91
92 my $atype = 'unknown';
93 my $afreq = 'unknown';
94 my $achan = 'unknown';
95 my $arate = 'unknown';
96
97 my $audio_safe = quotemeta $audio;
98
99 # strip off everything up to filename
100 $result =~ s/^.*\'$audio_safe\'://s;
101
102 if ($result =~ m/Audio: (\w+), (\d+) Hz, (\w+)(?:, (\d+.*))?/m) {
103 $atype = $1;
104 $afreq = $2;
105 $achan = $3;
106 $arate = $4 if (defined $4);
107 }
108
109 # Read the duration
110 my $duration = "unknown";
111 if ($result =~ m/Duration: (\d+:\d+:\d+\.\d+)/m) {
112 $duration = $1;
113 }
114 print $outhandle " file: $audio:\t $atype, $duration\n"
115 if ($verbosity > 2);
116
117 if ($verbosity >3) {
118 print $outhandle "\t freq = $afreq Hz, $achan, $arate\n";
119 }
120
121 # Return the specs
122 return ($duration, -s $audio, $atype,$afreq,$achan,$arate);
123}
124
125
126
127sub audio_converto_cmd
128{
129 my $self = shift (@_);
130 my ($originalfilename,$converttotype) = @_;
131
132 my $output_dir = $self->{'cached_dir'};
133 my $iaudio_root = $self->{'cached_file_root'};
134
135
136 my $file = "$iaudio_root.$converttotype";
137 my $filename = &util::filename_cat($output_dir,$file);
138
139 my $exp_duration = $self->{'exp_duration'};
140 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
141
142 my $main_opts = "-y $t_opt";
143
144
145 my $originalfilename_gsdlenv = $self->gsdlhome_independent($originalfilename);
146 my $filename_gsdlenv = $self->gsdlhome_independent($filename);
147
148
149 my $convertto_cmd = "ffmpeg $main_opts -i \"$originalfilename_gsdlenv\"";
150 $convertto_cmd .= " -y \"$filename_gsdlenv\"";
151
152 return $convertto_cmd;
153}
154
155
156
157sub audio_stream_ffmpeg_cmd
158{
159 my $self = shift (@_);
160 my ($iaudio_filename, $ofile_ext,
161 $streaming_bitrate,$streaming_size,
162 $opt_streaming_achan, $opt_streaming_arate) = @_;
163
164 my $streaming_achan
165 = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;
166
167 my $streaming_arate
168 = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;
169
170 my $output_dir = $self->{'cached_dir'};
171 my $iaudio_root = $self->{'cached_file_root'};
172
173 my $op_file = "${iaudio_root}_stream.$ofile_ext";
174 my $op_filename = &util::filename_cat($output_dir,$op_file);
175
176 my $exp_duration = $self->{'exp_duration'};
177 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
178
179 my $main_opts = "-y $t_opt";
180
181 my $bitrate_opt = "-b $streaming_bitrate";
182 my $stream_opts .= " -ac $streaming_achan -ar $streaming_arate";
183
184
185 my $all_opts = "$main_opts $stream_opts";
186
187 my $ffmpeg_cmd;
188
189 my $iaudio_filename_gsdlenv = $self->gsdlhome_independent($iaudio_filename);
190 my $op_filename_gsdlenv = $self->gsdlhome_independent($op_filename);
191
192
193 $ffmpeg_cmd = "ffmpeg -i \"$iaudio_filename_gsdlenv\" $all_opts -y \"$op_filename_gsdlenv\"";
194
195 return ($ffmpeg_cmd,$op_filename,$op_file);
196}
197
198
199
200sub stream_mp4_audio_cmd
201{
202 my $self = shift (@_);
203 my ($ivideo_filename,$streaming_HQ_size, $streaming_HQ_AudioBitrate) = @_;
204
205 my $output_dir = $self->{'cached_dir'};
206 my $ivideo_root = $self->{'cached_file_root'};
207
208 my $omp4_file = "${ivideo_root}_aHQstream.mp4";
209 my $omp4_filename = &util::filename_cat($output_dir,$omp4_file);
210
211
212 #my $exp_duration = $self->{'exp_duration'};
213 #my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
214
215 my $deinterlace = $self->{'videoDeinterlacingFilter'};
216 my $video_processing_parameters;
217 # Use specific resizing algorythms depending if we need to downsize the video resolution or not
218 if(!$streaming_HQ_size || $streaming_HQ_size eq "fullsize") {
219 $video_processing_parameters = "--strict-anamorphic";
220 } else {
221 $video_processing_parameters = "-w $streaming_HQ_size --loose-anamorphic";
222 }
223 # Use the deinterlace video filter if enabled in the plugin
224 if ($deinterlace eq "true")
225 {
226 $video_processing_parameters .= " --decomb";
227 }
228
229 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
230 my $omp4_filename_gsdlenv = $self->gsdlhome_independent($omp4_filename);
231
232 # Generate the pre and post video parameters for Handbrake v0.9.4
233 my $pre_opts = "-t 1 -c 1";
234 my $post_opts = "-f mp4 -O $video_processing_parameters -e x264 -a 1 -E faac -6 dpl2 -R Auto -B $streaming_HQ_AudioBitrate -D 0.0 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0";
235
236 my $handbrake_cmd = "HandbrakeCLI.exe -i \"$ivideo_filename_gsdlenv\" $pre_opts -o \"$omp4_filename_gsdlenv\" $post_opts";
237
238 #print STDERR "****\nHandbrake command: $handbrake_cmd\n****\n";
239
240 return ($handbrake_cmd,$omp4_filename,$omp4_file);
241}
242
243
244
245
246
2471;
Note: See TracBrowser for help on using the repository browser.