source: extensions/gsdl-video/trunk/perllib/plugins/VideoConverter.pm@ 20003

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

Some minor tweaks to better align the audio and video plugins

File size: 25.5 KB
Line 
1###########################################################################
2#
3# VideoConverter - helper plugin that does video (and audio) conversion using ffmpeg
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2008 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,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU 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###########################################################################
26package VideoConverter;
27
28use MultimediaConverter;
29
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
33
34use gsprintf 'gsprintf';
35
36BEGIN {
37 @VideoConverter::ISA = ('MultimediaConverter');
38}
39
40
41my @thumb_arguments = (
42 { 'name' => "create_thumbnail",
43 'desc' => "{ImageConverter.create_thumbnail}",
44 'type' => "enum",
45 'list' => [{'name' => "true", 'desc' => "{common.true}"},
46 {'name' => "false", 'desc' => "{common.false}"}],
47 'deft' => "true",
48 'reqd' => "no" },
49 { 'name' => "thumbnailsize",
50 'desc' => "{ImageConverter.thumbnailsize}",
51 'type' => "int",
52 'deft' => "100",
53 'range' => "1,",
54 'reqd' => "no" },
55 { 'name' => "thumbnailtype",
56 'desc' => "{ImageConverter.thumbnailtype}",
57 'type' => "string",
58 'deft' => "jpg",
59 'reqd' => "no" },
60 { 'name' => "noscaleup",
61 'desc' => "{ImageConverter.noscaleup}",
62 'type' => "flag",
63 'reqd' => "no" } );
64
65
66my @screenview_arguments = (
67 { 'name' => "create_screenview",
68 'desc' => "{ImageConverter.create_screenview}",
69 'type' => "enum",
70 'list' => [{'name' => "true", 'desc' => "{common.true}"},
71 {'name' => "false", 'desc' => "{common.false}"}],
72 'deft' => "true",
73 'reqd' => "no" },
74 { 'name' => "screenviewsize",
75 'desc' => "{ImageConverter.screenviewsize}",
76 'type' => "int",
77 'deft' => "352",
78 'range' => "1,",
79 'reqd' => "no" },
80 { 'name' => "screenviewtype",
81 'desc' => "{ImageConverter.screenviewtype}",
82 'type' => "string",
83 'deft' => "jpg",
84 'reqd' => "no" } );
85
86my $arguments = [
87 @thumb_arguments,
88 @screenview_arguments,
89
90 { 'name' => "converttotype",
91 'desc' => "{ImageConverter.converttotype}",
92 'type' => "string",
93 'deft' => "",
94 'reqd' => "no" },
95
96
97 { 'name' => "converttosize",
98 'desc' => "{VideoPlugin.converttosize}",
99 'type' => "int",
100 'deft' => "",
101## 'deft' => "352",
102 'reqd' => "no" },
103 { 'name' => "converttobitrate",
104 'desc' => "{VideoPlugin.converttobitrate}",
105 'type' => "string",
106 'deft' => "200k",
107 'reqd' => "no" },
108 { 'name' => "extractkeyframes",
109 'desc' => "{VideoPlugin.extractkeyframes}",
110 'type' => "flag",
111 'deft' => "0",
112 'reqd' => "no" },
113 { 'name' => "streamingsize",
114 'desc' => "{VideoPlugin.streamingsize}",
115 'type' => "int",
116 'deft' => "352",
117 'reqd' => "no" },
118 { 'name' => "streamingbitrate",
119 'desc' => "{VideoPlugin.streamingbitrate}",
120 'type' => "string",
121 'deft' => "200k",
122 'reqd' => "no" },
123
124 { 'name' => "minimumsize",
125 'desc' => "{ImageConverter.minimumsize}",
126 'type' => "int",
127 'deft' => "100",
128 'range' => "1,",
129 'reqd' => "no" },
130
131 ];
132
133my $options = { 'name' => "VideoConverter",
134 'desc' => "{VideoConverter.desc}",
135 'abstract' => "yes",
136 'inherits' => "yes",
137 'args' => $arguments };
138
139sub new {
140 my ($class) = shift (@_);
141 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
142 push(@$pluginlist, $class);
143
144 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
145 push(@{$hashArgOptLists->{"OptList"}},$options);
146
147 my $self = new MultimediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
148
149
150 return bless $self, $class;
151
152}
153
154
155
156
157# Discover the characteristics of a video file.
158
159# Equivalent step to that in ImagePlugin that uses ImageMagicks's
160# 'indentify' utility
161
162# Here we use 'ffmpeg' for video but for consistency keep the Perl
163# method name the same as before
164
165
166sub identify {
167 my ($video, $outhandle, $verbosity) = @_;
168
169 # Use the ffmpeg command to get the file specs
170 my $command = "ffmpeg -i \"$video\"";
171
172 print $outhandle " $command\n" if ($verbosity > 2);
173 my $result = '';
174 $result = `$command 2>&1`;
175 print $outhandle " $result\n" if ($verbosity > 4);
176
177 # Read the type, width, and height etc.
178 my $vtype = 'unknown';
179 my $vcodec = 'unknown';
180 my $width = 'unknown';
181 my $height = 'unknown';
182 my $fps = 'unknown';
183
184 my $atype = 'unknown';
185 my $afreq = 'unknown';
186 my $achan = 'unknown';
187 my $arate = 'unknown';
188
189 my $video_safe = quotemeta $video;
190
191 # strip off everything up to filename
192 $result =~ s/^.*\'$video_safe\'://s;
193
194## if ($result =~ m/Video: (.*?) fps/m) {
195 if ($result =~ m/^\s+Stream.*?Video: (.*?)$/m) {
196 my $video_info = $1;
197 $video_info =~ s/\s*\[.*?\]//g;
198
199 my @video_fields = split(/,\s*/,$video_info);
200
201 $vtype = shift @video_fields;
202
203 if ($video_fields[0] !~ m/(\d+)x(\d+)/) {
204 $vcodec = shift @video_fields;
205 }
206 my $video_dim = shift @video_fields;
207
208 ($width,$height) = ($video_dim =~ m/(\d+)x(\d+)/);
209
210# if ($video_info =~ m/([^,]+),(?: ([^,]+),)? (\d+)x(\d+),.*?(\d+\.\d+)/)
211# {
212# $vtype = $1;
213# $vcodec = $2 if defined $2;
214# $width = $3;
215# $height = $4;
216# $fps = $5;
217# }
218 }
219
220 if ($result =~ m/Audio: (\w+), (\d+) Hz, (\w+)(?:, (\d+.*))?/m) {
221 $atype = $1;
222 $afreq = $2;
223 $achan = $3;
224 $arate = $4 if (defined $4);
225 }
226
227 # Read the duration
228 my $duration = "unknown";
229 if ($result =~ m/Duration: (\d+:\d+:\d+\.\d+)/m) {
230 $duration = $1;
231 }
232 print $outhandle " file: $video:\t $vtype, $width, $height, $duration\n"
233 if ($verbosity > 2);
234
235 if ($verbosity >3) {
236 print $outhandle "\t video codec=$vcodec, fps = $fps\n";
237 print $outhandle "\t audio codec=$atype, freq = $afreq Hz, $achan, $arate\n";
238 }
239
240 # Return the specs
241 return ($vtype, $width, $height, $duration, -s $video,
242 $vcodec,$fps,$atype,$afreq,$achan,$arate);
243}
244
245
246sub vob_durations
247{
248 my ($media_base_dir,$title_num,$outhandle) = @_;
249
250 my $filter_re = sprintf("^VTS_%02d_[1-9]\\.VOB\$",$title_num);
251
252 my $duration_info = {};
253
254 if (opendir(VTSIN,$media_base_dir)) {
255 my @vts_title_vobs = grep { $_ =~ m/$filter_re/ } readdir(VTSIN);
256 closedir(VTSIN);
257
258 foreach my $v (@vts_title_vobs) {
259 my $full_v = &util::filename_cat($media_base_dir,$v);
260
261 my ($vtype, $width, $height, $duration, $vsize,
262 $vcodec,$fps,$atype,$afreq,$achan,$arate) = identify($full_v,$outhandle,0);
263
264 my ($vob_num) = ($v =~ m/^VTS_\d\d_(\d)\.VOB$/);
265
266 $duration_info->{$vob_num} = $duration;
267 print STDERR "**** $title_num: $title_num, storing {$vob_num} => $duration\n";
268
269 }
270
271 }
272 else {
273 print $outhandle "Warning: unable to read files in directory $media_base_dir\n";
274 }
275
276 return $duration_info;
277
278}
279
280
281
282sub init_cache_for_file {
283 my $self = shift(@_);
284
285 my ($video_filename) = @_;
286
287 $self->SUPER::init_cache_for_file($video_filename);
288
289
290 my @flvtool2_monitor = ( 'monitor_init' ,"convertutil::monitor_init_unbuffered",
291 'monitor_line' , "VideoConverter::flvtool2_monitor_line",
292 'monitor_deinit' , "convertutil::monitor_deinit_unbuffered" );
293
294 $self->{'flvtool2_monitor'} = \@flvtool2_monitor;
295
296
297}
298
299
300
301sub optional_frame_scale
302{
303 my $self = shift (@_);
304 my ($orig_size,$video_width,$video_height) = @_;
305
306 my $s_opt = "";
307
308 if ($video_width > $video_height) {
309 if ($video_width > $orig_size) {
310 my $scale_factor = $orig_size/$video_width;
311 my $scaled_width = int($video_width * $scale_factor);
312 my $scaled_height = int($video_height * $scale_factor);
313
314 # round to be ensure multiple of 2 (needed by some codecs)
315 $scaled_width = int($scaled_width/2)*2;
316 $scaled_height = int($scaled_height/2)*2;
317
318 $s_opt = "-s ${scaled_width}x${scaled_height}";
319 }
320 # else, video is smaller than requested size, don't scale up
321 }
322 else {
323 if ($video_height > $orig_size) {
324 my $scale_factor = $orig_size/$video_height;
325 my $scaled_width = int($video_width * $scale_factor);
326 my $scaled_height = int($video_height * $scale_factor);
327
328 # round to be ensure multiple of 2 (needed by some codecs)
329 $scaled_width = int($scaled_width/2)*2;
330 $scaled_height = int($scaled_height/2)*2;
331
332 $s_opt = "-s ${scaled_width}x${scaled_height}";
333 }
334 # else, video is smaller than requested size, don't scale up
335
336 }
337
338 return $s_opt;
339}
340
341
342sub keyframe_cmd
343{
344 my $self = shift (@_);
345 my ($ivideo_filename) = @_;
346
347 my $video_ext_dir = &util::filename_cat($ENV{'GSDLHOME'},"ext","video");
348
349 my $output_dir = $self->{'cached_dir'};
350 my $ivideo_root = $self->{'cached_file_root'};
351
352 my $oshot_filename = &util::filename_cat($output_dir,"shots.xml");
353
354 my $exp_duration = $self->{'exp_duration'};
355 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
356
357 my $main_opts = "-y $t_opt";
358
359 my $hive = &util::filename_cat($video_ext_dir,"lib","vhook","hive.so");
360
361 my $oflash_filename = &util::filename_cat($output_dir,"$ivideo_root\_keyframe.flv");
362
363 my $vhook_opts = "$hive -o $oshot_filename -k $output_dir $ivideo_filename";
364
365 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
366 my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);
367
368 my $ffmpeg_cmd = "ffkeyframe $main_opts -vhook \"$vhook_opts\" -i \"$ivideo_filename_gsdlenv\" -an -y \"$oflash_filename_gsdlenv\"";
369
370
371 return ($ffmpeg_cmd,$oflash_filename);
372}
373
374
375sub stream_flv_video_cmd
376{
377 my $self = shift (@_);
378 my ($ivideo_filename,$video_width,$video_height,
379 $streaming_quality,
380 $streaming_bitrate,$streaming_size,
381 $opt_streaming_achan, $opt_streaming_arate) = @_;
382
383 my $streaming_achan
384 = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;
385
386 my $streaming_arate
387 = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;
388
389 my $output_dir = $self->{'cached_dir'};
390 my $ivideo_root = $self->{'cached_file_root'};
391
392 my $oflash_file = "${ivideo_root}_vstream.flv";
393 my $oflash_filename = &util::filename_cat($output_dir,$oflash_file);
394
395 my $s_opt = "";
396 my $media_type = $self->{'media_type'};
397 if ($media_type ne "audio") {
398 $s_opt = $self->optional_frame_scale($streaming_size,$video_width,$video_height);
399 }
400
401
402 my $exp_duration = $self->{'exp_duration'};
403 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
404
405 my $main_opts = "-y $t_opt";
406
407 my $bitrate_opt = "-b $streaming_bitrate";
408 ### my $stream_opts = "-r 25 $s_opt";
409 my $stream_opts .= " $s_opt -ac $streaming_achan -ar $streaming_arate";
410
411 # -flags +ilme+ildct' and maybe '-flags +alt' for interlaced material, and try '-top 0/1'
412
413 my $all_opts = "$main_opts $stream_opts";
414
415 my $ffmpeg_cmd;
416
417 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
418 my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);
419
420 if ($streaming_quality eq "high") {
421
422 my $pass_log_file = &util::filename_cat($output_dir,"$ivideo_root-logpass.txt");
423 if (-e $pass_log_file) {
424 &util::rm($pass_log_file);
425 }
426
427 my $pass_log_file_gsdlenv = $self->gsdlhome_independent($pass_log_file);
428
429 $all_opts .= " -passlogfile \"$pass_log_file_gsdlenv\"";
430
431 my $ffmpeg_cmd_pass1 = "ffmpeg -pass 1 -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
432
433 my $ffmpeg_cmd_pass2 = "ffmpeg -pass 2 -i \"$ivideo_filename_gsdlenv\" $all_opts $bitrate_opt -y \"$oflash_filename_gsdlenv\"";
434 $ffmpeg_cmd = "( $ffmpeg_cmd_pass1 && $ffmpeg_cmd_pass2 )";
435 }
436 else {
437 # single pass
438
439 $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
440 }
441
442 return ($ffmpeg_cmd,$oflash_filename,$oflash_file);
443}
444
445
446
447
448sub stream_flv_audio_cmd
449{
450 # AudioConverter also has a routine for doing this
451 # => merge into one!
452 # **************
453
454 my $self = shift (@_);
455 my ($ivideo_filename, $opt_streaming_achan, $opt_streaming_arate) = @_;
456
457 # Convert either audio or video to streamable audio format
458
459 my $streaming_achan
460 = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;
461
462 my $streaming_arate
463 = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;
464
465 my $output_dir = $self->{'cached_dir'};
466 my $ivideo_root = $self->{'cached_file_root'};
467
468 my $ofla_file = "${ivideo_root}_astream.flv";
469 my $ofla_filename = &util::filename_cat($output_dir,$ofla_file);
470
471 my $exp_duration = $self->{'exp_duration'};
472 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
473
474 my $main_opts = "-vn -y $t_opt";
475
476 my $stream_opts .= " -ac $streaming_achan -ar $streaming_arate";
477
478 my $all_opts = "$main_opts $stream_opts";
479
480 my $ffmpeg_cmd;
481
482 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
483 my $ofla_filename_gsdlenv = $self->gsdlhome_independent($ofla_filename);
484 $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$ofla_filename_gsdlenv\"";
485
486 return ($ffmpeg_cmd,$ofla_filename,$ofla_file);
487}
488
489
490
491
492sub audio_excerpt_cmd
493{
494 my $self = shift (@_);
495 my ($ivoa_filename,$hh,$mm,$ss,$opt_excerpt_len) = @_;
496
497 # ivoa = input video or audio
498
499 my $time_encoded = "$hh:$mm:$ss";
500 my $time_encoded_file = "$hh$mm$ss";
501
502
503 my $output_dir = $self->{'cached_dir'};
504 my $ivoa_root = $self->{'cached_file_root'};
505
506 my $omp3_file = "${ivoa_root}_$time_encoded_file.mp3";
507 my $omp3_filename = &util::filename_cat($output_dir,$omp3_file);
508
509 my $all_opts = "-y -acodec mp3 -ss $time_encoded ";
510
511 if (defined $opt_excerpt_len) {
512 $all_opts .= "-t $opt_excerpt_len ";
513 }
514
515
516 my $ivoa_filename_gsdlenv = $self->gsdlhome_independent($ivoa_filename);
517 my $omp3_filename_gsdlenv = $self->gsdlhome_independent($omp3_filename);
518
519
520 my $ffmpeg_cmd = "ffmpeg -i \"$ivoa_filename_gsdlenv\" $all_opts \"$omp3_filename_gsdlenv\"";
521
522 return ($ffmpeg_cmd,$omp3_filename,$omp3_file);
523}
524
525
526
527sub streamseekable_cmd
528{
529 my $self = shift (@_);
530 my ($oflash_filename) = @_;
531
532 my $output_dir = $self->{'cached_dir'};
533 my $ivideo_root = $self->{'cached_file_root'};
534
535 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
536
537 my $flvtool_cmd = "flvtool2 -vUP \"$oflash_filename\"";
538
539 return ($flvtool_cmd,$oflash_filename);
540}
541
542
543sub streamkeyframes_cmd
544{
545 my $self = shift (@_);
546 my ($oflash_filename,$doc_obj,$section) = @_;
547
548 my $assocfilepath
549 = $doc_obj->get_metadata_element($section,"assocfilepath");
550
551 my $output_dir = $self->{'cached_dir'};
552
553 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
554
555 my $video_server = $ENV{'GEXT_VIDEO_SERVER'};
556 my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};
557
558 my $collect = $ENV{'GSDLCOLLECTION'};
559
560 print STDERR "**** Warning: need to remove dependency of GEXT_VIDEO_SERVER and _PREFIX\n";
561
562 my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" -thumbLocation:$video_server$video_prefix/collect/$collect/index/assoc/$assocfilepath \"$oflash_filename\"";
563
564
565 return ($flvtool_cmd,$oflash_filename);
566}
567
568
569sub streamcuepts_cmd
570{
571 my $self = shift (@_);
572 my ($oflash_filename) = @_;
573
574 my $output_dir = $self->{'cached_dir'};
575
576 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
577
578 my $video_server = $ENV{'GEXT_VIDEO_SERVER'};
579 my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};
580
581 my $collect = $ENV{'GSDLCOLLECTION'};
582
583 ## my $thumbloc = "$video_server$video_prefix/collect/$collect";
584
585
586# my $flvtool_cmd = "flvtool2 -vUAtP \"$cue_filename\" -thumbLocation:$thumbloc \"$oflash_filename\"";
587
588# my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";
589
590
591
592# my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";
593
594
595## my $flvtool_cmd = "flvtool2 -vAt \"$cue_filename\" -UP \"$oflash_filename\" \"$output_dir/updated.flv\"";
596
597## my $flvtool_cmd = "flvtool2 -vAtU \"$cue_filename\" \"$oflash_filename\" \"$output_dir/updated.flv\"";
598
599 my $flvtool_cmd = "flvtool2 -vAtUP \"$cue_filename\" \"$oflash_filename\"";
600
601 return ($flvtool_cmd,$oflash_filename);
602}
603
604
605sub keyframe_thumbnail_cmd
606{
607 my $self = shift (@_);
608 my ($ivideo_filename,$thumbnailfile,$thumbnailwidth,$thumbnailheight) = @_;
609
610 my $output_dir = $self->{'cached_dir'};
611 my $ivideo_root = $self->{'cached_file_root'};
612
613 my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);
614
615
616 # Try for 4th keyframe, but fall back to 1st if doesn't exist
617 my $key_filename = "${key_filename_prefix}_0003.jpg";
618 $key_filename = "${key_filename_prefix}_0000.jpg" if (!-e $key_filename);
619
620 my $key_filename_gsdlenv = $self->gsdlhome_independent($key_filename);
621 my $thumbnailfile_gsdlenv = $self->gsdlhome_independent($thumbnailfile);
622
623 my $command;
624
625 if (-e $key_filename) {
626 $command = "convert -interlace plane -verbose -geometry $thumbnailwidth"
627 . "x$thumbnailheight \"$key_filename_gsdlenv\" \"$thumbnailfile_gsdlenv\"";
628 }
629 else {
630 # extractkeyframe has either not been switched on, or else had
631 # a problem when running
632 # => extract a from
633 # my $frame_rate = 1.0 / 60.0;
634
635 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
636
637
638
639 $command = "ffmpeg -i \"$ivideo_filename_gsdlenv\" -ss 12.5 -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -y \"$thumbnailfile_gsdlenv\"";
640
641 # fmpeg -i input.dv -r 1 -f image2 -s 120x96 images%05d.png
642 }
643
644 return ($command,$thumbnailfile);
645}
646
647
648sub keyframe_montage_cmd
649{
650 my $self = shift (@_);
651 my ($ivideo_filename,$montagefile) = @_;
652
653 my $output_dir = $self->{'cached_dir'};
654 my $ivideo_root = $self->{'cached_file_root'};
655
656 my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);
657
658 my $options = "-tile 10 -geometry 75x62+2+2";
659
660 my $command = "montage $options ${key_filename_prefix}_*.jpg \"$montagefile\"";
661
662 return ($command,$montagefile);
663}
664
665
666
667sub parse_shot_xml
668{
669 my ($self) = shift(@_);
670
671 my $outhandle = $self->{'outhandle'};
672 my $output_dir = $self->{'cached_dir'};
673
674 my $shots_filename = &util::filename_cat($output_dir,"shots.xml");
675
676 eval {
677 $self->{'parser'}->parsefile($shots_filename);
678 };
679
680 if ($@) {
681 print $outhandle "VideoConverter: skipping $shots_filename as not conformant to Hive shot syntax\n" if ($self->{'verbosity'} > 1);
682 print $outhandle "\n Perl Error:\n $@\n" if ($self->{'verbosity'}>2);
683 return 0;
684 }
685
686}
687
688sub associate_keyframes_old
689{
690 my ($self) = shift(@_);
691
692 my ($doc_obj,$section) = @_;
693
694 my $output_dir = $self->{'cached_dir'};
695
696 my $count = 1;
697 foreach my $kframe_file (@{$self->{'keyframe_fnames'}}) {
698
699 my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
700 $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg","image/jpeg",
701 $section);
702 $count++;
703 }
704
705 $doc_obj->add_utf8_metadata($section,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));
706
707
708 # *****
709 # $doc_obj->add_metadata ($section, "thumblist", $self->{'flowplayer_thumblist'});
710
711}
712
713sub associate_keyframes
714{
715 my ($self) = shift(@_);
716
717 my ($doc_obj,$section) = @_;
718
719 my $output_dir = $self->{'cached_dir'};
720 my $timeline = $self->{'keyframe_timeline'};
721
722 my $count = 1;
723
724 foreach my $t (sort { $timeline->{$a}->{'keyframeindex'} <=> $timeline->{$b}->{'keyframeindex'} } keys %$timeline)
725 {
726 my $kframe_file = $timeline->{$t}->{'thumb'};
727 my $timestamp = $timeline->{$t}->{'timestamp'};
728
729 my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
730 $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg","image/jpeg",
731 $section);
732 $doc_obj->add_utf8_metadata($section,"KeyframeTimestamp",$timestamp);
733
734 $count++;
735 }
736
737 $doc_obj->add_utf8_metadata($section,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));
738
739
740 # *****
741 # $doc_obj->add_metadata ($section, "thumblist", $self->{'flowplayer_thumblist'});
742}
743
744
745
746sub enable_audio_streaming
747{
748 my $self = shift (@_);
749 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated) = @_;
750
751 my $section = $doc_obj->get_top_section();
752
753 my $output_dir = $self->{'cached_dir'};
754 my $ivideo_root = $self->{'cached_file_root'};
755
756 # Generate FLV audio-only format for streaming purposes
757
758 my $optionally_run_general_cmd = "run_uncached_general_cmd";
759 if ($self->{'enable_cache'}) {
760 $optionally_run_general_cmd
761 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
762 }
763
764
765 my ($stream_cmd,$ofla_filename,$ofla_file)
766 = $self->stream_flv_audio_cmd($originalfilename || $filename);
767
768
769 my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
770 'message_prefix' => "Stream",
771 'message' => "Generating streamable audio: $ofla_file" };
772
773
774 my ($streamable_regenerated,$streamable_result,$streamable_had_error)
775 = $self->$optionally_run_general_cmd($stream_cmd,$ofla_filename,
776 $streamable_options);
777
778
779 if (!$streamable_had_error) {
780 my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($ofla_filename);
781
782 my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
783 'message_prefix' => "Stream Seekable",
784 'message' => "Reprocessing audio stream to be seekable by timeline: $ofla_file" };
785
786 if ($streamable_regenerated) {
787 $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
788 }
789
790 my $streamable_url = $ofla_file;
791 my $streamable_url_safe = $self->url_safe($streamable_url);
792
793 $doc_obj->add_utf8_metadata ($section, "streamableaudio", $streamable_url_safe);
794 $doc_obj->associate_file($ofla_filename,$ofla_file,"audio/flash",
795 $section);
796 }
797
798 $doc_obj->add_metadata ($section, "audioflashwidth", 400);
799 $doc_obj->add_metadata ($section, "audioflashheight", 22 + 100);
800
801 $self->{'ofla_file'} = $ofla_file;
802 $self->{'ofla_filename'} = $ofla_filename;
803
804 return $streamable_regenerated;
805}
806
807
808
809
810
811sub enable_video_streaming
812{
813 my $self = shift (@_);
814 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
815 $video_width,$video_height) = @_;
816
817 my $section = $doc_obj->get_top_section();
818
819 my $output_dir = $self->{'cached_dir'};
820 my $ivideo_root = $self->{'cached_file_root'};
821
822 # Generate the Flash FLV format for streaming purposes
823
824 my $optionally_run_general_cmd = "run_uncached_general_cmd";
825 if ($self->{'enable_cache'}) {
826 $optionally_run_general_cmd
827 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
828 }
829
830
831 my $streaming_bitrate = $self->{'streamingbitrate'};
832 my $streaming_size = $self->{'streamingsize'};
833
834 my $streaming_quality = "high";
835
836 my ($stream_cmd,$oflash_filename,$oflash_file)
837 = $self->stream_flv_video_cmd($originalfilename || $filename,
838 $video_width,$video_height,
839 $streaming_quality,
840 $streaming_bitrate, $streaming_size);
841
842
843 my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
844 'message_prefix' => "Stream",
845 'message' => "Generating streamable video: $oflash_file" };
846
847
848 my ($streamable_regenerated,$streamable_result,$streamable_had_error)
849 = $self->$optionally_run_general_cmd($stream_cmd,$oflash_filename,$streamable_options);
850
851 if (!$streamable_had_error) {
852 my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($oflash_filename);
853
854 my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
855 'message_prefix' => "Stream Seekable",
856 'message' => "Reprocessing video stream to be seekable by timeline: $oflash_file" };
857
858 if ($streamable_regenerated) {
859 $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
860 }
861
862 my $streamable_url = $oflash_file;
863 my $streamable_url_safe = $self->url_safe($streamable_url);
864
865 $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
866 $doc_obj->associate_file($oflash_filename,$oflash_file,"video/flash",
867 $section);
868 }
869
870
871 #
872 # FlowPlayer.swf height+22 pixels
873 # FlowPlayerBlack.swf height+16 pixels
874 # FlowPlayerThermo.swf height+16 pixels
875 # FlowPlayerWhite.swf height+26 pixels
876
877 my $flashwidth = $video_width;
878 my $flashheight = $video_height + 22;
879
880 if ($self->{'extractkeyframes'}) {
881 $flashheight += 100;
882 }
883
884 $doc_obj->add_metadata ($section, "flashwidth", $flashwidth);
885 $doc_obj->add_metadata ($section, "flashheight", $flashheight);
886
887 $self->{'oflash_file'} = $oflash_file;
888 $self->{'oflash_filename'} = $oflash_filename;
889
890 return $streamable_regenerated;
891}
892
893
894sub enable_full_streaming
895{
896 my $self = shift (@_);
897 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
898 $video_width,$video_height) = @_;
899
900 my $video_streamable_regenerated
901 = $self->enable_video_streaming($doc_obj,$originalfilename,$filename,
902 $convertto_regenerated,
903 $video_width,$video_height);
904
905 my $audio_streamable_regenerated
906 = $self->enable_audio_streaming($doc_obj,$originalfilename,$filename,
907 $convertto_regenerated);
908
909 return ($video_streamable_regenerated || $audio_streamable_regenerated);
910}
911
912
913
914sub flvtool2_monitor_line
915{
916 my ($line) = @_;
917
918 my $had_error = 0;
919 my $generate_dot = 1;
920
921 if ($line =~ m/\s+\- /) {
922 # ignore tabulated output printed at end of command
923 $generate_dot = 0;
924 }
925
926 if ($line =~ m/^Error:/i) {
927 $had_error = 1;
928 }
929
930 return ($had_error,$generate_dot);
931}
932
933
934
935
936
9371;
Note: See TracBrowser for help on using the repository browser.