[7487] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # MP3Plug.pm -- Plugin for MP3 files (MPEG audio layer 3).
|
---|
| 4 | #
|
---|
| 5 | # A component of the Greenstone digital library software from the New
|
---|
| 6 | # Zealand Digital Library Project at the University of Waikato, New
|
---|
| 7 | # Zealand.
|
---|
| 8 | #
|
---|
| 9 | # Copyright (C) 2001 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, but
|
---|
| 17 | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 19 | # 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 |
|
---|
| 27 |
|
---|
| 28 | package MP3Plug;
|
---|
| 29 |
|
---|
| 30 | use UnknownPlug;
|
---|
| 31 | use parsargv;
|
---|
| 32 |
|
---|
| 33 | use MP3::Info;
|
---|
| 34 |
|
---|
| 35 | require giget;
|
---|
| 36 |
|
---|
| 37 | sub BEGIN {
|
---|
| 38 | @ISA = ('UnknownPlug');
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | my $arguments =
|
---|
[9167] | 42 | [ { 'name' => "process_exp",
|
---|
| 43 | 'desc' => "{BasPlug.process_exp}",
|
---|
| 44 | 'type' => "string",
|
---|
| 45 | 'deft' => &get_default_process_exp(),
|
---|
| 46 | 'reqd' => "no" },
|
---|
| 47 | { 'name' => "assoc_images",
|
---|
[7490] | 48 | 'desc' => "{MP3Plug.assoc_images}",
|
---|
[7487] | 49 | 'type' => "flag",
|
---|
| 50 | 'deft' => "",
|
---|
[7504] | 51 | 'reqd' => "no" },
|
---|
| 52 | { 'name' => "applet_metadata",
|
---|
| 53 | 'desc' => "{MP3Plug.applet_metadata}",
|
---|
| 54 | 'type' => "flag",
|
---|
| 55 | 'deft' => "" },
|
---|
| 56 | { 'name' => "metadata_fields",
|
---|
| 57 | 'desc' => "{MP3Plug.metadata_fields}",
|
---|
| 58 | 'type' => "string",
|
---|
| 59 | 'deft' => "Title,Artist,Genre" } ];
|
---|
[7487] | 60 |
|
---|
| 61 | my $options = { 'name' => "MP3Plug",
|
---|
| 62 | 'desc' => "{MP3Plug.desc}",
|
---|
| 63 | 'abstract' => "no",
|
---|
| 64 | 'inherits' => "yes",
|
---|
| 65 | 'args' => $arguments };
|
---|
| 66 |
|
---|
| 67 | sub new {
|
---|
| 68 | my ($class) = @_;
|
---|
| 69 | my $self = new UnknownPlug ($class, @_);
|
---|
| 70 | $self->{'plugin_type'} = "MP3Plug";
|
---|
| 71 | # 14-05-02 To allow for proper inheritance of arguments - John Thompson
|
---|
| 72 | my $option_list = $self->{'option_list'};
|
---|
| 73 | push( @{$option_list}, $options );
|
---|
| 74 |
|
---|
| 75 | if (!parsargv::parse(\@_,
|
---|
| 76 | q^assoc_images^, \$self->{'assoc_images'},
|
---|
[7504] | 77 | q^applet_metadata^, \$self->{'applet_metadata'},
|
---|
| 78 | q^metadata_fields/.*/Title,Artist,Genre^, \$self->{'metadata_fields'},
|
---|
[7487] | 79 | "allow_extra_options")) {
|
---|
| 80 | print STDERR "\nIncorrect options passed to MP3Plug, check your collect.cfg configuration file\n";
|
---|
| 81 | $self->print_txt_usage(""); # Use default resource bundle
|
---|
| 82 | die "\n";
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return bless $self, $class;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | sub get_default_process_exp {
|
---|
| 89 | return q^(?i)\.mp3$^;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | sub gen_mp3applet {
|
---|
| 93 |
|
---|
| 94 | my ($mp3_filename) = @_;
|
---|
| 95 |
|
---|
| 96 | my $applet_html = '
|
---|
| 97 | <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
|
---|
| 98 | WIDTH = "59"
|
---|
| 99 | HEIGHT = "32"
|
---|
| 100 | codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
|
---|
| 101 | <PARAM NAME = CODE VALUE = "javazoom.jlGui.TinyPlayer" >
|
---|
| 102 | <PARAM NAME = ARCHIVE VALUE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl020.jar" >
|
---|
| 103 | <PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
|
---|
| 104 | <PARAM NAME="scriptable" VALUE="false">
|
---|
| 105 | <PARAM NAME = "skin" VALUE ="_httpcollection_/skins/Digitalized">
|
---|
| 106 | <PARAM NAME = "autoplay" VALUE ="yes">
|
---|
| 107 | <PARAM NAME = "bgcolor" VALUE ="638182">
|
---|
| 108 | <PARAM NAME = "audioURL" VALUE ="MP3FILENAME">
|
---|
| 109 | <COMMENT>
|
---|
| 110 | <EMBED type="application/x-java-applet;version=1.3"
|
---|
| 111 | CODE = "javazoom.jlGui.TinyPlayer"
|
---|
| 112 | ARCHIVE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl020.jar"
|
---|
| 113 | WIDTH = "59"
|
---|
| 114 | HEIGHT = "32"
|
---|
| 115 | skin = "_httpcollection_/skins/Digitalized"
|
---|
| 116 | autoplay = "yes"
|
---|
| 117 | bgcolor = "638182"
|
---|
| 118 | audioURL = "MP3FILENAME"
|
---|
| 119 | scriptable=false
|
---|
| 120 | pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html">
|
---|
| 121 | <NOEMBED>
|
---|
| 122 | </COMMENT>
|
---|
| 123 | </NOEMBED></EMBED>
|
---|
| 124 | </OBJECT>
|
---|
| 125 | ';
|
---|
| 126 |
|
---|
| 127 | $applet_html =~ s/MP3FILENAME/$mp3_filename/g;
|
---|
| 128 |
|
---|
| 129 | return $applet_html;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | # Associate the mp3 file with the new document
|
---|
| 135 |
|
---|
| 136 | sub associate_mp3_file {
|
---|
| 137 | my $self = shift (@_);
|
---|
| 138 | my $filename = shift (@_); # filename with full path
|
---|
| 139 | my $file = shift (@_); # filename without path
|
---|
| 140 | my $doc_obj = shift (@_);
|
---|
| 141 |
|
---|
| 142 | my $verbosity = $self->{'verbosity'};
|
---|
| 143 | my $outhandle = $self->{'outhandle'};
|
---|
| 144 |
|
---|
| 145 | # check the filename is okay
|
---|
| 146 | return 0 if ($file eq "" || $filename eq "");
|
---|
| 147 |
|
---|
| 148 | # Add the file metadata
|
---|
| 149 | my $assoc_url = $file;
|
---|
| 150 | # $assoc_url =~ s/ /%20/g; # probably need to do more escaping than this!!
|
---|
| 151 | $assoc_url =~ s/ /_/g; # workaround for now
|
---|
| 152 | my $dst_file = $file;
|
---|
| 153 | $dst_file =~ s/ /_/g;
|
---|
| 154 |
|
---|
| 155 | # Add the file as an associated file ...
|
---|
| 156 | my $section = $doc_obj->get_top_section();
|
---|
[7504] | 157 | my $mime_type = $self->{'mime_type'} || "audio/mp3";
|
---|
[7487] | 158 | my $assoc_field = $self->{'assoc_field'} || "mp3";
|
---|
| 159 | my $assoc_name = $file;
|
---|
| 160 | $assoc_name =~ s/\.mp3$//;
|
---|
| 161 |
|
---|
[7504] | 162 | $doc_obj->associate_file($filename, $dst_file, $mime_type, $section);
|
---|
[7487] | 163 | $doc_obj->add_metadata ($section, "Source", $file);
|
---|
| 164 | $doc_obj->add_metadata ($section, $assoc_field, $assoc_name);
|
---|
| 165 | $doc_obj->add_metadata ($section, "srcurl", $assoc_url);
|
---|
| 166 |
|
---|
| 167 | my $mp3_info = get_mp3info($filename);
|
---|
[7504] | 168 | my $mp3_tags = get_mp3tag($filename);
|
---|
[7487] | 169 |
|
---|
[7504] | 170 | my $metadata_fields = $self->{'metadata_fields'};
|
---|
| 171 |
|
---|
| 172 | if ($metadata_fields eq "*") {
|
---|
| 173 | # Locate all info and tag metadata
|
---|
| 174 |
|
---|
| 175 | foreach my $ki ( keys %$mp3_info ) {
|
---|
| 176 | my $mp3_metavalue = $mp3_info->{$ki};
|
---|
| 177 |
|
---|
| 178 | if ($mp3_metavalue !~ m/^\s*$/s) {
|
---|
| 179 | my $mp3_metaname = "mp3:".lc($ki);
|
---|
| 180 | $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
|
---|
| 181 | }
|
---|
[7487] | 182 | }
|
---|
[7504] | 183 |
|
---|
| 184 | foreach my $kt ( keys %$mp3_tags ) {
|
---|
| 185 | my $mp3_metavalue = $mp3_tags->{$kt};
|
---|
| 186 |
|
---|
| 187 | if ($mp3_metavalue !~ m/^\s*$/s) {
|
---|
| 188 | my $kt_len = length($kt);
|
---|
| 189 | $kt_initial_cap = uc(substr($kt,0,1)).lc(substr($kt,1,$kt_len-1));
|
---|
| 190 | my $mp3_metaname = "mp3:".$kt_initial_cap;
|
---|
| 191 |
|
---|
| 192 | $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
[7487] | 195 | }
|
---|
[7504] | 196 | else {
|
---|
[7487] | 197 |
|
---|
[7504] | 198 | # Restrict metadata to that specifically given
|
---|
| 199 | foreach my $field (split /,/, $metadata_fields) {
|
---|
[7487] | 200 |
|
---|
[7504] | 201 | # check info
|
---|
| 202 | if (defined $mp3_info->{$field}) {
|
---|
[7487] | 203 |
|
---|
[7504] | 204 | my $mp3i_metavalue = $mp3_info->{$field};
|
---|
| 205 |
|
---|
| 206 | if ($mp3i_metavalue !~ m/^\s*$/s) {
|
---|
| 207 | my $mp3i_metaname = "mp3:".lc($field);
|
---|
| 208 | $doc_obj->add_metadata ($section, $mp3i_metaname, $mp3i_metavalue);
|
---|
| 209 | }
|
---|
[7487] | 210 | }
|
---|
[7504] | 211 |
|
---|
| 212 | # check tags
|
---|
[7513] | 213 | if (defined $mp3_tags->{uc($field)}) {
|
---|
[7504] | 214 |
|
---|
| 215 | my $mp3t_metavalue = $mp3_tags->{uc($field)};
|
---|
| 216 |
|
---|
| 217 | if ($mp3t_metavalue !~ m/^\s*$/s) {
|
---|
| 218 | my $mp3t_metaname = "mp3:".$field;
|
---|
| 219 |
|
---|
| 220 | $doc_obj->add_metadata ($section, $mp3t_metaname, $mp3t_metavalue);
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[7487] | 224 | }
|
---|
| 225 | }
|
---|
[7528] | 226 |
|
---|
| 227 | $doc_obj->add_metadata ($section, "FileFormat", "MP3");
|
---|
[7487] | 228 |
|
---|
| 229 | $doc_obj->add_metadata ($section, "srclink",
|
---|
[8365] | 230 | "<a href=\"_httpcollection_/index/assoc/[assocfilepath]/[srcurl]\">");
|
---|
| 231 | $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpimg_/mp3icon.gif\">");
|
---|
[7487] | 232 | $doc_obj->add_metadata ($section, "/srclink", "</a>");
|
---|
| 233 |
|
---|
[7504] | 234 | my $applet_metadata = $self->{'applet_metadata'};
|
---|
| 235 | if (defined $applet_metadata && $applet_metadata ) {
|
---|
| 236 | my $applet_html
|
---|
| 237 | = gen_mp3applet("_httpcollection_/index/assoc/[assocfilepath]/[srcurl]");
|
---|
| 238 | $doc_obj->add_metadata ($section, "mp3applet", $applet_html);
|
---|
| 239 | }
|
---|
[7487] | 240 |
|
---|
| 241 | my $assoc_images = $self->{'assoc_images'};
|
---|
| 242 | if (defined $assoc_images && $assoc_images) {
|
---|
| 243 | my @search_terms = ();
|
---|
| 244 |
|
---|
| 245 | my $title = $mp3_tags->{'TITLE'};
|
---|
| 246 | my $artist = $mp3_tags->{'ARTIST'};
|
---|
| 247 |
|
---|
| 248 | if (defined $title && $title ne "") {
|
---|
| 249 |
|
---|
| 250 |
|
---|
| 251 | push(@search_terms,$title);
|
---|
| 252 |
|
---|
| 253 | if (defined $artist && $artist ne "") {
|
---|
| 254 | push(@search_terms,$artist);
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | else {
|
---|
| 258 | push(@search_terms,$assoc_name);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | my $output_dir = $filename;
|
---|
| 262 | $output_dir =~ s/\.\w+$//;
|
---|
| 263 |
|
---|
| 264 | my ($imgref_urls) = giget(\@search_terms,$output_dir);
|
---|
| 265 |
|
---|
| 266 | my $gi_base = gi_url_base();
|
---|
| 267 | my $gi_query_url = gi_query_url(\@search_terms);
|
---|
| 268 |
|
---|
[8365] | 269 | $doc_obj->add_metadata ($section, "giquery", "<a href=\"$gi_base$gi_query_url\" target=giwindow>");
|
---|
[7487] | 270 | $doc_obj->add_metadata ($section, "/giquery", "</a>");
|
---|
| 271 |
|
---|
| 272 | for (my $i=1; $i<=2; $i++) {
|
---|
| 273 | my $img_filename = "$output_dir/img_$i.jpg";
|
---|
| 274 | my $dst_file = "img_$i.jpg";
|
---|
| 275 |
|
---|
| 276 | if (-e $img_filename) {
|
---|
| 277 | $doc_obj->associate_file($img_filename, $dst_file, "image/jpeg", $section);
|
---|
| 278 |
|
---|
| 279 | my $srcurl = "src=\"_httpcollection_/index/assoc/[assocfilepath]/$dst_file\"";
|
---|
| 280 |
|
---|
| 281 | $doc_obj->add_metadata ($section, "img$i",
|
---|
| 282 | "<img $srcurl>");
|
---|
| 283 | $doc_obj->add_metadata ($section, "smallimg$i",
|
---|
| 284 | "<img $srcurl width=100>");
|
---|
| 285 |
|
---|
| 286 | my $imgref_url = $imgref_urls->[$i-1];
|
---|
| 287 |
|
---|
[8365] | 288 | $doc_obj->add_metadata ($section, "imgref$i", "<a href=\"$imgref_url\" target=giwindow>");
|
---|
[7487] | 289 | $doc_obj->add_metadata ($section, "/imgref$i", "</a>");
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 |
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | return 1;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 | # The MP3Plug read() function is based on UnknownPlug read(). This
|
---|
| 303 | # function does all the right things to make general options work for
|
---|
| 304 | # a given plugin.
|
---|
| 305 |
|
---|
[7504] | 306 | my $mp3_doc_count = 0; ## is this used anywhere now !!???
|
---|
[7487] | 307 |
|
---|
| 308 | sub read {
|
---|
| 309 | my $self = shift (@_);
|
---|
| 310 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $gli) = @_;
|
---|
| 311 |
|
---|
| 312 | my $outhandle = $self->{'outhandle'};
|
---|
| 313 |
|
---|
| 314 | # Make sure we're processing the correct file
|
---|
| 315 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
| 316 | return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
|
---|
| 317 | if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
|
---|
| 318 | return undef;
|
---|
| 319 | }
|
---|
| 320 | print STDERR "<Processing n='$file' p='MP3Plug'>\n" if ($gli);
|
---|
| 321 | print $outhandle "MP3Plug processing \"$filename\"\n"
|
---|
| 322 | if $self->{'verbosity'} > 1;
|
---|
| 323 |
|
---|
| 324 | #if there's a leading directory name, eat it...
|
---|
| 325 | $file =~ s/^.*[\/\\]//;
|
---|
| 326 |
|
---|
| 327 | # create a new document
|
---|
| 328 | my $doc_obj = new doc ($filename, "indexed_doc");
|
---|
| 329 | $mp3_doc_count++;
|
---|
| 330 |
|
---|
| 331 | ## $doc_obj->set_OIDtype ($processor->{'OIDtype'});
|
---|
[7504] | 332 | $doc_obj->set_OIDtype ("incremental"); # this is done to avoid hashing content of file
|
---|
[7508] | 333 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
|
---|
[8166] | 334 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
|
---|
[7487] | 335 |
|
---|
| 336 | # associate the file with the document
|
---|
| 337 | if (associate_mp3_file($self, $filename, $file, $doc_obj) != 1)
|
---|
| 338 | {
|
---|
| 339 | print "MP3Plug: couldn't process \"$filename\"\n";
|
---|
| 340 | return 0;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | #create an empty text string so we don't break downstream plugins
|
---|
[7506] | 344 | my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}");
|
---|
[7487] | 345 | $text .= "[img1]<br>";
|
---|
| 346 | $text .= "[img2]<br>";
|
---|
| 347 |
|
---|
| 348 | # include any metadata passed in from previous plugins
|
---|
| 349 | my $section = $doc_obj->get_top_section();
|
---|
| 350 | $self->extra_metadata ($doc_obj, $section, $metadata);
|
---|
| 351 |
|
---|
[8519] | 352 | $self->title_fallback($doc_obj,$section,$file);
|
---|
| 353 |
|
---|
[7487] | 354 | # do plugin specific processing of doc_obj
|
---|
| 355 | return undef unless defined ($self->process (\$text, $pluginfo, $base_dir,
|
---|
| 356 | $file, $metadata, $doc_obj));
|
---|
| 357 |
|
---|
| 358 | # do any automatic metadata extraction
|
---|
| 359 | $self->auto_extract_metadata ($doc_obj);
|
---|
| 360 |
|
---|
| 361 | # add an OID
|
---|
| 362 | $doc_obj->set_OID();
|
---|
| 363 | $doc_obj->add_text($section, $text);
|
---|
| 364 |
|
---|
| 365 | # process the document
|
---|
| 366 | $processor->process($doc_obj);
|
---|
| 367 |
|
---|
| 368 | $self->{'num_processed'} ++;
|
---|
| 369 | return 1;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 |
|
---|
[8519] | 373 | sub title_fallback
|
---|
| 374 | {
|
---|
| 375 | my $self = shift (@_);
|
---|
| 376 | my ($doc_obj,$section,$file) = @_;
|
---|
[7487] | 377 |
|
---|
[8519] | 378 | if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
|
---|
| 379 | my $mp3_title = $doc_obj->get_metadata_element ($section, "mp3:Title");
|
---|
| 380 | if (defined $mp3_title) {
|
---|
| 381 | $doc_obj->add_metadata ($section, "Title", $mp3_title);
|
---|
| 382 | }
|
---|
| 383 | else {
|
---|
| 384 | &BasPlug::title_fallback($self, $doc_obj, $section, $file);
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 |
|
---|
[7487] | 390 | 1;
|
---|
| 391 |
|
---|
| 392 |
|
---|
| 393 |
|
---|
| 394 |
|
---|
| 395 |
|
---|
| 396 |
|
---|
| 397 |
|
---|
| 398 |
|
---|
| 399 |
|
---|
| 400 |
|
---|
| 401 |
|
---|