source: main/trunk/greenstone2/perllib/plugins/MediainfoOGVPlugin.pm@ 22705

Last change on this file since 22705 was 22663, checked in by mdewsnip, 14 years ago

Changed "srclink_file" metadata to always contain the filename, instead of referencing another metadata element. This is so the C++ code can escape underscores in the filename values correctly.

File size: 8.3 KB
Line 
1###########################################################################
2#
3# MediainfoOGVPlugin.pm -- Plugin for OGV multimedia files
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 Gordon W. Paynter
10# Copyright (C) 2001 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful, but
18# WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20# General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# MediainfoOGVPlugin - a plugin for OGV multimedia files
29
30# This is a simple Plugin for importing OGV multimedia files.
31# Mediainfo will retrieve the metadata of the file. A fictional document will
32# be created for every such file, and the file itself will be passed
33# to Greenstone as the "associated file" of the document.
34
35# Here's an example where it is useful: I have a collection of
36# ogg movie files with names like conference_20080402.ogv.
37# I add this line to the collection configuration file:
38
39# plugin MediainfoOGVPlugin -process_exp "*.ogv" -assoc_field "movie"
40
41# A document is created for each movie, with the associated movie
42# file's name in the "movie" metadata field.
43
44# Te plugin also add some metadata :
45# Duration (in seconds), filesize, title, artist, location, organization,
46# date, contact, copyright.
47
48
49
50package MediainfoOGVPlugin;
51
52use BasePlugin;
53
54use strict;
55no strict 'refs'; # allow filehandles to be variables and viceversa
56no strict 'subs';
57
58sub BEGIN {
59 @MediainfoOGVPlugin::ISA = ('BasePlugin');
60}
61
62my $arguments =
63 [ { 'name' => "process_exp",
64 'desc' => "{BasePlugin.process_exp}",
65 'type' => "regexp",
66 'deft' => &get_default_process_exp(),
67 'reqd' => "no" },
68 { 'name' => "assoc_field",
69 'desc' => "{MediainfoOGVPlugin.assoc_field}",
70 'type' => "string",
71 'deft' => "Movie",
72 'reqd' => "no" },
73 ];
74
75my $options = { 'name' => "MediainfoOGVPlugin",
76 'desc' => "{MediainfoOGVPlugin.desc}",
77 'abstract' => "no",
78 'inherits' => "yes",
79 'args' => $arguments };
80
81
82sub new {
83 my ($class) = shift (@_);
84 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
85 push(@$pluginlist, $class);
86
87 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
88 push(@{$hashArgOptLists->{"OptList"}},$options);
89
90 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
91
92 return bless $self, $class;
93}
94
95sub init {
96 my $self = shift(@_);
97 my ($verbosity, $outhandle, $failhandle) = @_;
98
99 $self->BasePlugin::init($verbosity, $outhandle, $failhandle);
100
101 # Check that Mediainfo is installed and available on the path (except for Windows 95/98)
102 if (!($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT())) {
103 my $result = `mediainfo 2>&1`;
104 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
105 $self->{'mediainfo_not_installed'} = 1;
106 }
107 }
108
109}
110sub get_default_process_exp {
111 return q^(?i)\.ogv$^;
112}
113
114# MediainfoOGVPlugin processing of doc_obj.
115
116sub process {
117 my $self = shift (@_);
118 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
119
120 #print STDERR "\n\n**** START of processing MediainfoOGVPlugin\n\n";
121
122 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
123 my $outhandle = $self->{'outhandle'};
124 my $verbosity = $self->{'verbosity'};
125
126 # check the filename is okay - do we need this??
127 if ($filename_full_path eq "" || $filename_no_path eq "") {
128 print $outhandle "MediainfoOGVPlugin: couldn't process \"$filename_no_path\"\n";
129 return undef;
130 }
131
132 # Add the file as an associated file ...
133 my $section = $doc_obj->get_top_section();
134 my $file_format = "ogv";
135 my $mime_type = "video/ogg";
136 my $assoc_field = $self->{'assoc_field'};
137
138 # The assocfilename is the url-encoded version of the utf8 filename
139 my $assoc_file = $doc_obj->get_assocfile_from_sourcefile();
140
141 $doc_obj->associate_file($filename_full_path, $assoc_file, $mime_type, $section);
142 $doc_obj->add_metadata ($section, "FileFormat", $file_format);
143 $doc_obj->add_metadata ($section, "dc.Format", $file_format);
144 $doc_obj->add_metadata ($section, "MimeType", $mime_type);
145 $doc_obj->add_utf8_metadata ($section, $assoc_field, $doc_obj->get_source()); # Source metadata is already in utf8
146
147 $doc_obj->add_metadata ($section, "srclink_file", $doc_obj->get_sourcefile());
148 $doc_obj->add_metadata ($section, "srcicon", "_iconogg_");
149
150 # we have no text - add dummy text and NoText metadata
151 $self->add_dummy_text($doc_obj, $section);
152
153 if (defined $self->{'mediainfo_not_installed'}) {
154 # can't do any extracted
155 print STDERR "Mediainfo not installed, so can't extract metadata\n";
156 return 1;
157 }
158
159
160 # Retrieve the file metadata
161 my $command = "mediainfo --inform=\"General;%Duration/String2%|%Duration%|%FileSize/String2%|%Performer%|%Title%|%Recorded_Date%|%Recorded/Location%|%Producer%|%Copyright%|%LICENSE%|%Publisher%\" \"$filename_full_path\"";
162 print $outhandle "$command\n" if ($verbosity > 2);
163
164 # execute the mediainfo command and store the output of execution in $videodata
165 # my $video_metadata = `$command`; # backticks operator way
166 # Another way to execute the command: better experience with this than with backticks operator above:
167 # use open() to read the output of executing the command (which is piped through to a handle using the | operator)
168 my $video_metadata;
169 if (open(VMIN,"$command|")) {
170 my $line;
171
172 # we read a line of output at a time
173 while (defined ($line=<VMIN>)) {
174 #print STDERR "***** line = $line\n";
175
176 $video_metadata .= $line;
177 }
178
179 close(VMIN);
180 }
181
182 print $outhandle "$video_metadata\n" if ($verbosity > 2);
183
184 # There are 10 fields separated by |, split these into ordered, individual-named variables
185 my ($FormattedDuration,$Duration,$Filesize,$Artist,$Title,$Date,$Location,$Organization,$Copyright,$License,$Contact) = split(/\|/,$video_metadata);
186 $Duration = int($Duration/1000);
187 $Artist =~ s/\\047/\'/g; # Perl's way of doing: $artist = `echo $artist | sed \"s/\\047/'/g\"`;
188 $Title =~ s/\\047/\'/g; # $title = `echo $title | sed \"s/\\047/'/g\"`;
189
190 print $outhandle "RESULT = $FormattedDuration\n$Duration\n$Filesize\n$Artist\n$Title\n$Date\n$Location\n$Organization\n$Copyright\n$License\n$Contact\n" if ($verbosity > 2);
191
192
193 $doc_obj->add_metadata ($section, "FormattedDuration", $FormattedDuration);
194 $doc_obj->add_metadata ($section, "Duration", $Duration);
195
196
197 $doc_obj->add_metadata($section,"FileSize",$Filesize);
198 $doc_obj->add_utf8_metadata($section,"dc.Creator",$Artist);
199 $doc_obj->add_utf8_metadata($section,"Artist",$Artist);
200 $doc_obj->add_utf8_metadata($section,"dc.Title",$Title);
201 $doc_obj->add_utf8_metadata($section,"Title",$Title);
202 $doc_obj->add_utf8_metadata($section,"dc.Date",$Date);
203 $doc_obj->add_utf8_metadata($section,"Date",$Date);
204 $doc_obj->add_utf8_metadata($section,"dc.Coverage",$Location);
205 $doc_obj->add_utf8_metadata($section,"Location",$Location);
206 $doc_obj->add_utf8_metadata($section,"dc.Publisher",$Organization);
207 $doc_obj->add_utf8_metadata($section,"Organization",$Organization);
208 $doc_obj->add_utf8_metadata($section,"dc.Rights",$Copyright);
209 $doc_obj->add_utf8_metadata($section,"Copyright",$Copyright);
210 $doc_obj->add_utf8_metadata($section,"dc.accessRights",$License);
211 $doc_obj->add_utf8_metadata($section,"License",$License);
212 $doc_obj->add_utf8_metadata($section,"dc.Contributor",$Contact);
213 $doc_obj->add_utf8_metadata($section,"Contact",$Contact);
214
215
216 #print STDERR "\n\n**** END of MediainfoOGVPlugin\n\n";
217
218 return 1;
219}
220
221
2221;
223
224
225
226
227
228
229
230
231
232
233
Note: See TracBrowser for help on using the repository browser.