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

Last change on this file since 29028 was 29028, checked in by jmt12, 10 years ago

Adding a configuration property (true/false enum) to allow for the disabling of keyframe extraction - this is so I can exercise finer control over CPU load during scale/parallel import testing

File size: 16.8 KB
Line 
1###########################################################################
2#
3# SimpleVideoPlugin.pm -- Plugin for multimedia with some simple video
4# processing
5#
6# A component of the Greenstone digital library software from the New
7# Zealand Digital Library Project at the University of Waikato, New
8# Zealand.
9#
10# Copyright (C) 2012 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
28package SimpleVideoPlugin;
29
30use File::Temp qw/ tempdir /;
31
32use BasePlugin;
33use MetadataRead;
34use util;
35use FileUtils;
36
37use strict;
38use warnings;
39no strict 'refs'; # allow filehandles to be variables and viceversa
40
41sub BEGIN
42{
43 @SimpleVideoPlugin::ISA = ('MetadataRead', 'BasePlugin');
44}
45
46my $arguments = [ { 'name' => "process_exp",
47 'desc' => "{BasePlugin.process_exp}",
48 'type' => "regexp",
49 'reqd' => "no",
50 'deft' => &get_default_process_exp() },
51 { 'name' => "streamingHQsize",
52 'desc' => "{VideoPlugin.streamingsize}",
53 'type' => "int",
54 'deft' => "720",
55 'reqd' => "no" },
56 { 'name' => "streamingHQVideoBitrate",
57 'desc' => "{VideoPlugin.streamingbitrate}",
58 'type' => "int",
59 'deft' => "496",
60 'reqd' => "no" },
61 { 'name' => "streamingHQAudioBitrate",
62 'desc' => "{VideoPlugin.streamingbitrate}",
63 'type' => "int",
64 'deft' => "80",
65 'reqd' => "no" },
66 { 'name' => "videoDeinterlacingFilter",
67 'desc' => "Activate a deinterlacing filter to increase the quality of TV footage",
68 'type' => "enum",
69 'list' => [{'name' => "true", 'desc' => "{common.true}"},
70 {'name' => "false", 'desc' => "{common.false}"}],
71 'deft' => "false",
72 'reqd' => "no" },
73 { 'name' => "isParallel",
74 'desc' => "Will the import use parallel processing? (maybe this should be set by parallel-import.pl somehow)",
75 'type' => "enum",
76 'list' => [{'name' => "true", 'desc' => "{common.true}"},
77 {'name' => "false", 'desc' => "{common.false}"}],
78 'deft' => "true",
79 'reqd' => "no" },
80 { 'name' => "isCluster",
81 'desc' => "Will the import be run on a cluster (multiple computers) or not (single computer - possibly multiple processors)",
82 'type' => "enum",
83 'list' => [{'name' => "true", 'desc' => "{common.true}"},
84 {'name' => "false", 'desc' => "{common.false}"}],
85 'deft' => "false",
86 'reqd' => "no" },
87 { 'name' => "separateIO",
88 'desc' => "copy and process the file locally (good for segregating IO cost)",
89 'type' => "enum",
90 'list' => [{'name' => "true", 'desc' => "{common.true}"},
91 {'name' => "false", 'desc' => "{common.false}"}],
92 'deft' => "false",
93 'reqd' => "no" },
94 { 'name' => "fixedCore",
95 'desc' => "Restrict the execution of Handbrake to a single core (0 = no restriction, > 0 use value-1'th core)",
96 'type' => "int",
97 'deft' => "0",
98 'reqd' => "no" },
99 { 'name' => "no_keyframes",
100 'desc' => "Disable keyframe extraction",
101 'type' => "enum",
102 'list' => [{'name' => "true", 'desc' => "{common.true}"},
103 {'name' => "false", 'desc' => "{common.false}"}],
104 'deft' => "false",
105 'reqd' => "no" },
106 ];
107
108my $options = { 'name' => "BasicVideoPlugin",
109 'desc' => "",
110 'abstract' => "no",
111 'inherits' => "yes",
112 'args' => $arguments };
113
114sub new
115{
116 my ($class) = shift (@_);
117 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
118 push(@$pluginlist, $class);
119
120 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
121 push(@{$hashArgOptLists->{"OptList"}},$options);
122 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
123 return bless $self, $class;
124}
125
126sub get_default_process_exp
127{
128 return '(?i)\.ts$';
129}
130
131sub get_oid_hash_type
132{
133 my $self = shift (@_);
134 return "hash_on_ga_xml";
135}
136
137sub process
138{
139 my $self = shift (@_);
140 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
141
142 if (!&FileUtils::isFilenameAbsolute($file) && $base_dir ne '')
143 {
144 $file = &FileUtils::filenameConcatenate($base_dir, $file);
145 }
146
147 print STDERR " * SimpleVideoPlugin processing: " . $file . "\n";
148
149 # - I have to add some text (yay, back to needing dummy text) otherwise the
150 # DocumentText formatting is ignored (?!?)
151 my $topsection = $doc_obj->get_top_section();
152 $doc_obj->add_utf8_text($topsection, "This is dummy text");
153
154
155 $file =~ /[\/]?([^\/]+)\.(?:ts)$/;
156 my $filename = $1;
157
158 # Optional date metadata (available on raw ReplayMe recordings)
159 if ($filename =~ /(\d\d\d\d)-(\d\d)-(\d\d)/)
160 {
161 my $date = $1 . $2 . $3;
162 $filename =~ s/[^a-z0-9]+/_/ig;
163 $filename =~ s/^_+|_+$//g;
164 $doc_obj->add_utf8_metadata($topsection,"Date",$date);
165 }
166
167 # Special Case: HDFS *only* supported by separateIO flag (you need to move
168 # the file out of HDFS to local filespace to allow MediaInfo and Handbrake
169 # to be run on it.
170 my $separate_io = $self->{'separateIO'};
171 if (&FileUtils::isHDFS($file))
172 {
173 $separate_io = 'true';
174 }
175 ###rint STDERR "[DEBUG] separate_io:" . $separate_io . "\n";
176
177 my $process_dir = $ENV{'GSDLCOLLECTDIR'};
178 # If we are in a cluster, then we don't want to be writing all the logs
179 # etc to the shared file system. Instead, we write to the tmp drive
180 if ($separate_io eq 'true')
181 {
182 $process_dir = &FileUtils::filenameConcatenate('/tmp', 'gsimport-' . $filename);
183 if (!&FileUtils::directoryExists($process_dir))
184 {
185 mkdir($process_dir, 0775);
186 }
187 }
188 my $logs_dir = &FileUtils::filenameConcatenate($process_dir, "logs");
189 if (!&FileUtils::directoryExists($logs_dir))
190 {
191 mkdir($logs_dir, 0775);
192 }
193 my $convert_log_path = &FileUtils::filenameConcatenate($logs_dir, 'convert-' . $filename . '.log');
194 my $pass_log_path = &FileUtils::filenameConcatenate($logs_dir, 'convert-' . $filename . '-pass');
195 my $tmp_dir = &FileUtils::filenameConcatenate($process_dir, "cached");
196 if (!&FileUtils::directoryExists($tmp_dir))
197 {
198 mkdir($tmp_dir, 0775);
199 }
200 $tmp_dir = &FileUtils::filenameConcatenate($tmp_dir, $filename);
201 if (!&FileUtils::directoryExists($tmp_dir))
202 {
203 mkdir($tmp_dir, 0775);
204 }
205
206 # If we are separating IO, then we also start by copying the file to
207 # the process directory (local tmp) as well
208 my $ivideo_path = $file;
209 if (!&FileUtils::isFilenameAbsolute($file) && $base_dir ne '')
210 {
211 $ivideo_path = &FileUtils::filenameConcatenate($base_dir, $file);
212 }
213 if ($separate_io eq 'true')
214 {
215 my $io_start = time();
216 print " - creating local copy of file: " . $ivideo_path . " [IOS:" . time() . "]\n";
217 my $local_ivideo_path = &FileUtils::filenameConcatenate($process_dir, $filename . ".ts");
218 &FileUtils::copyFiles($ivideo_path, $local_ivideo_path);
219 my $remote_size = &FileUtils::fileSize($ivideo_path);
220 my $local_size = &FileUtils::fileSize($local_ivideo_path);
221 if ($remote_size != $local_size)
222 {
223 die('Error! File copied is not of same size as original: ' . $remote_size . 'b != ' . $local_size . "b\n");
224 }
225 elsif (&FileUtils::fileExists($local_ivideo_path))
226 {
227 $ivideo_path = $local_ivideo_path;
228 print ' - copied! [IOE:' . time() . "]\n";
229 }
230 else
231 {
232 die('Error! Failed to copy file: ' . $ivideo_path . ' => ' . $local_ivideo_path . ' [IOE:' . time() . "]\n");
233 }
234 }
235
236 # 1. Use MediaInfo to extract important metadata
237 print ' - Extracting metadata using MediaInfo...';
238 my $mi_metadata = $self->getMetadata($ivideo_path);
239 my $input_video_duration = &parseDurationAsSeconds($mi_metadata->{'General'}->{'Duration'});
240 $doc_obj->add_utf8_metadata($topsection,"Duration",$mi_metadata->{'General'}->{'Duration'});
241 $doc_obj->add_utf8_metadata($topsection,"Format", 'multimedia (' . $mi_metadata->{'General'}->{'Format'} . ')');
242 if (defined $mi_metadata->{'General'}->{'File_size'})
243 {
244 $doc_obj->set_metadata_element($topsection, "FileSize", $mi_metadata->{'General'}->{'File_size'});
245 }
246 else
247 {
248 $doc_obj->set_metadata_element($topsection, "FileSize", &FileUtils::fileSize($ivideo_path));
249 }
250 if (defined $mi_metadata->{'Video'}->{'Format_Info'} && defined $mi_metadata->{'Video'}->{'Format'})
251 {
252 $doc_obj->add_utf8_metadata($topsection,"VideoFormat",$mi_metadata->{'Video'}->{'Format_Info'} . ' (' . $mi_metadata->{'Video'}->{'Format'} . ')');
253 }
254 if (defined $mi_metadata->{'Audio'}->{'Format_Info'} && defined $mi_metadata->{'Audio'}->{'Format'})
255 {
256 $doc_obj->add_utf8_metadata($topsection,"AudioFormat",$mi_metadata->{'Audio'}->{'Format_Info'} . ' (' . $mi_metadata->{'Audio'}->{'Format'} . ')');
257 }
258 $doc_obj->add_utf8_metadata($topsection,"Width",$mi_metadata->{'Video'}->{'Width'});
259 $doc_obj->add_utf8_metadata($topsection,"Height",$mi_metadata->{'Video'}->{'Height'});
260 print STDERR "Done!\n";
261
262 # 2. Convert into FLV, reprocess to make seekable, and associate
263 # - generate a path for our temporary converted video file
264 print STDERR " - Converting video to streamble format...\n";
265 my $ovideo_path = &FileUtils::filenameConcatenate($tmp_dir, 'gsv.mp4');
266 if (&FileUtils::fileExists($ovideo_path))
267 {
268 print " - Found existing converted video in cache!\n";
269 }
270 else
271 {
272 # - first conversion pass
273 print " - Convert using Handbrake\n";
274 my $streaming_HQ_size = $self->{'streamingHQsize'};
275 my $streaming_HQ_VideoBitrate = $self->{'streamingHQVideoBitrate'};
276 my $streaming_HQ_AudioBitrate = $self->{'streamingHQAudioBitrate'};
277 my $deinterlace = $self->{'videoDeinterlacingFilter'};
278 my $video_processing_parameters;
279 if (!$streaming_HQ_size || $streaming_HQ_size eq "fullsize")
280 {
281 $video_processing_parameters = "--strict-anamorphic";
282 }
283 else
284 {
285 $video_processing_parameters = "-w $streaming_HQ_size --loose-anamorphic";
286 }
287 if ($deinterlace eq "true")
288 {
289 $video_processing_parameters .= " --decomb";
290 }
291 # Default MenCoder options for x264
292 my $mencoder_options = 'ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0';
293 my $is_cluster = $self->{'isCluster'};
294 my $is_parallel = $self->{'isParallel'};
295 # If we are parallel processing on a single (presumably) multicore computer
296 # then we need to limit the number of threads (and hence CPUs) HandBrake
297 # will utilize in order to emulate true parallel processing (otherwise the
298 # first thread to get to HandBrake conversion will take up most the CPUs
299 # causing all other threads to wait anyway). It will interesting to test
300 # whether parallel processing or serial processing (with HandBrake parallel
301 # processing) is faster. *update* threads=1 *only* controls the encoding and
302 # several other parts of Handbrake can run parallel (demuxing etc). I've
303 # had to include a 'taskset' command to truely make Handbrake serial
304 if ($is_parallel eq 'true' && $is_cluster eq 'false')
305 {
306 $mencoder_options .= ':threads=1';
307 }
308 # Banish HandbrakeCLI to the (fixedCore-1)'th CPU if necessary
309 my $cmd = '';
310 if (defined $self->{'fixedCore'} && $self->{'fixedCore'} > 0)
311 {
312 $cmd .= 'taskset -c ' . ($self->{'fixedCore'} - 1) . ' ';
313 }
314 $cmd .= 'HandBrakeCLI -i "' . $ivideo_path . '" -t 1 -c 1 -f mp4 -O -o "' . $ovideo_path . '" ' . $video_processing_parameters . ' -e x264 -b ' . $streaming_HQ_VideoBitrate . ' -a 1 -E faac -6 dpl2 -R Auto -B ' . $streaming_HQ_AudioBitrate . ' -D 0.0 -x ' . $mencoder_options . ' > "' . $convert_log_path . '" 2>&1';
315 my $attempt_count = 0;
316 do
317 {
318 $attempt_count++;
319 ###rint "[DEBUG: Video conversion attempt #" . $attempt_count . ": |" . $cmd . "|]\n";
320 `$cmd`;
321 }
322 while ($attempt_count < 5 && !&FileUtils::fileExists($ovideo_path))
323 }
324 if (!&FileUtils::fileExists($ovideo_path))
325 {
326 die("Fatal Error! Failed to convert video: " . $ovideo_path . "\nReason:" . $! . "\n");
327 }
328 # Extra check - ensure the converted video is approximately the same duration
329 # as the input video, given or take around 5 seconds
330 my $output_raw_video_duration = &getDuration($ovideo_path);
331 my $output_video_duration = &parseDurationAsSeconds($output_raw_video_duration);
332 if (abs($input_video_duration - $output_video_duration) > 5)
333 {
334 print STDERR "!Warning! Output video does not have same duration as input video.\n";
335 }
336 print STDERR " - conversion done!\n";
337
338 # 3. Extract keyframes using hive
339 print STDERR " - extract keyframes...\n";
340 my $oshots_path = &FileUtils::filenameConcatenate($tmp_dir, 'shots.xml');
341 if (&FileUtils::fileExists($oshots_path))
342 {
343 print " - found existing keyframe images in cache\n";
344 }
345 else
346 {
347 print " - generating keyframe images using Hive2\n";
348 my $cmd = 'hive2_ffmpegsvn -o "' . $oshots_path . '" -k "' . $tmp_dir . '" "' . $ovideo_path . '" >> "' . $convert_log_path . '" 2>&1';
349 ###print "[cmd: " . $cmd . "]\n";
350 `$cmd`;
351 }
352 if (!&FileUtils::fileExists($oshots_path))
353 {
354 die("Fatal Error! Failed to extract keyframe images: " . $oshots_path . "\nReason:" . $! . "\n");
355 }
356 print STDERR " - keyframes extracted!\n";
357
358
359 # 4. Associate files (copies back to shared space if IO separated)
360 print STDERR " - Associate derived files to doc_obj... ";
361 # - associate streamable video
362 $doc_obj->associate_file($ovideo_path,'gsv.mp4','video/mp4',$topsection);
363 # - associate all of the JPGs found in the temp directory
364 opendir(my $dh, $tmp_dir);
365 my @shots = readdir($dh);
366 closedir($dh);
367 my $thumbnail = 0;
368 foreach my $shot (sort @shots)
369 {
370 my $shot_path = &FileUtils::filenameConcatenate($tmp_dir, $shot);
371 if ($shot =~ /.jpg$/)
372 {
373 if (!$thumbnail)
374 {
375 $doc_obj->add_utf8_metadata($topsection,"Thumbnail",$shot);
376 $thumbnail = 1;
377 }
378 $doc_obj->add_utf8_metadata($topsection,"Keyframe",$shot);
379 $doc_obj->associate_file($shot_path,$shot,"image/jpeg",$topsection);
380 }
381 }
382 print STDERR "Done!\n";
383
384 # 5. Done! Cleanup.
385 print STDERR "SimpleVideoPlugin: Complete! [IOS:" . time() . "]\n";
386 return 1;
387}
388
389
390## @function getDuration()
391#
392sub getDuration
393{
394 my ($video_path) = @_;
395 my $cmd = 'mediainfo --Inform="General;%Duration/String%" "' . $video_path . '" 2>&1';
396 my $duration = `$cmd`;
397 return $duration;
398}
399## getDuration() ##
400
401
402## @function getMetadata()
403#
404sub getMetadata
405{
406 my ($self, $ivideo_path) = @_;
407 my $cmd = 'mediainfo --Output=XML "' . $ivideo_path . '" 2>&1';
408 ###rint "[DEBUG] command: " . $cmd . "\n";
409 my $metadata_xml = `$cmd`;
410 ###rint "[DEBUG] result: " . $metadata_xml . "\n\n";
411 my @lines = split(/\r?\n/, $metadata_xml);
412 my $metadata = {'Unknown'=>{}};
413 my $metadata_type = 'Unknown';
414 foreach my $line (@lines)
415 {
416 if ($line =~ /<track type="(.+)">/)
417 {
418 $metadata_type = $1;
419 if (!defined $metadata->{$metadata_type})
420 {
421 $metadata->{$metadata_type} = {};
422 }
423 }
424 elsif ($line =~ /<([^>]+)>(.+)<\/[^>]+>/)
425 {
426 my $field = $1;
427 my $value = $2;
428 $metadata->{$metadata_type}->{$field} = $value;
429 }
430 }
431 return $metadata;
432}
433## getMetadata() ##
434
435
436## @function parseDurationAsSeconds()
437#
438sub parseDurationAsSeconds
439{
440 my ($duration_str) = @_;
441 my $seconds = 0;
442 # h - hours
443 if ($duration_str =~ /(\d+)h/)
444 {
445 $seconds += $1 * 60 * 60;
446 }
447 # mn - minutes
448 if ($duration_str =~ /(\d+)mn/)
449 {
450 $seconds += $1 * 60;
451 }
452 # s - seconds
453 if ($duration_str =~ /(\d+)s/)
454 {
455 $seconds += $1;
456 }
457 # ms - milliseconds (ignore - I'll be lucky to match to the closest second)
458 return $seconds;
459}
460## parseDurationAsSeconds() ##
461
4621;
463
464
465
466
467
468
469
470
471
472
473
Note: See TracBrowser for help on using the repository browser.