source: extensions/gsdl-video/trunk/perllib/plugins/VideoPlugin.pm@ 19828

Last change on this file since 19828 was 19828, checked in by davidb, 15 years ago

Continued development of plugins to work smoothly with Flash player

File size: 28.2 KB
Line 
1######################################################################
2#
3# VideoPlugin.pm -- plugin for processing video files
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# -- Largely modeled on how ImagePlugin works
27# -- Can convert to audio as well as video
28
29package VideoPlugin;
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
33no strict 'subs';
34
35use XMLParser;
36use gsprintf;
37
38use MultimediaPlugin;
39use VideoConverter;
40use ReadXMLFile;
41
42sub BEGIN {
43 @VideoPlugin::ISA = ('MultimediaPlugin', 'VideoConverter', 'ReadXMLFile');
44}
45
46
47my $arguments =
48 [ { 'name' => "process_exp",
49 'desc' => "{BasePlugin.process_exp}",
50 'type' => "regexp",
51 'deft' => &get_default_process_exp(),
52 'reqd' => "no" } ];
53
54
55my $options = { 'name' => "VideoPlugin",
56 'desc' => "{VideoPlugin.desc}",
57 'abstract' => "no",
58 'inherits' => "yes",
59 'args' => $arguments };
60
61sub new {
62 my ($class) = shift (@_);
63 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
64 push(@$pluginlist, $class);
65
66 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
67 push(@{$hashArgOptLists->{"OptList"}},$options);
68
69 new VideoConverter($pluginlist, $inputargs, $hashArgOptLists);
70 my $self = new MultimediaPlugin($pluginlist, $inputargs, $hashArgOptLists);
71
72 if ($self->{'info_only'}) {
73 # don't worry about creating the XML parser as all we want is the
74 # list of plugin options
75 return bless $self, $class;
76 }
77
78
79 # create XML::Parser object for parsing keyframe files (produced by hive)
80 my $parser = new XML::Parser('Style' => 'Stream',
81 'Pkg' => 'VideoPlugin',
82 'PluginObj' => $self,
83 'Namespaces' => 1, # strip out namespaces
84 'Handlers' => {'Char' => \&Char,
85 'XMLDecl' => \&XMLDecl,
86 'Entity' => \&Entity,
87 'Doctype' => \&Doctype,
88 'Default' => \&Default
89 });
90
91 $self->{'parser'} = $parser;
92
93
94 return bless $self, $class;
95}
96
97
98
99sub begin {
100 my $self = shift (@_);
101 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
102
103 $self->SUPER::begin(@_);
104 $self->VideoConverter::begin(@_);
105}
106
107
108sub init {
109 my $self = shift (@_);
110 my ($verbosity, $outhandle, $failhandle) = @_;
111
112 $self->SUPER::init(@_);
113 $self->VideoConverter::init(@_);
114}
115
116
117sub get_default_process_exp {
118 my $self = shift (@_);
119
120 return q^(?i)\.(mpe?g|flv|mov|qt|wmv|vob|avi|mp4|m4v)$^;
121}
122
123
124sub extract_keyframes
125{
126 my $self = shift (@_);
127 my ($doc_obj,$originalfilename,$filename) = @_;
128
129 my $section = $doc_obj->get_top_section();
130
131 my $output_dir = $self->{'cached_dir'};
132 my $ivideo_root = $self->{'cached_file_root'};
133
134 # Generate the keyframes with ffmpeg and hive
135 my ($keyframe_cmd,$okeyframe_filename) = $self->keyframe_cmd($originalfilename || $filename);
136
137 my $keyframe_options = { @{$self->{'ffmpeg_monitor'}},
138 'message_prefix' => "Keyframe",
139 'message' => "Extracting keyframes" };
140
141 $self->run_cached_general_cmd($keyframe_cmd,$okeyframe_filename,$keyframe_options);
142 $self->parse_shot_xml();
143 $self->associate_keyframes($doc_obj,$section);
144}
145
146
147
148sub extract_thumbnail
149{
150 my $self = shift (@_);
151 my ($doc_obj,$filename,$convertto_regenerated,$thumbnailtype,
152 $thumbnail_width, $thumbnail_height) = @_;
153
154 my $section = $doc_obj->get_top_section();
155
156 my $output_dir = $self->{'cached_dir'};
157 my $ivideo_root = $self->{'cached_file_root'};
158
159 my $verbosity = $self->{'verbosity'};
160 my $outhandle = $self->{'outhandle'};
161
162
163 # Generate the thumbnail with convert, a la ImagePlug
164
165 my $thumbnailfile = &util::filename_cat($output_dir,"$ivideo_root.$thumbnailtype");
166
167
168 my $optionally_run_general_cmd = "run_uncached_general_cmd";
169 if ($self->{'enable_cache'}) {
170 $optionally_run_general_cmd
171 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
172 }
173
174 my ($thumb_cmd ,$othumb_filename)
175 = $self->keyframe_thumbnail_cmd($filename,$thumbnailfile,$thumbnail_width,$thumbnail_height);
176
177 my $thumb_options = { 'verbosity' => $verbosity,
178 'outhandle' => $outhandle,
179 'message_prefix' => "Thumbnail",
180 'message' => "Generating thumbnail" };
181
182 my ($thumb_regenerated,$thumb_result,$thumb_had_error)
183 = $self->$optionally_run_general_cmd($thumb_cmd,$thumbnailfile,$thumb_options);
184
185 # Add the thumbnail as an associated file ...
186 if (-e "$thumbnailfile") {
187 $doc_obj->associate_file("$thumbnailfile", "thumbnail.$thumbnailtype",
188 "image/$thumbnailtype",$section);
189 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
190 $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
191
192 $doc_obj->add_utf8_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
193### $doc_obj->add_utf8_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Thumb]\">");
194 }
195
196 # Extract Thumnail metadata from convert output
197 if ($thumb_result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
198 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
199 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
200 }
201 else {
202 # Two reasons for getting to here:
203 # 1.thumbnail was generated by ffmpeg, not imagemagick convert
204 # 2.thumbnail was cached, so imagemagick convert was not run
205 # Either way, the solution is the same:
206 # => run "identify $thumbnailfile" and parse result
207
208 $thumb_result = `identify \"$thumbnailfile\"`;
209
210 if ($thumb_result =~ m/([0-9]+)x([0-9]+)/) {
211 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
212 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
213 }
214 }
215}
216
217
218sub extract_keyframes_montage
219{
220 my $self = shift (@_);
221 my ($doc_obj,$filename,$thumbnailtype) = @_;
222
223 my $section = $doc_obj->get_top_section();
224
225 my $output_dir = $self->{'cached_dir'};
226 my $ivideo_root = $self->{'cached_file_root'};
227
228
229 # Generate the mosaic with 'montage'
230 my $montagefile = &util::filename_cat($output_dir,"$ivideo_root\_montage.$thumbnailtype");
231
232 my ($montage_cmd,$omontage_filename)
233 = $self->keyframe_montage_cmd($filename,$montagefile);
234
235 my $montage_options = { 'message_prefix' => "Montage",
236 'message' => "Generating montage" };
237
238 my ($montage_result,$montage_had_error)
239 = $self->run_general_cmd($montage_cmd,$montage_options);
240
241 # Add the montage as an associated file ...
242 if (-e "$montagefile") {
243 $doc_obj->associate_file("$montagefile", "montage.$thumbnailtype",
244 "image/$thumbnailtype",$section);
245 $doc_obj->add_metadata ($section, "MontageType", $thumbnailtype);
246 $doc_obj->add_metadata ($section, "Montage", "montage.$thumbnailtype");
247
248 $doc_obj->add_utf8_metadata ($section, "montageicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Montage]\" >");
249 }
250}
251
252
253
254sub extract_screenview
255{
256 my $self = shift (@_);
257 my ($doc_obj,$filename,$screenviewtype, $screenview_width,$screenview_height) = @_;
258
259 my $section = $doc_obj->get_top_section();
260
261 my $output_dir = $self->{'cached_dir'};
262 my $ivideo_root = $self->{'cached_file_root'};
263
264
265 # make the screenview image
266
267 my $screenviewfilename = &util::filename_cat($output_dir,"$ivideo_root.$screenviewtype");
268
269
270 my ($screenview_cmd,$oscreenview_filename)
271 = $self->keyframe_thumbnail_cmd($filename,$screenviewfilename,$screenview_width,$screenview_height);
272
273 my $screenview_options = { 'message_prefix' => "Screenview",
274 'message' => "Generating screenview image" };
275
276 my ($result,$had_error)
277 = $self->run_general_cmd($screenview_cmd,$screenview_options);
278
279 # get screenview dimensions, size and type
280 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
281 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
282 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
283 }
284 else {
285 $doc_obj->add_metadata ($section, "ScreenWidth", $screenview_width);
286 $doc_obj->add_metadata ($section, "ScreenHeight", $screenview_height);
287 }
288
289 #add the screenview as an associated file ...
290 if (-e "$screenviewfilename") {
291 $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
292 "image/$screenviewtype",$section);
293 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
294 $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
295
296 $doc_obj->add_utf8_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
297 } else {
298 my $outhandle = $self->{'outhandle'};
299 print $outhandle "VideoPlugin: couldn't find \"$screenviewfilename\"\n";
300 }
301}
302
303
304# Create the keyframes, thumbnail and screenview images, and discover
305# the Video's size, width, and height using the ffmpeg utility.
306
307sub run_convert {
308 my $self = shift (@_);
309 my $base_dir = shift (@_);
310 my $filename = shift (@_); # filename with full path
311 my $file = shift (@_); # filename without path
312 my $doc_obj = shift (@_);
313
314 my $section = $doc_obj->get_top_section();
315
316 my $verbosity = $self->{'verbosity'};
317 my $outhandle = $self->{'outhandle'};
318
319 # check the filename is okay
320 return 0 if ($file eq "" || $filename eq "");
321
322 my $minimumsize = $self->{'minimumsize'};
323 if (defined $minimumsize && (-s $filename < $minimumsize)) {
324 print $outhandle "VideoPlugin: \"$filename\" too small, skipping\n"
325 if ($verbosity > 1);
326 }
327
328 my ($video_type, $video_width, $video_height, $video_duration, $video_size,
329 $vcodec,$vfps,$atype,$afreq,$achan,$arate)
330 = &VideoConverter::identify($filename, $outhandle, $verbosity);
331
332 if ($vfps eq "unknown") {
333 print $outhandle "Unknown framerate, defaulting to 25 frames per second.\n";
334 $vfps = 25;
335 }
336
337 my ($dur_hour,$dur_min,$dur_sec)
338 = ($video_duration =~ m/(\d+):(\d+):(\d+\.\d+)/);
339 my $total_dur_secs = $dur_hour*3600 + $dur_min*60 + $dur_sec;
340
341 $self->{'video-fps'} = $vfps;
342 $self->{'num-total-frames'} = $total_dur_secs * $vfps;
343
344 # Convert the video to a new type (if required).
345 my $converttotype = $self->{'converttotype'};
346 my $converttosize = $self->{'converttosize'};
347
348 # shorten duration prcessed for experimentation purposes
349 my $exp_duration = undef;
350
351 my $excerpt_duration = $self->{'excerpt_duration'};
352
353 if ((defined $excerpt_duration) && ($excerpt_duration ne "")) {
354 $exp_duration = $excerpt_duration;
355 my ($hh,$mm,$ss,$ms);
356
357 if ($exp_duration =~ m/^(\d\d):(\d\d):(\d\d)\.?(\d\d)?/) {
358 $hh = $1;
359 $mm = $2;
360 $ss = $3;
361 $ms = $4;
362 }
363 else {
364 # assume value is in seconds
365 $hh = 0;
366 $mm = 0;
367 $ss = $exp_duration;
368 $ms = 0;
369 }
370
371 my $excerpt_dur_in_secs = $hh * 3600 + $mm * 60 + $ss;
372
373 if ($excerpt_dur_in_secs < $total_dur_secs) {
374 $self->{'num-total-frames'} = $excerpt_dur_in_secs * $vfps; # override calculation for full length duration
375 }
376 else {
377 # clip is already shorter than requested video excerpt duration
378 # set exp_duration back to undefined
379 $exp_duration = undef;
380 }
381 }
382
383
384 if (defined $exp_duration)
385 {
386 print $outhandle "Only encoding first $exp_duration of video.\n";
387 $self->{'exp_duration'} = $exp_duration;
388 }
389
390 my $ascii_only_filenames = $self->{'use_ascii_only_filenames'};
391 $self->init_cache_for_file($filename);
392
393 my $originalfilename = undef;
394 my $type = "unknown";
395
396 my $output_dir = $self->{'cached_dir'};
397 my $ivideo_root = $self->{'cached_file_root'};
398
399 my $convertto_regenerated = 0;
400 if (($converttotype ne "" && $filename =~ m/$converttotype$/i) ||
401 (($converttosize ne "" && $converttosize ne $video_width) || ($converttosize ne "" && $converttosize ne $video_height))) {
402 if ($converttotype eq "") {
403 # in this block because the video width x height different to original
404 # => set (for this call to run_convert only) converttotype
405 # to input file extension
406 ($converttotype) = ($filename =~ m/\.(.*?)$/);
407 }
408
409 $originalfilename = $filename;
410
411 $file = "$ivideo_root.$converttotype";
412 $filename = &util::filename_cat($output_dir,$file);
413
414 my $s_opt = $self->optional_frame_scale($converttosize,$video_width,$video_height);
415 my $exp_duration = $self->{'exp_duration'};
416 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
417
418 my $main_opts = "-y $t_opt $s_opt";
419
420
421 my $originalfilename_gsdlenv = $self->gsdlhome_independent($originalfilename);
422 my $filename_gsdlenv = $self->gsdlhome_independent($filename);
423
424
425 my $convertto_command = "ffmpeg $main_opts -i \"$originalfilename_gsdlenv\"";
426 $convertto_command .= " -ar 22050" if ($converttotype eq "flv");
427 $convertto_command .= " -y \"$filename_gsdlenv\"";
428
429 my $convertto_result;
430 my $convertto_error;
431
432 my $convertto_options = { @{$self->{'ffmpeg_monitor'}},
433 'message_prefix' => "Convert to",
434 'message' => "Converting video to $converttotype" };
435
436 ($convertto_regenerated,$convertto_result,$convertto_error)
437 = $self->run_cached_general_cmd($convertto_command,$filename,$convertto_options);
438
439 $type = $converttotype;
440 }
441
442
443 # Add the video metadata
444
445 my $file_unicode = pack("U0C*", map { ord($_) } split(//,$file)); # force explicitly to unicode
446
447 $file_unicode =~ s/\x{2018}|\x{2019}|\x{201C}|\x{201D}//g; # remove smart quotes as cause problem in URL for video server
448 $file_unicode =~ s/\x{2013}/\-/g; # change en-dash to '-' as again causes problems for video server
449
450## print STDERR "**** file metadata = ", &gsprintf::debug_unicode_string($file_unicode), "\n";
451
452
453 # split filename on char if specified
454 if ($file_unicode =~ m/\-/) {
455
456 my @file_split = split(/\s*\-\s*/,$file_unicode);
457 my $creator = shift @file_split;
458 my $title = shift @file_split;
459
460 $title =~ s/\..*?$//;
461 $title =~ s/^(.)/\u$1/;
462
463 $doc_obj->add_utf8_metadata($section,"Title",$title);
464
465 my @creator_split = split(/\s+and\s+/,$creator);
466 foreach my $c (@creator_split) {
467 $doc_obj->add_utf8_metadata($section,"Creator",$c);
468 }
469 }
470
471 $file = $file_unicode;
472 my $filemeta = $self->filename_to_utf8_metadata($file);
473 my $filemeta_url_safe = $self->url_safe($filemeta);
474
475 $doc_obj->add_utf8_metadata ($section, "Video", $filemeta_url_safe);
476
477 # Also want to set filename as 'Source' metadata to be
478 # consistent with other plugins
479 $doc_obj->add_utf8_metadata ($section, "Source", $filemeta_url_safe);
480
481
482 if ($video_type ne " ") {
483 $type = $video_type;
484 }
485
486 $doc_obj->add_metadata ($section, "FileFormat", $type);
487 $doc_obj->add_metadata ($section, "FileSize", $video_size);
488
489 $doc_obj->add_metadata ($section, "VideoType", $video_type);
490 $doc_obj->add_metadata ($section, "VideoWidth", $video_width);
491 $doc_obj->add_metadata ($section, "VideoHeight", $video_height);
492 $doc_obj->add_metadata ($section, "VideoDuration", $video_duration);
493 $doc_obj->add_metadata ($section, "VideoSize", $video_size);
494
495 $doc_obj->add_metadata ($section, "VideoCodec", $vcodec);
496 $doc_obj->add_metadata ($section, "VideoFPS", $vfps);
497
498 $doc_obj->add_metadata ($section, "AudioType", $atype);
499 $doc_obj->add_metadata ($section, "AudioFreq", $afreq);
500 $doc_obj->add_metadata ($section, "AudioChannels", $achan);
501 $doc_obj->add_metadata ($section, "AudioRate", $arate);
502
503 $doc_obj->add_utf8_metadata ($section, "srclink",
504 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Video]\">");
505 $doc_obj->add_utf8_metadata ($section, "/srclink", "</a>");
506 $doc_obj->add_metadata ($section, "srcicon", "[VideoType]");
507
508 # Add the image as an associated file
509 $doc_obj->associate_file($filename,$file,"video/$type",$section);
510
511
512 if ($self->{'extractkeyframes'}) {
513 $self->extract_keyframes($doc_obj,$originalfilename,$filename);
514 }
515
516 my $streamable_regenerated = 0;
517
518 if ($self->{'enable_streaming'}) {
519 $streamable_regenerated
520 = $self->enable_full_streaming($doc_obj,
521 $originalfilename,$filename,
522 $convertto_regenerated,
523 $video_width,$video_height);
524 }
525
526
527 my $thumbnailsize = $self->{'thumbnailsize'} || 100;
528 my $thumbnailtype = $self->{'thumbnailtype'} || 'jpg';
529
530
531 if ($self->{'create_thumbnail'} eq "true") {
532
533 my $thumbnail_width;
534 my $thumbnail_height;
535
536 if ($video_width>$video_height) {
537 my $scale_ratio = $video_height / $video_width;
538 $thumbnail_width = $thumbnailsize;
539 $thumbnail_height = int($thumbnailsize * $scale_ratio);
540 }
541 else {
542 my $scale_ratio = $video_width / $video_height;
543 $thumbnail_width = int($thumbnailsize * $scale_ratio);
544 $thumbnail_height = $thumbnailsize;
545 }
546
547 # for some video formats, extracted size needs to be multiple of 2
548 $thumbnail_width = int($thumbnail_width/2) * 2;
549 $thumbnail_height = int($thumbnail_height/2) * 2;
550
551 $self->extract_thumbnail($doc_obj,$filename,$convertto_regenerated,
552 $thumbnailtype,
553 $thumbnail_width,$thumbnail_height);
554 }
555
556
557 if ($self->{'extractkeyframes'}) {
558 $self->extract_keyframes_montage($doc_obj,$filename,$thumbnailtype);
559 }
560
561 my $screenviewsize = $self->{'screenviewsize'};
562 my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
563
564 # Make a screen-sized version of the picture if requested
565 if ($self->{'create_screenview'} eq "true") {
566
567 # To do: if the actual image smaller than the screenview size,
568 # we should use the original !
569
570 my $screenview_width;
571 my $screenview_height;
572
573 if ($video_width>$video_height) {
574 my $scale_ratio = $video_height / $video_width;
575 $screenview_width = $screenviewsize;
576 $screenview_height = int($screenviewsize * $scale_ratio);
577 }
578 else {
579 my $scale_ratio = $video_width / $video_height;
580 $screenview_width = int($screenviewsize * $scale_ratio);
581 $screenview_height = $screenviewsize;
582 }
583
584
585 # for some video formats, extracted size needs to be multiple of 2
586 $screenview_width = int($screenview_width/2) * 2;
587 $screenview_height = int($screenview_height/2) * 2;
588
589 $self->extract_screenview($doc_obj,$filename,
590 $screenviewtype,
591 $screenview_width,$screenview_height);
592 }
593
594 return $type;
595}
596
597
598
599
600sub read_into_doc_obj {
601 my $self = shift (@_);
602 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
603
604 my ($rv,$doc_obj) = $self->SUPER::read_into_doc_obj(@_);
605
606 if ($rv != 1) {
607 return ($rv,$doc_obj);
608 }
609
610
611 if (($self->{'enablestreaming'}) && ($self->{'extractkeyframes'})) {
612 my $section = $doc_obj->get_top_section();
613 my $oflash_filename = $self->{'oflash_filename'};
614 my ($streamkeyframes_cmd,$ostreamkeyframes_filename)
615 = $self->streamkeyframes_cmd($oflash_filename,$doc_obj,$section);
616
617 my $verbosity = $self->{'verbosity'};
618
619 my $streamkeyframes_options = { @{$self->{'flvtool2_monitor'}},
620 'message_prefix' => "Stream Keyframes",
621 'message' => "Reprocessing video stream to add keyframes on timeline" };
622
623 $self->run_general_cmd($streamkeyframes_cmd,$streamkeyframes_options);
624 }
625
626 return ($rv,$doc_obj);
627}
628
629
630
631
632sub output_distributed_keyframes
633{
634 my ($self) = shift @_;
635 my ($timeline,$num_dist_frames) = @_;
636
637 my $num_total_frames = $self->{'num-total-frames'};
638
639 my $frame_delta = $num_total_frames/$num_dist_frames;
640
641 my $closest_to = $frame_delta;
642 my $prev_t = 0;
643
644# print STDERR "*** num total frames = $num_total_frames\n";
645# print STDERR "*** frame delta = $frame_delta\n";
646
647 foreach my $curr_t (sort { $a <=> $b } keys %$timeline)
648 {
649# print STDERR "*** curr_t = $curr_t\n";
650
651 if ($curr_t>$closest_to) {
652 # decide if previous t (prev_t_ or current t (curr_t) is closest
653
654 my $timeline_rec;
655
656 my $prev_t_dist = $closest_to - $prev_t;
657 my $curr_t_dist = $curr_t - $closest_to;
658
659 if ($curr_t_dist <= $prev_t_dist)
660 {
661 $timeline_rec = $timeline->{$curr_t}
662 }
663 else
664 {
665 $timeline_rec = $timeline->{$prev_t}
666 }
667
668 my $name = $timeline_rec->{'name'};
669 my $timestamp = $timeline_rec->{'timestamp'};
670 my $thumb = $timeline_rec->{'thumb'};
671
672
673 print CUEOUT " <metatag event=\"onCuePoint\" overwrite=\"true\">\n";
674 print CUEOUT " <name>$name</name>\n";
675 print CUEOUT " <timestamp>$timestamp</timestamp>\n";
676 print CUEOUT " <parameters>\n";
677 print CUEOUT " <thumb>$thumb</thumb>\n";
678 print CUEOUT " </parameters>\n";
679 print CUEOUT " <type>navigation</type>\n";
680 print CUEOUT " </metatag>\n";
681
682# my $testtime = $timestamp+1000;
683# print CUEOUT " <metatag event=\"onCuePoint\" overwrite=\"true\">\n";
684# print CUEOUT " <name>Test $name</name>\n";
685# print CUEOUT " <timestamp>$testtime</timestamp>\n";
686# print CUEOUT " <parameters>\n";
687# print CUEOUT " <thumb>$thumb</thumb>\n";
688# print CUEOUT " <secnum>1</secnum>\n";
689# print CUEOUT " <subsecnum>0</subsecnum>\n";
690# print CUEOUT " </parameters>\n";
691# print CUEOUT " <type>event</type>\n";
692# print CUEOUT " </metatag>\n";
693
694 $closest_to += $frame_delta;
695 }
696 $prev_t = $curr_t;
697 }
698}
699
700
701sub StartDocument {$_[0]->{'PluginObj'}->xml_start_document(@_);}
702sub XMLDecl {$_[0]->{'PluginObj'}->xml_xmldecl(@_);}
703sub Entity {$_[0]->{'PluginObj'}->xml_entity(@_);}
704sub Doctype {$_[0]->{'PluginObj'}->xml_doctype(@_);}
705sub StartTag {$_[0]->{'PluginObj'}->xml_start_tag(@_);}
706sub EndTag {$_[0]->{'PluginObj'}->xml_end_tag(@_);}
707sub Text {$_[0]->{'PluginObj'}->xml_text(@_);}
708sub PI {$_[0]->{'PluginObj'}->xml_pi(@_);}
709sub EndDocument {$_[0]->{'PluginObj'}->xml_end_document(@_);}
710sub Default {$_[0]->{'PluginObj'}->xml_default(@_);}
711
712
713# This Char function overrides the one in XML::Parser::Stream to overcome a
714# problem where $expat->{Text} is treated as the return value, slowing
715# things down significantly in some cases.
716sub Char {
717 use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+
718 $_[0]->{'Text'} .= $_[1];
719 return undef;
720}
721
722sub xml_start_document {
723 my $self = shift(@_);
724 my ($expat, $name, $sysid, $pubid, $internal) = @_;
725
726}
727
728# Called for XML declarations
729sub xml_xmldecl {
730 my $self = shift(@_);
731 my ($expat, $version, $encoding, $standalone) = @_;
732}
733
734# Called for XML entities
735sub xml_entity {
736 my $self = shift(@_);
737 my ($expat, $name, $val, $sysid, $pubid, $ndata) = @_;
738}
739
740
741# Called for DOCTYPE declarations - use die to bail out if this doctype
742# is not meant for this plugin
743sub xml_doctype {
744 my $self = shift(@_);
745 my ($expat, $name, $sysid, $pubid, $internal) = @_;
746
747 # This test used to be done in xml_start_document
748 # Moved to here as seems more logical
749
750 if ($name !~ "seg") {
751 die "VideoPlugin: Root tag $name does not match expected <seg>";
752 }
753}
754
755
756sub xml_start_tag {
757 my $self = shift(@_);
758 my ($expat, $element) = @_;
759
760 my %attr = %_;
761
762 if ($element eq "seg") {
763 $self->{'keyframe_index'} = 0;
764 $self->{'keyframe_fnames'} = [];
765 $self->{'keyframe_timeline'} = {};
766
767 #$self->{'flowplayer_thumblist'} = "thumbs: \\[";
768
769 my $output_dir = $self->{'cached_dir'};
770 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
771
772 open(CUEOUT,">$cue_filename")
773 || die "Unable to open $cue_filename: $!\n";
774 print CUEOUT "<tags>\n";
775 }
776 elsif ($element eq "trans") {
777 my $trans_type = $attr{'type'};
778 my $pre_frame_num = $attr{'preFNum'};
779 my $post_frame_num = $attr{'postFNum'};
780
781 my $avg_frame_num = int(($pre_frame_num+$post_frame_num)/2.0)+1;
782
783 my $output_dir = $self->{'cached_dir'};
784 my $ivideo_root = $self->{'cached_file_root'};
785
786 my $keyframe_index = $self->{'keyframe_index'};
787
788 my $fps = $self->{'video-fps'};
789
790 if ($keyframe_index==0)
791 {
792 # hive actually generates one extra keyframe at the start,
793 # which is half way between frame 0 and the frist pre_frame
794
795 my $thumb_file = sprintf("%s_%04d.jpg",$ivideo_root,$keyframe_index);
796 my $thumb_filename = &util::filename_cat($output_dir,$thumb_file);
797 push(@{$self->{'keyframe_fnames'}},$thumb_file);
798
799 my $half_frame_num = $pre_frame_num/2.0;
800 my $time_msec = ($half_frame_num / $fps) * 1000;
801
802# print CUEOUT " <metatag event=\"onCuePoint\" overwrite=\"true\">\n";
803# print CUEOUT " <name>Keyframe $keyframe_index</name>\n";
804# print CUEOUT " <timestamp>$time_msec</timestamp>\n";
805# print CUEOUT " <parameters>\n";
806# print CUEOUT " <thumb>$thumb_file</thumb>\n";
807# print CUEOUT " </parameters>\n";
808# print CUEOUT " <type>navigation</type>\n";
809# print CUEOUT " </metatag>\n";
810
811
812 my $timeline_rec = { 'name'=> "Keyframe $keyframe_index",
813 'keyframeindex' => $keyframe_index,
814 'timestamp' => $time_msec,
815 'thumb' => $thumb_file };
816
817 $self->{'keyframe_timeline'}->{$half_frame_num}=$timeline_rec;
818
819 }
820
821 $keyframe_index++;
822
823 my $thumb_file = sprintf("%s_%04d.jpg",$ivideo_root,$keyframe_index);
824 my $thumb_filename = &util::filename_cat($output_dir,$thumb_file);
825 push(@{$self->{'keyframe_fnames'}},$thumb_file);
826
827 my $time_msec = (($avg_frame_num) / $fps) * 1000;
828 my $time_sec = (($avg_frame_num)/ $fps);
829
830# print CUEOUT " <metatag event=\"onCuePoint\" overwrite=\"true\">\n";
831# print CUEOUT " <name>Keyframe $keyframe_index</name>\n";
832# print CUEOUT " <timestamp>$time_msec</timestamp>\n";
833# print CUEOUT " <parameters>\n";
834# print CUEOUT " <thumb>$thumb_file</thumb>\n";
835# print CUEOUT " </parameters>\n";
836# print CUEOUT " <type>navigation</type>\n";
837# print CUEOUT " </metatag>\n";
838
839
840 my $timeline_rec = { 'name'=> "Keyframe $keyframe_index",
841 'keyframeindex' => $keyframe_index,
842 'timestamp' => $time_msec,
843 'thumb' => $thumb_file };
844
845 $self->{'keyframe_timeline'}->{$avg_frame_num}=$timeline_rec;
846
847
848 # $self->{'flowplayer_thumblist'} .= "\\{ thumbNail: '$thumb_file', time: $time_sec \\},";
849
850 $self->{'keyframe_index'} = $keyframe_index;
851 }
852}
853
854sub xml_end_tag {
855 my $self = shift(@_);
856 my ($expat, $element) = @_;
857
858 if ($element eq "seg") {
859
860 $self->output_distributed_keyframes($self->{'keyframe_timeline'},6);
861
862
863 print CUEOUT "</tags>\n";
864 close(CUEOUT);
865
866 #$self->{'flowplayer_thumblist'} .= "\\]";
867 }
868}
869
870
871
872
873
874# Called just before start or end tags with accumulated non-markup text in
875# the $_ variable.
876sub xml_text {
877 my $self = shift(@_);
878 my ($expat) = @_;
879}
880
881# Called for processing instructions. The $_ variable will contain a copy
882# of the pi.
883sub xml_pi {
884 my $self = shift(@_);
885 my ($expat, $target, $data) = @_;
886}
887
888# Called at the end of the XML document.
889sub xml_end_document {
890 my $self = shift(@_);
891 my ($expat) = @_;
892
893 # ****
894 # $self->close_document();
895}
896
897
898# Called for any characters not handled by the above functions.
899sub xml_default {
900 my $self = shift(@_);
901 my ($expat, $text) = @_;
902}
903
904
9051;
906
907
908
909
910
911
912
913
914
915
916
Note: See TracBrowser for help on using the repository browser.