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

Last change on this file since 21335 was 21335, checked in by davidb, 13 years ago

Support added for -keep_keyframes option

File size: 30.4 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|ts)$^;
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
144 my $keep_keyframe_timeline = $self->{'keep_keyframe_timeline'};
145 $self->associate_keyframes($doc_obj,$section,$keep_keyframe_timeline);
146
147}
148
149
150
151sub extract_thumbnail
152{
153 my $self = shift (@_);
154 my ($doc_obj,$filename,$convertto_regenerated,$thumbnailtype,
155 $thumbnail_width, $thumbnail_height) = @_;
156
157 my $section = $doc_obj->get_top_section();
158
159 my $output_dir = $self->{'cached_dir'};
160 my $ivideo_root = $self->{'cached_file_root'};
161
162 my $verbosity = $self->{'verbosity'};
163 my $outhandle = $self->{'outhandle'};
164
165
166 # Generate the thumbnail with convert, a la ImagePlug
167
168 my $thumbnailfile = &util::filename_cat($output_dir,"$ivideo_root-thumbnail.$thumbnailtype");
169
170
171 my $optionally_run_general_cmd = "run_uncached_general_cmd";
172 if ($self->{'enable_cache'}) {
173 $optionally_run_general_cmd
174 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
175 }
176
177### print STDERR "**** creating thumbnail: $thumbnail_width x $thumbnail_height\n";
178
179 my ($thumb_cmd ,$othumb_filename)
180 = $self->keyframe_thumbnail_cmd($filename,$thumbnailfile,$thumbnail_width,$thumbnail_height);
181
182 my $thumb_options = { 'verbosity' => $verbosity,
183 'outhandle' => $outhandle,
184 'message_prefix' => "Thumbnail",
185 'message' => "Generating thumbnail" };
186
187 my ($thumb_regenerated,$thumb_result,$thumb_had_error)
188 = $self->$optionally_run_general_cmd($thumb_cmd,$thumbnailfile,$thumb_options);
189
190 # Add the thumbnail as an associated file ...
191 if (-e "$thumbnailfile") {
192 $doc_obj->associate_file("$thumbnailfile", "thumbnail.$thumbnailtype",
193 "image/$thumbnailtype",$section);
194 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
195 $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
196
197 $doc_obj->add_utf8_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
198### $doc_obj->add_utf8_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Thumb]\">");
199 }
200
201 # Extract Thumnail metadata from convert output
202 if ($thumb_result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
203 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
204 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
205 }
206 else {
207 # Two reasons for getting to here:
208 # 1.thumbnail was generated by ffmpeg, not imagemagick convert
209 # 2.thumbnail was cached, so imagemagick convert was not run
210 # Either way, the solution is the same:
211 # => run "identify $thumbnailfile" and parse result
212
213 $thumb_result = `identify \"$thumbnailfile\"`;
214
215 if ($thumb_result =~ m/([0-9]+)x([0-9]+)/) {
216 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
217 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
218 }
219 }
220}
221
222
223sub extract_keyframes_montage
224{
225 my $self = shift (@_);
226 my ($doc_obj,$filename,$thumbnailtype) = @_;
227
228 my $section = $doc_obj->get_top_section();
229
230 my $output_dir = $self->{'cached_dir'};
231 my $ivideo_root = $self->{'cached_file_root'};
232
233
234 # Generate the mosaic with 'montage'
235 my $montagefile = &util::filename_cat($output_dir,"$ivideo_root\_montage.$thumbnailtype");
236
237 my ($montage_cmd,$omontage_filename)
238 = $self->keyframe_montage_cmd($filename,$montagefile);
239
240 my $montage_options = { 'message_prefix' => "Montage",
241 'message' => "Generating montage" };
242
243 my ($montage_result,$montage_had_error)
244 = $self->run_general_cmd($montage_cmd,$montage_options);
245
246 # Add the montage as an associated file ...
247 if (-e "$montagefile") {
248 $doc_obj->associate_file("$montagefile", "montage.$thumbnailtype",
249 "image/$thumbnailtype",$section);
250 $doc_obj->add_metadata ($section, "MontageType", $thumbnailtype);
251 $doc_obj->add_metadata ($section, "Montage", "montage.$thumbnailtype");
252
253 $doc_obj->add_utf8_metadata ($section, "montageicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Montage]\" >");
254 }
255}
256
257
258
259sub extract_screenview
260{
261 my $self = shift (@_);
262 my ($doc_obj,$filename,$convertto_regenerated, $screenviewtype, $screenview_width,$screenview_height) = @_;
263
264 my $section = $doc_obj->get_top_section();
265
266 my $output_dir = $self->{'cached_dir'};
267 my $ivideo_root = $self->{'cached_file_root'};
268
269 my $optionally_run_general_cmd = "run_uncached_general_cmd";
270 if ($self->{'enable_cache'}) {
271 $optionally_run_general_cmd
272 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
273 }
274 # make the screenview image
275
276 my $screenviewfilename = &util::filename_cat($output_dir,"$ivideo_root-screenview.$screenviewtype");
277
278
279 my ($screenview_cmd,$oscreenview_filename)
280 = $self->keyframe_thumbnail_cmd($filename,$screenviewfilename,$screenview_width,$screenview_height);
281
282 my $screenview_options = { 'message_prefix' => "Screenview",
283 'message' => "Generating screenview image" };
284
285
286 my ($screenview_regenerated,$screenview_result,$screenview_had_error)
287 = $self->$optionally_run_general_cmd($screenview_cmd,$screenviewfilename,$screenview_options);
288
289
290 # get screenview dimensions, size and type
291 if ($screenview_result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
292 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
293 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
294 }
295 else {
296 $doc_obj->add_metadata ($section, "ScreenWidth", $screenview_width);
297 $doc_obj->add_metadata ($section, "ScreenHeight", $screenview_height);
298 }
299
300 #add the screenview as an associated file ...
301 if (-e "$screenviewfilename") {
302 $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
303 "image/$screenviewtype",$section);
304 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
305 $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
306
307 $doc_obj->add_utf8_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
308 } else {
309 my $outhandle = $self->{'outhandle'};
310 print $outhandle "VideoPlugin: couldn't find \"$screenviewfilename\"\n";
311 }
312}
313
314
315# Create the keyframes, thumbnail and screenview images, and discover
316# the Video's size, width, and height using the ffmpeg utility.
317
318sub run_convert {
319 my $self = shift (@_);
320 my $base_dir = shift (@_);
321 my $filename = shift (@_); # filename with full path
322 my $file = shift (@_); # filename without path
323 my $doc_obj = shift (@_);
324
325 my $section = $doc_obj->get_top_section();
326
327 my $verbosity = $self->{'verbosity'};
328 my $outhandle = $self->{'outhandle'};
329
330 # check the filename is okay
331 return 0 if ($file eq "" || $filename eq "");
332
333 my $minimumsize = $self->{'minimumsize'};
334 if (defined $minimumsize && (-s $filename < $minimumsize)) {
335 print $outhandle "VideoPlugin: \"$filename\" too small, skipping\n"
336 if ($verbosity > 1);
337 }
338
339 my ($video_type, $video_width, $video_height, $video_duration, $video_size,
340 $vcodec,$vfps,$atype,$afreq,$achan,$arate)
341 = &VideoConverter::identify($filename, $outhandle, $verbosity);
342
343 if ($vfps eq "unknown") {
344 print $outhandle "Unknown framerate, defaulting to 25 frames per second.\n";
345 $vfps = 25;
346 }
347
348 my ($dur_hour,$dur_min,$dur_sec)
349 = ($video_duration =~ m/(\d+):(\d+):(\d+\.\d+)/);
350 my $total_dur_secs = $dur_hour*3600 + $dur_min*60 + $dur_sec;
351
352 $self->{'video-fps'} = $vfps;
353 $self->{'num-total-frames'} = $total_dur_secs * $vfps;
354
355 # Convert the video to a new type (if required).
356 my $converttotype = $self->{'converttotype'};
357 my $converttosize = $self->{'converttosize'};
358
359 # shorten duration prcessed for experimentation purposes
360 my $exp_duration = undef;
361
362 my $excerpt_duration = $self->{'excerpt_duration'};
363
364 if ((defined $excerpt_duration) && ($excerpt_duration ne "")) {
365 $exp_duration = $excerpt_duration;
366 my ($hh,$mm,$ss,$ms);
367
368 if ($exp_duration =~ m/^(\d\d):(\d\d):(\d\d)\.?(\d\d)?/) {
369 $hh = $1;
370 $mm = $2;
371 $ss = $3;
372 $ms = $4;
373 }
374 else {
375 # assume value is in seconds
376 $hh = 0;
377 $mm = 0;
378 $ss = $exp_duration;
379 $ms = 0;
380 }
381
382 my $excerpt_dur_in_secs = $hh * 3600 + $mm * 60 + $ss;
383
384 if ($excerpt_dur_in_secs < $total_dur_secs) {
385 $self->{'num-total-frames'} = $excerpt_dur_in_secs * $vfps; # override calculation for full length duration
386 }
387 else {
388 # clip is already shorter than requested video excerpt duration
389 # set exp_duration back to undefined
390 $exp_duration = undef;
391 }
392 }
393
394
395 if (defined $exp_duration)
396 {
397 print $outhandle "Only encoding first $exp_duration of video.\n";
398 $self->{'exp_duration'} = $exp_duration;
399 }
400
401 my $ascii_only_filenames = $self->{'use_ascii_only_filenames'};
402 $self->init_cache_for_file($filename);
403
404 my $originalfilename = undef;
405 my $type = "unknown";
406
407 my $output_dir = $self->{'cached_dir'};
408 my $ivideo_root = $self->{'cached_file_root'};
409
410 my $convertto_regenerated = 0;
411 if (($converttotype ne "" && $filename =~ m/$converttotype$/i) ||
412 (($converttosize ne "" && $converttosize ne $video_width) || ($converttosize ne "" && $converttosize ne $video_height))) {
413 if ($converttotype eq "") {
414 # in this block because the video width x height different to original
415 # => set (for this call to run_convert only) converttotype
416 # to input file extension
417 ($converttotype) = ($filename =~ m/\.(.*?)$/);
418 }
419
420 $originalfilename = $filename;
421
422 $file = "$ivideo_root.$converttotype";
423 $filename = &util::filename_cat($output_dir,$file);
424
425 my $s_opt = $self->optional_frame_scale($converttosize,$video_width,$video_height);
426 my $exp_duration = $self->{'exp_duration'};
427 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
428
429 my $main_opts = "-y $t_opt $s_opt";
430
431
432 my $originalfilename_gsdlenv = $self->gsdlhome_independent($originalfilename);
433 my $filename_gsdlenv = $self->gsdlhome_independent($filename);
434
435
436 my $convertto_command = "ffmpeg $main_opts -i \"$originalfilename_gsdlenv\"";
437 $convertto_command .= " -ar 22050" if ($converttotype eq "flv");
438 $convertto_command .= " -y \"$filename_gsdlenv\"";
439
440 my $convertto_result;
441 my $convertto_error;
442
443 my $convertto_options = { @{$self->{'ffmpeg_monitor'}},
444 'message_prefix' => "Convert to",
445 'message' => "Converting video to $converttotype" };
446
447 ($convertto_regenerated,$convertto_result,$convertto_error)
448 = $self->run_cached_general_cmd($convertto_command,$filename,$convertto_options);
449
450 $type = $converttotype;
451 }
452
453
454 # Add the video metadata
455
456 my $file_unicode = pack("U0C*", map { ord($_) } split(//,$file)); # force explicitly to unicode
457
458 $file_unicode =~ s/\x{2018}|\x{2019}|\x{201C}|\x{201D}//g; # remove smart quotes as cause problem in URL for video server
459 $file_unicode =~ s/\x{2013}/\-/g; # change en-dash to '-' as again causes problems for video server
460
461## print STDERR "**** file metadata = ", &gsprintf::debug_unicode_string($file_unicode), "\n";
462
463
464 # split filename on char if specified
465 if (0) {
466# don't do any more
467# if ($file_unicode =~ m/\-/) {
468
469 my @file_split = split(/\s*\-\s*/,$file_unicode);
470 my $creator = shift @file_split;
471 my $title = shift @file_split;
472
473 $title =~ s/\..*?$//;
474 $title =~ s/^(.)/\u$1/;
475
476 $doc_obj->add_utf8_metadata($section,"Title",$title);
477
478 my @creator_split = split(/\s+and\s+/,$creator);
479 foreach my $c (@creator_split) {
480 $doc_obj->add_utf8_metadata($section,"Creator",$c);
481 }
482 }
483
484 $file = $file_unicode;
485 my $filemeta = $self->filename_to_utf8_metadata($file);
486 my $filemeta_url_safe = $self->url_safe($filemeta);
487
488 $doc_obj->add_utf8_metadata ($section, "Video", $filemeta_url_safe);
489
490 # Also want to set filename as 'Source' metadata to be
491 # consistent with other plugins
492 # $doc_obj->add_utf8_metadata ($section, "Source", $filemeta_url_safe);
493
494
495 if ($video_type ne " ") {
496 $type = $video_type;
497 }
498
499 $doc_obj->add_metadata ($section, "FileFormat", $type);
500 $doc_obj->add_metadata ($section, "FileSize", $video_size);
501
502 $doc_obj->add_metadata ($section, "VideoType", $video_type);
503 $doc_obj->add_metadata ($section, "VideoWidth", $video_width);
504 $doc_obj->add_metadata ($section, "VideoHeight", $video_height);
505 $doc_obj->add_metadata ($section, "VideoDuration", $video_duration);
506 $doc_obj->add_metadata ($section, "VideoSize", $video_size);
507
508 $doc_obj->add_metadata ($section, "VideoCodec", $vcodec);
509 $doc_obj->add_metadata ($section, "VideoFPS", $vfps);
510
511 $doc_obj->add_metadata ($section, "AudioType", $atype);
512 $doc_obj->add_metadata ($section, "AudioFreq", $afreq);
513 $doc_obj->add_metadata ($section, "AudioChannels", $achan);
514 $doc_obj->add_metadata ($section, "AudioRate", $arate);
515
516 $doc_obj->add_utf8_metadata ($section, "srclink",
517 "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/[Video]\">");
518 $doc_obj->add_utf8_metadata ($section, "/srclink", "</a>");
519 $doc_obj->add_metadata ($section, "srcicon", "[VideoType]");
520
521 # Add the original file as an associated file
522 # $doc_obj->associate_file($filename,$file,"video/$type",$section);
523
524
525 if ($self->{'extract_keyframes'}) {
526 $self->extract_keyframes($doc_obj,$originalfilename,$filename);
527 }
528
529 my $streamable_regenerated = 0;
530
531 if ($self->{'enable_flv_streaming'} && !$self->{'enable_mp4_streaming'}) {
532 $streamable_regenerated
533 = $self->enable_full_streaming($doc_obj,
534 $originalfilename,$filename,
535 $convertto_regenerated,
536 $video_width,$video_height);
537 }
538
539 # Create an H264+ACC video file with Handbrake
540 if ($self->{'enable_mp4_streaming'}) {
541 $streamable_regenerated
542 = $self->enable_h264_streaming($doc_obj,
543 $originalfilename,$filename,
544 $convertto_regenerated,
545 $video_width,$video_height);
546 }
547
548 my $thumbnailsize = $self->{'thumbnailsize'} || 100;
549 my $thumbnailtype = $self->{'thumbnailtype'} || 'jpg';
550
551
552 if ($self->{'create_thumbnail'} eq "true") {
553
554 my $thumbnail_width;
555 my $thumbnail_height;
556
557 if ($video_width>$video_height) {
558 my $scale_ratio = $video_height / $video_width;
559 $thumbnail_width = $thumbnailsize;
560 $thumbnail_height = int($thumbnailsize * $scale_ratio);
561 }
562 else {
563 my $scale_ratio = $video_width / $video_height;
564 $thumbnail_width = int($thumbnailsize * $scale_ratio);
565 $thumbnail_height = $thumbnailsize;
566 }
567
568 # for some video formats, extracted size needs to be multiple of 2
569 $thumbnail_width = int($thumbnail_width/2) * 2;
570 $thumbnail_height = int($thumbnail_height/2) * 2;
571
572 $self->extract_thumbnail($doc_obj,$filename,$convertto_regenerated,
573 $thumbnailtype,
574 $thumbnail_width,$thumbnail_height);
575 }
576
577
578 if ($self->{'extract_keyframes'}) {
579 $self->extract_keyframes_montage($doc_obj,$filename,$thumbnailtype);
580 }
581
582 my $screenviewsize = $self->{'screenviewsize'};
583 my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
584
585 # Make a screen-sized version of the picture if requested
586 if ($self->{'create_screenview'} eq "true") {
587
588 # To do: if the actual image smaller than the screenview size,
589 # we should use the original !
590
591 my $screenview_width;
592 my $screenview_height;
593
594 if ($video_width>$video_height) {
595 my $scale_ratio = $video_height / $video_width;
596 $screenview_width = $screenviewsize;
597 $screenview_height = int($screenviewsize * $scale_ratio);
598 }
599 else {
600 my $scale_ratio = $video_width / $video_height;
601 $screenview_width = int($screenviewsize * $scale_ratio);
602 $screenview_height = $screenviewsize;
603 }
604
605
606 # for some video formats, extracted size needs to be multiple of 2
607 $screenview_width = int($screenview_width/2) * 2;
608 $screenview_height = int($screenview_height/2) * 2;
609
610 $self->extract_screenview($doc_obj,$filename, $convertto_regenerated,
611 $screenviewtype,
612 $screenview_width,$screenview_height);
613 }
614
615 return $type;
616}
617
618
619
620
621sub read_into_doc_obj {
622 my $self = shift (@_);
623 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
624
625 $self->{'media_type'} = "video";
626
627 my ($rv,$doc_obj) = $self->SUPER::read_into_doc_obj(@_);
628
629 if ($rv != 1) {
630 return ($rv,$doc_obj);
631 }
632
633 my $enable_streaming
634 = ($self->{'enable_flv_streaming'} || $self->{'enable_mp4_streaming'})
635 ? 1 : 0;
636
637 if (($enable_streaming) && ($self->{'extract_keyframes'})) {
638 my $section = $doc_obj->get_top_section();
639 my $oflash_filename = $self->{'oflash_filename'};
640 my ($streamkeyframes_cmd,$ostreamkeyframes_filename)
641 = $self->streamkeyframes_cmd($oflash_filename,$doc_obj,$section);
642
643 my $verbosity = $self->{'verbosity'};
644
645 my $streamkeyframes_options = { @{$self->{'flvtool2_monitor'}},
646 'message_prefix' => "Stream Keyframes",
647 'message' => "Reprocessing video stream to add keyframes on timeline" };
648
649 $self->run_general_cmd($streamkeyframes_cmd,$streamkeyframes_options);
650 }
651
652 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
653 my $section = $doc_obj->get_top_section();
654
655 $self->title_fallback($doc_obj,$section,$filename_no_path);
656
657 $self->{'media_type'} = undef;
658
659 return ($rv,$doc_obj);
660}
661
662
663
664
665sub output_distributed_keyframes
666{
667 my ($self) = shift @_;
668 my ($timeline,$num_dist_frames) = @_;
669
670 my $num_total_frames = $self->{'num-total-frames'};
671
672 my $keep_timeline = {};
673
674 my $frame_delta;
675 if ($num_dist_frames eq "all") {
676 $frame_delta = undef;
677 }
678 else {
679 $frame_delta = $num_total_frames/$num_dist_frames;
680 }
681
682 my $closest_to = $frame_delta;
683 my $prev_t = 0;
684
685# print STDERR "*** num total frames = $num_total_frames\n";
686# print STDERR "*** frame delta = $frame_delta\n";
687
688 my $keep_keyframe_num = 1;
689
690 foreach my $curr_t (sort { $a <=> $b } keys %$timeline)
691 {
692# print STDERR "*** curr_t = $curr_t\n";
693
694 my $timeline_rec = undef;
695
696 if (!defined $closest_to) {
697 $timeline_rec = $timeline->{$curr_t};
698 $keep_timeline->{$curr_t} = $timeline_rec;
699 }
700 elsif ($curr_t>$closest_to) {
701 # decide if previous t (prev_t_ or current t (curr_t) is closest
702
703 my $prev_t_dist = $closest_to - $prev_t;
704 my $curr_t_dist = $curr_t - $closest_to;
705
706 if ($curr_t_dist <= $prev_t_dist)
707 {
708 $timeline_rec = $timeline->{$curr_t};
709 $keep_timeline->{$curr_t} = $timeline_rec;
710
711 }
712 else
713 {
714 $timeline_rec = $timeline->{$prev_t};
715 $keep_timeline->{$prev_t} = $timeline_rec;
716 }
717
718 $closest_to += $frame_delta;
719 }
720
721 if (defined $timeline_rec) {
722
723 my $name = $timeline_rec->{'name'};
724 my $timestamp = $timeline_rec->{'timestamp'};
725 my $thumb = $timeline_rec->{'thumb'};
726 my $keyframe_num = $timeline_rec->{'keyframenum'};
727
728
729 print CUEOUT " <metatag event=\"onCuePoint\" overwrite=\"true\">\n";
730 print CUEOUT " <name>$name</name>\n";
731 print CUEOUT " <timestamp>$timestamp</timestamp>\n";
732 print CUEOUT " <parameters>\n";
733 print CUEOUT " <thumb>$thumb</thumb>\n";
734 print CUEOUT " <keyframeNum>$keyframe_num</keyframeNum>\n";
735 print CUEOUT " <keepKeyframeNum>$keep_keyframe_num</keepKeyframeNum>\n";
736 print CUEOUT " </parameters>\n";
737 print CUEOUT " <type>navigation</type>\n";
738 print CUEOUT " </metatag>\n";
739
740# my $testtime = $timestamp+1000;
741# print CUEOUT " <metatag event=\"onCuePoint\" overwrite=\"true\">\n";
742# print CUEOUT " <name>Test $name</name>\n";
743# print CUEOUT " <timestamp>$testtime</timestamp>\n";
744# print CUEOUT " <parameters>\n";
745# print CUEOUT " <thumb>$thumb</thumb>\n";
746# print CUEOUT " <secnum>1</secnum>\n";
747# print CUEOUT " <subsecnum>0</subsecnum>\n";
748# print CUEOUT " </parameters>\n";
749# print CUEOUT " <type>event</type>\n";
750# print CUEOUT " </metatag>\n";
751
752 $keep_keyframe_num++;
753
754 }
755 $prev_t = $curr_t;
756 }
757
758 $self->{'keep_keyframe_timeline'} = $keep_timeline;
759}
760
761
762sub StartDocument {$_[0]->{'PluginObj'}->xml_start_document(@_);}
763sub XMLDecl {$_[0]->{'PluginObj'}->xml_xmldecl(@_);}
764sub Entity {$_[0]->{'PluginObj'}->xml_entity(@_);}
765sub Doctype {$_[0]->{'PluginObj'}->xml_doctype(@_);}
766sub StartTag {$_[0]->{'PluginObj'}->xml_start_tag(@_);}
767sub EndTag {$_[0]->{'PluginObj'}->xml_end_tag(@_);}
768sub Text {$_[0]->{'PluginObj'}->xml_text(@_);}
769sub PI {$_[0]->{'PluginObj'}->xml_pi(@_);}
770sub EndDocument {$_[0]->{'PluginObj'}->xml_end_document(@_);}
771sub Default {$_[0]->{'PluginObj'}->xml_default(@_);}
772
773
774# This Char function overrides the one in XML::Parser::Stream to overcome a
775# problem where $expat->{Text} is treated as the return value, slowing
776# things down significantly in some cases.
777sub Char {
778 use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+
779 $_[0]->{'Text'} .= $_[1];
780 return undef;
781}
782
783sub xml_start_document {
784 my $self = shift(@_);
785 my ($expat, $name, $sysid, $pubid, $internal) = @_;
786
787}
788
789# Called for XML declarations
790sub xml_xmldecl {
791 my $self = shift(@_);
792 my ($expat, $version, $encoding, $standalone) = @_;
793}
794
795# Called for XML entities
796sub xml_entity {
797 my $self = shift(@_);
798 my ($expat, $name, $val, $sysid, $pubid, $ndata) = @_;
799}
800
801
802# Called for DOCTYPE declarations - use die to bail out if this doctype
803# is not meant for this plugin
804sub xml_doctype {
805 my $self = shift(@_);
806 my ($expat, $name, $sysid, $pubid, $internal) = @_;
807
808 # This test used to be done in xml_start_document
809 # Moved to here as seems more logical
810
811 if ($name !~ "seg") {
812 die "VideoPlugin: Root tag $name does not match expected <seg>";
813 }
814}
815
816
817sub xml_start_tag {
818 my $self = shift(@_);
819 my ($expat, $element) = @_;
820
821 my %attr = %_;
822
823 if ($element eq "seg") {
824 $self->{'keyframe_index'} = 0;
825 $self->{'keyframe_fnames'} = [];
826 $self->{'keyframe_timeline'} = {};
827
828 #$self->{'flowplayer_thumblist'} = "thumbs: \\[";
829
830 my $output_dir = $self->{'cached_dir'};
831 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
832
833 open(CUEOUT,">$cue_filename")
834 || die "Unable to open $cue_filename: $!\n";
835 print CUEOUT "<tags>\n";
836 }
837 elsif ($element eq "trans") {
838 my $trans_type = $attr{'type'};
839 my $pre_frame_num = $attr{'preFNum'};
840 my $post_frame_num = $attr{'postFNum'};
841
842 my $avg_frame_num = int(($pre_frame_num+$post_frame_num)/2.0)+1;
843
844 my $output_dir = $self->{'cached_dir'};
845 my $ivideo_root = $self->{'cached_file_root'};
846
847 my $keyframe_index = $self->{'keyframe_index'};
848
849 my $fps = $self->{'video-fps'};
850
851 if ($keyframe_index==0)
852 {
853 # hive actually generates one extra keyframe at the start,
854 # which is half way between frame 0 and the frist pre_frame
855
856 my $thumb_file = sprintf("%s_%04d.jpg",$ivideo_root,$keyframe_index);
857 my $thumb_filename = &util::filename_cat($output_dir,$thumb_file);
858 push(@{$self->{'keyframe_fnames'}},$thumb_file);
859
860 my $half_frame_num = $pre_frame_num/2.0;
861 my $time_msec = ($half_frame_num / $fps) * 1000;
862
863# print CUEOUT " <metatag event=\"onCuePoint\" overwrite=\"true\">\n";
864# print CUEOUT " <name>Keyframe $keyframe_index</name>\n";
865# print CUEOUT " <timestamp>$time_msec</timestamp>\n";
866# print CUEOUT " <parameters>\n";
867# print CUEOUT " <thumb>$thumb_file</thumb>\n";
868# print CUEOUT " </parameters>\n";
869# print CUEOUT " <type>navigation</type>\n";
870# print CUEOUT " </metatag>\n";
871
872
873 my $timeline_rec = { 'name'=> "Keyframe $keyframe_index",
874 'keyframeindex' => $keyframe_index,
875 'timestamp' => $time_msec,
876 'thumb' => $thumb_file,
877 'keyframenum' => $keyframe_index};
878
879 $self->{'keyframe_timeline'}->{$half_frame_num}=$timeline_rec;
880 }
881
882 $keyframe_index++;
883
884 my $thumb_file = sprintf("%s_%04d.jpg",$ivideo_root,$keyframe_index);
885 my $thumb_filename = &util::filename_cat($output_dir,$thumb_file);
886 push(@{$self->{'keyframe_fnames'}},$thumb_file);
887
888 my $time_msec = (($avg_frame_num) / $fps) * 1000;
889 my $time_sec = (($avg_frame_num)/ $fps);
890
891# print CUEOUT " <metatag event=\"onCuePoint\" overwrite=\"true\">\n";
892# print CUEOUT " <name>Keyframe $keyframe_index</name>\n";
893# print CUEOUT " <timestamp>$time_msec</timestamp>\n";
894# print CUEOUT " <parameters>\n";
895# print CUEOUT " <thumb>$thumb_file</thumb>\n";
896# print CUEOUT " </parameters>\n";
897# print CUEOUT " <type>navigation</type>\n";
898# print CUEOUT " </metatag>\n";
899
900
901 my $timeline_rec = { 'name'=> "Keyframe $keyframe_index",
902 'keyframeindex' => $keyframe_index,
903 'timestamp' => $time_msec,
904 'thumb' => $thumb_file,
905 'keyframenum' => $keyframe_index};
906
907 $self->{'keyframe_timeline'}->{$avg_frame_num}=$timeline_rec;
908
909 # $self->{'flowplayer_thumblist'} .= "\\{ thumbNail: '$thumb_file', time: $time_sec \\},";
910
911 $self->{'keyframe_index'} = $keyframe_index;
912 }
913}
914
915sub xml_end_tag {
916 my $self = shift(@_);
917 my ($expat, $element) = @_;
918
919 if ($element eq "seg") {
920
921 $self->output_distributed_keyframes($self->{'keyframe_timeline'},$self->{'keep_keyframes'});
922
923
924 print CUEOUT "</tags>\n";
925 close(CUEOUT);
926
927 #$self->{'flowplayer_thumblist'} .= "\\]";
928 }
929}
930
931
932
933
934
935# Called just before start or end tags with accumulated non-markup text in
936# the $_ variable.
937sub xml_text {
938 my $self = shift(@_);
939 my ($expat) = @_;
940}
941
942# Called for processing instructions. The $_ variable will contain a copy
943# of the pi.
944sub xml_pi {
945 my $self = shift(@_);
946 my ($expat, $target, $data) = @_;
947}
948
949# Called at the end of the XML document.
950sub xml_end_document {
951 my $self = shift(@_);
952 my ($expat) = @_;
953
954 # ****
955 # $self->close_document();
956}
957
958
959# Called for any characters not handled by the above functions.
960sub xml_default {
961 my $self = shift(@_);
962 my ($expat, $text) = @_;
963}
964
965
9661;
967
968
969
970
971
972
973
974
975
976
977
Note: See TracBrowser for help on using the repository browser.