[7487] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15872] | 3 | # MP3Plugin.pm -- Plugin for MP3 files (MPEG audio layer 3).
|
---|
[7487] | 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 |
|
---|
[15872] | 28 | package MP3Plugin;
|
---|
[7487] | 29 |
|
---|
[15872] | 30 | use BasePlugin;
|
---|
[7487] | 31 |
|
---|
[10254] | 32 | use strict;
|
---|
| 33 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[15872] | 34 | no strict 'subs';
|
---|
[10254] | 35 |
|
---|
[7487] | 36 | use MP3::Info;
|
---|
| 37 |
|
---|
| 38 | require giget;
|
---|
| 39 |
|
---|
| 40 | sub BEGIN {
|
---|
[15872] | 41 | @MP3Plugin::ISA = ('BasePlugin');
|
---|
[7487] | 42 | }
|
---|
| 43 |
|
---|
| 44 | my $arguments =
|
---|
[9167] | 45 | [ { 'name' => "process_exp",
|
---|
[15872] | 46 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[14687] | 47 | 'type' => "regexp",
|
---|
[9167] | 48 | 'deft' => &get_default_process_exp(),
|
---|
| 49 | 'reqd' => "no" },
|
---|
| 50 | { 'name' => "assoc_images",
|
---|
[15872] | 51 | 'desc' => "{MP3Plugin.assoc_images}",
|
---|
[7487] | 52 | 'type' => "flag",
|
---|
| 53 | 'deft' => "",
|
---|
[7504] | 54 | 'reqd' => "no" },
|
---|
| 55 | { 'name' => "applet_metadata",
|
---|
[15872] | 56 | 'desc' => "{MP3Plugin.applet_metadata}",
|
---|
[7504] | 57 | 'type' => "flag",
|
---|
| 58 | 'deft' => "" },
|
---|
| 59 | { 'name' => "metadata_fields",
|
---|
[15872] | 60 | 'desc' => "{MP3Plugin.metadata_fields}",
|
---|
[7504] | 61 | 'type' => "string",
|
---|
| 62 | 'deft' => "Title,Artist,Genre" } ];
|
---|
[7487] | 63 |
|
---|
[15872] | 64 | my $options = { 'name' => "MP3Plugin",
|
---|
| 65 | 'desc' => "{MP3Plugin.desc}",
|
---|
[7487] | 66 | 'abstract' => "no",
|
---|
| 67 | 'inherits' => "yes",
|
---|
| 68 | 'args' => $arguments };
|
---|
| 69 |
|
---|
| 70 | sub new {
|
---|
[10218] | 71 | my ($class) = shift (@_);
|
---|
| 72 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 73 | push(@$pluginlist, $class);
|
---|
[7487] | 74 |
|
---|
[15872] | 75 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 76 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[7487] | 77 |
|
---|
[15872] | 78 | my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[10218] | 79 |
|
---|
[7487] | 80 | return bless $self, $class;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | sub get_default_process_exp {
|
---|
| 84 | return q^(?i)\.mp3$^;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[17026] | 87 | # we don't want to hash on the file
|
---|
| 88 | sub get_oid_hash_type {
|
---|
| 89 | my $self = shift (@_);
|
---|
| 90 | return "hash_on_ga_xml";
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[15872] | 93 | sub process {
|
---|
| 94 | my $self = shift (@_);
|
---|
| 95 | my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
| 96 |
|
---|
[16392] | 97 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[15872] | 98 |
|
---|
| 99 | # associate the file with the document
|
---|
| 100 | if ($self->associate_mp3_file($filename_full_path, $filename_no_path, $doc_obj) != 1)
|
---|
| 101 | {
|
---|
| 102 | print "MP3Plugin: couldn't process \"$filename_full_path\"\n";
|
---|
| 103 | return 0;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[15911] | 106 | my $text = &gsprintf::lookup_string("{BasePlugin.dummy_text}",1);
|
---|
[15872] | 107 | if ($self->{'assoc_images'}) {
|
---|
| 108 | $text .= "[img1]<br>";
|
---|
| 109 | $text .= "[img2]<br>";
|
---|
| 110 | }
|
---|
| 111 | $doc_obj->add_utf8_text($doc_obj->get_top_section(), $text);
|
---|
[15911] | 112 | $doc_obj->add_metadata ($doc_obj->get_top_section(), "NoText", "1");
|
---|
[15872] | 113 |
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[7487] | 116 | sub gen_mp3applet {
|
---|
| 117 |
|
---|
| 118 | my ($mp3_filename) = @_;
|
---|
| 119 |
|
---|
| 120 | my $applet_html = '
|
---|
| 121 | <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
|
---|
| 122 | WIDTH = "59"
|
---|
| 123 | HEIGHT = "32"
|
---|
| 124 | codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
|
---|
| 125 | <PARAM NAME = CODE VALUE = "javazoom.jlGui.TinyPlayer" >
|
---|
[9962] | 126 | <PARAM NAME = ARCHIVE VALUE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar" >
|
---|
[7487] | 127 | <PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
|
---|
| 128 | <PARAM NAME="scriptable" VALUE="false">
|
---|
| 129 | <PARAM NAME = "skin" VALUE ="_httpcollection_/skins/Digitalized">
|
---|
| 130 | <PARAM NAME = "autoplay" VALUE ="yes">
|
---|
| 131 | <PARAM NAME = "bgcolor" VALUE ="638182">
|
---|
| 132 | <PARAM NAME = "audioURL" VALUE ="MP3FILENAME">
|
---|
| 133 | <COMMENT>
|
---|
| 134 | <EMBED type="application/x-java-applet;version=1.3"
|
---|
| 135 | CODE = "javazoom.jlGui.TinyPlayer"
|
---|
[9962] | 136 | ARCHIVE = "_httpcollection_/tinyplayer.jar,_httpcollection_/jl10.jar"
|
---|
[7487] | 137 | WIDTH = "59"
|
---|
| 138 | HEIGHT = "32"
|
---|
| 139 | skin = "_httpcollection_/skins/Digitalized"
|
---|
| 140 | autoplay = "yes"
|
---|
| 141 | bgcolor = "638182"
|
---|
| 142 | audioURL = "MP3FILENAME"
|
---|
| 143 | scriptable=false
|
---|
| 144 | pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html">
|
---|
| 145 | <NOEMBED>
|
---|
| 146 | </COMMENT>
|
---|
| 147 | </NOEMBED></EMBED>
|
---|
| 148 | </OBJECT>
|
---|
| 149 | ';
|
---|
| 150 |
|
---|
| 151 | $applet_html =~ s/MP3FILENAME/$mp3_filename/g;
|
---|
| 152 |
|
---|
| 153 | return $applet_html;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | # Associate the mp3 file with the new document
|
---|
| 159 |
|
---|
| 160 | sub associate_mp3_file {
|
---|
| 161 | my $self = shift (@_);
|
---|
| 162 | my $filename = shift (@_); # filename with full path
|
---|
| 163 | my $file = shift (@_); # filename without path
|
---|
| 164 | my $doc_obj = shift (@_);
|
---|
| 165 |
|
---|
| 166 | my $verbosity = $self->{'verbosity'};
|
---|
| 167 | my $outhandle = $self->{'outhandle'};
|
---|
| 168 |
|
---|
| 169 | # check the filename is okay
|
---|
| 170 | return 0 if ($file eq "" || $filename eq "");
|
---|
| 171 |
|
---|
[16952] | 172 | # Add the file metadata.
|
---|
| 173 | # $assoc_url will be the srcurl. Since it is URL encoded here, it will be
|
---|
| 174 | # able to cope with special characters--including spaces--in mp3 filenames.
|
---|
| 175 | # the assocfilename generated will be a URL encoded version of the utf8 filename
|
---|
| 176 | my $assoc_url = $doc_obj->get_sourcefile();
|
---|
| 177 | my $dst_file = $doc_obj->get_assocfile_from_sourcefile();
|
---|
[7487] | 178 |
|
---|
| 179 | # Add the file as an associated file ...
|
---|
| 180 | my $section = $doc_obj->get_top_section();
|
---|
[7504] | 181 | my $mime_type = $self->{'mime_type'} || "audio/mp3";
|
---|
[7487] | 182 | my $assoc_field = $self->{'assoc_field'} || "mp3";
|
---|
| 183 | my $assoc_name = $file;
|
---|
| 184 | $assoc_name =~ s/\.mp3$//;
|
---|
| 185 |
|
---|
[7504] | 186 | $doc_obj->associate_file($filename, $dst_file, $mime_type, $section);
|
---|
[7487] | 187 | $doc_obj->add_metadata ($section, $assoc_field, $assoc_name);
|
---|
| 188 | $doc_obj->add_metadata ($section, "srcurl", $assoc_url);
|
---|
| 189 |
|
---|
| 190 | my $mp3_info = get_mp3info($filename);
|
---|
[7504] | 191 | my $mp3_tags = get_mp3tag($filename);
|
---|
[7487] | 192 |
|
---|
[7504] | 193 | my $metadata_fields = $self->{'metadata_fields'};
|
---|
| 194 |
|
---|
| 195 | if ($metadata_fields eq "*") {
|
---|
| 196 | # Locate all info and tag metadata
|
---|
| 197 |
|
---|
| 198 | foreach my $ki ( keys %$mp3_info ) {
|
---|
| 199 | my $mp3_metavalue = $mp3_info->{$ki};
|
---|
| 200 |
|
---|
| 201 | if ($mp3_metavalue !~ m/^\s*$/s) {
|
---|
| 202 | my $mp3_metaname = "mp3:".lc($ki);
|
---|
| 203 | $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
|
---|
| 204 | }
|
---|
[7487] | 205 | }
|
---|
[7504] | 206 |
|
---|
| 207 | foreach my $kt ( keys %$mp3_tags ) {
|
---|
| 208 | my $mp3_metavalue = $mp3_tags->{$kt};
|
---|
| 209 |
|
---|
| 210 | if ($mp3_metavalue !~ m/^\s*$/s) {
|
---|
| 211 | my $kt_len = length($kt);
|
---|
[10254] | 212 | my $kt_initial_cap = uc(substr($kt,0,1)).lc(substr($kt,1,$kt_len-1));
|
---|
[7504] | 213 | my $mp3_metaname = "mp3:".$kt_initial_cap;
|
---|
| 214 |
|
---|
| 215 | $doc_obj->add_metadata ($section, $mp3_metaname, $mp3_metavalue);
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
[7487] | 218 | }
|
---|
[7504] | 219 | else {
|
---|
[7487] | 220 |
|
---|
[7504] | 221 | # Restrict metadata to that specifically given
|
---|
| 222 | foreach my $field (split /,/, $metadata_fields) {
|
---|
[7487] | 223 |
|
---|
[7504] | 224 | # check info
|
---|
| 225 | if (defined $mp3_info->{$field}) {
|
---|
[7487] | 226 |
|
---|
[7504] | 227 | my $mp3i_metavalue = $mp3_info->{$field};
|
---|
| 228 |
|
---|
| 229 | if ($mp3i_metavalue !~ m/^\s*$/s) {
|
---|
| 230 | my $mp3i_metaname = "mp3:".lc($field);
|
---|
| 231 | $doc_obj->add_metadata ($section, $mp3i_metaname, $mp3i_metavalue);
|
---|
| 232 | }
|
---|
[7487] | 233 | }
|
---|
[7504] | 234 |
|
---|
| 235 | # check tags
|
---|
[7513] | 236 | if (defined $mp3_tags->{uc($field)}) {
|
---|
[7504] | 237 |
|
---|
| 238 | my $mp3t_metavalue = $mp3_tags->{uc($field)};
|
---|
| 239 |
|
---|
| 240 | if ($mp3t_metavalue !~ m/^\s*$/s) {
|
---|
| 241 | my $mp3t_metaname = "mp3:".$field;
|
---|
| 242 |
|
---|
| 243 | $doc_obj->add_metadata ($section, $mp3t_metaname, $mp3t_metavalue);
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[7487] | 247 | }
|
---|
| 248 | }
|
---|
[7528] | 249 |
|
---|
| 250 | $doc_obj->add_metadata ($section, "FileFormat", "MP3");
|
---|
[7487] | 251 |
|
---|
| 252 | $doc_obj->add_metadata ($section, "srclink",
|
---|
[11834] | 253 | "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]\">");
|
---|
[11305] | 254 | $doc_obj->add_metadata ($section, "srcicon", "_iconmp3_");
|
---|
[7487] | 255 | $doc_obj->add_metadata ($section, "/srclink", "</a>");
|
---|
| 256 |
|
---|
[7504] | 257 | my $applet_metadata = $self->{'applet_metadata'};
|
---|
| 258 | if (defined $applet_metadata && $applet_metadata ) {
|
---|
| 259 | my $applet_html
|
---|
[11834] | 260 | = gen_mp3applet("_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[srcurl]");
|
---|
[7504] | 261 | $doc_obj->add_metadata ($section, "mp3applet", $applet_html);
|
---|
| 262 | }
|
---|
[7487] | 263 |
|
---|
| 264 | my $assoc_images = $self->{'assoc_images'};
|
---|
| 265 | if (defined $assoc_images && $assoc_images) {
|
---|
| 266 | my @search_terms = ();
|
---|
| 267 |
|
---|
| 268 | my $title = $mp3_tags->{'TITLE'};
|
---|
| 269 | my $artist = $mp3_tags->{'ARTIST'};
|
---|
| 270 |
|
---|
| 271 | if (defined $title && $title ne "") {
|
---|
| 272 |
|
---|
| 273 | push(@search_terms,$title);
|
---|
| 274 |
|
---|
| 275 | if (defined $artist && $artist ne "") {
|
---|
| 276 | push(@search_terms,$artist);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | else {
|
---|
| 280 | push(@search_terms,$assoc_name);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[9962] | 283 | push(@search_terms,"song");
|
---|
| 284 |
|
---|
[7487] | 285 | my $output_dir = $filename;
|
---|
| 286 | $output_dir =~ s/\.\w+$//;
|
---|
| 287 |
|
---|
| 288 | my ($imgref_urls) = giget(\@search_terms,$output_dir);
|
---|
| 289 |
|
---|
| 290 | my $gi_base = gi_url_base();
|
---|
| 291 | my $gi_query_url = gi_query_url(\@search_terms);
|
---|
| 292 |
|
---|
[8365] | 293 | $doc_obj->add_metadata ($section, "giquery", "<a href=\"$gi_base$gi_query_url\" target=giwindow>");
|
---|
[7487] | 294 | $doc_obj->add_metadata ($section, "/giquery", "</a>");
|
---|
| 295 |
|
---|
| 296 | for (my $i=1; $i<=2; $i++) {
|
---|
| 297 | my $img_filename = "$output_dir/img_$i.jpg";
|
---|
| 298 | my $dst_file = "img_$i.jpg";
|
---|
| 299 |
|
---|
| 300 | if (-e $img_filename) {
|
---|
| 301 | $doc_obj->associate_file($img_filename, $dst_file, "image/jpeg", $section);
|
---|
| 302 |
|
---|
[11834] | 303 | my $srcurl = "src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$dst_file\"";
|
---|
[7487] | 304 |
|
---|
| 305 | $doc_obj->add_metadata ($section, "img$i",
|
---|
| 306 | "<img $srcurl>");
|
---|
| 307 | $doc_obj->add_metadata ($section, "smallimg$i",
|
---|
| 308 | "<img $srcurl width=100>");
|
---|
| 309 |
|
---|
| 310 | my $imgref_url = $imgref_urls->[$i-1];
|
---|
| 311 |
|
---|
[8365] | 312 | $doc_obj->add_metadata ($section, "imgref$i", "<a href=\"$imgref_url\" target=giwindow>");
|
---|
[7487] | 313 | $doc_obj->add_metadata ($section, "/imgref$i", "</a>");
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | return 1;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
[15872] | 325 | # we want to use mp3:Title if its there, otherwise we'll use BasePlugin method
|
---|
[8519] | 326 | sub title_fallback
|
---|
| 327 | {
|
---|
| 328 | my $self = shift (@_);
|
---|
| 329 | my ($doc_obj,$section,$file) = @_;
|
---|
[7487] | 330 |
|
---|
[8519] | 331 | if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
|
---|
| 332 | my $mp3_title = $doc_obj->get_metadata_element ($section, "mp3:Title");
|
---|
| 333 | if (defined $mp3_title) {
|
---|
| 334 | $doc_obj->add_metadata ($section, "Title", $mp3_title);
|
---|
| 335 | }
|
---|
| 336 | else {
|
---|
[15872] | 337 | $self->BasePlugin::title_fallback($doc_obj, $section, $file);
|
---|
[8519] | 338 | }
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 |
|
---|
[7487] | 343 | 1;
|
---|
| 344 |
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 |
|
---|
| 351 |
|
---|
| 352 |
|
---|
| 353 |
|
---|
| 354 |
|
---|