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 | ########################################################################### |
---|
26 | package AudioConverter; |
---|
27 | |
---|
28 | use MultimediaConverter; |
---|
29 | |
---|
30 | |
---|
31 | use strict; |
---|
32 | no strict 'refs'; # allow filehandles to be variables and viceversa |
---|
33 | |
---|
34 | use gsprintf 'gsprintf'; |
---|
35 | |
---|
36 | BEGIN { |
---|
37 | @AudioConverter::ISA = ('MultimediaConverter'); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | my $arguments = [ |
---|
42 | ]; |
---|
43 | |
---|
44 | my $options = { 'name' => "ImageConverter", |
---|
45 | 'desc' => "{ImageConverter.desc}", |
---|
46 | 'abstract' => "yes", |
---|
47 | 'inherits' => "yes", |
---|
48 | 'args' => $arguments }; |
---|
49 | |
---|
50 | sub 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 | |
---|
74 | sub 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 | |
---|
127 | sub 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 | |
---|
157 | sub audio_stream_cmd |
---|
158 | { |
---|
159 | my $self = shift (@_); |
---|
160 | my ($iaudio_filename, |
---|
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 $mp3_file = "${iaudio_root}_stream.mp3"; |
---|
174 | my $mp3_filename = &util::filename_cat($output_dir,$mp3_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 $mp3_filename_gsdlenv = $self->gsdlhome_independent($mp3_filename); |
---|
191 | |
---|
192 | |
---|
193 | $ffmpeg_cmd = "ffmpeg -i \"$iaudio_filename_gsdlenv\" $all_opts -y \"$mp3_filename_gsdlenv\""; |
---|
194 | |
---|
195 | return ($ffmpeg_cmd,$mp3_filename,$mp3_file); |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | 1; |
---|