source: gs2-extensions/video/trunk/perllib/plugins/VideoConverter.pm@ 21606

Last change on this file since 21606 was 21606, checked in by kjdon, 12 years ago

insert_section inserts *before* the specified section, so need to get the end child each time, otherwise each new one will be inserted before the previous one instead of after

File size: 31.2 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' => "extract_keyframes",
109 'desc' => "{VideoPlugin.extractkeyframes}",
110 'type' => "flag",
111 'deft' => "0",
112 'reqd' => "no" },
113 { 'name' => "keep_keyframes",
114 'desc' => "{VideoPlugin.keep_keyframes}",
115 'type' => "string",
116 'deft' => "all",
117 'reqd' => "no" },
118 { 'name' => "streamingsize",
119 'desc' => "{VideoPlugin.streamingsize}",
120 'type' => "int",
121 'deft' => "352",
122 'reqd' => "no" },
123 { 'name' => "streamingbitrate",
124 'desc' => "{VideoPlugin.streamingbitrate}",
125 'type' => "string",
126 'deft' => "200k",
127 'reqd' => "no" },
128 { 'name' => "streamingHQsize",
129 'desc' => "{VideoPlugin.streamingsize}",
130 'type' => "int",
131 'deft' => "576",
132 'reqd' => "no" },
133 { 'name' => "streamingHQVideoBitrate",
134 'desc' => "{VideoPlugin.streamingbitrate}",
135 'type' => "int",
136 'deft' => "426",
137 'reqd' => "no" },
138 { 'name' => "streamingHQAudioBitrate",
139 'desc' => "{VideoPlugin.streamingbitrate}",
140 'type' => "int",
141 'deft' => "86",
142 'reqd' => "no" },
143
144 { 'name' => "minimumsize",
145 'desc' => "{ImageConverter.minimumsize}",
146 'type' => "int",
147 'deft' => "100",
148 'range' => "1,",
149 'reqd' => "no" },
150
151 ];
152
153my $options = { 'name' => "VideoConverter",
154 'desc' => "{VideoConverter.desc}",
155 'abstract' => "yes",
156 'inherits' => "yes",
157 'args' => $arguments };
158
159sub new {
160 my ($class) = shift (@_);
161 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
162 push(@$pluginlist, $class);
163
164 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
165 push(@{$hashArgOptLists->{"OptList"}},$options);
166
167 my $self = new MultimediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
168
169
170 return bless $self, $class;
171
172}
173
174
175
176
177# Discover the characteristics of a video file.
178
179# Equivalent step to that in ImagePlugin that uses ImageMagicks's
180# 'indentify' utility
181
182# Here we use 'ffmpeg' for video but for consistency keep the Perl
183# method name the same as before
184
185
186sub identify {
187 my ($video, $outhandle, $verbosity) = @_;
188
189 # Use the ffmpeg command to get the file specs
190 my $command = "ffmpeg -i \"$video\"";
191
192 print $outhandle " $command\n" if ($verbosity > 2);
193 my $result = '';
194 $result = `$command 2>&1`;
195 print $outhandle " $result\n" if ($verbosity > 4);
196
197 # Read the type, width, and height etc.
198 my $vtype = 'unknown';
199 my $vcodec = 'unknown';
200 my $width = 'unknown';
201 my $height = 'unknown';
202 my $fps = 'unknown';
203
204 my $atype = 'unknown';
205 my $afreq = 'unknown';
206 my $achan = 'unknown';
207 my $arate = 'unknown';
208
209 my $video_safe = quotemeta $video;
210
211 # strip off everything up to filename
212 $result =~ s/^.*\'$video_safe\'://s;
213
214## if ($result =~ m/Video: (.*?) fps/m) {
215 if ($result =~ m/^\s+Stream.*?Video: (.*?)$/m) {
216 my $video_info = $1;
217 $video_info =~ s/\s*\[.*?\]//g;
218
219 my @video_fields = split(/,\s*/,$video_info);
220
221 $vtype = shift @video_fields;
222
223 if ($video_fields[0] !~ m/(\d+)x(\d+)/) {
224 $vcodec = shift @video_fields;
225 }
226 my $video_dim = shift @video_fields;
227
228 ($width,$height) = ($video_dim =~ m/(\d+)x(\d+)/);
229
230# if ($video_info =~ m/([^,]+),(?: ([^,]+),)? (\d+)x(\d+),.*?(\d+\.\d+)/)
231# {
232# $vtype = $1;
233# $vcodec = $2 if defined $2;
234# $width = $3;
235# $height = $4;
236# $fps = $5;
237# }
238 }
239
240 if ($result =~ m/Audio: (\w+), (\d+) Hz, (\w+)(?:, (\d+.*))?/m) {
241 $atype = $1;
242 $afreq = $2;
243 $achan = $3;
244 $arate = $4 if (defined $4);
245 }
246
247 # Read the duration
248 my $duration = "unknown";
249 if ($result =~ m/Duration: (\d+:\d+:\d+\.\d+)/m) {
250 $duration = $1;
251 }
252 print $outhandle " file: $video:\t $vtype, $width, $height, $duration\n"
253 if ($verbosity > 2);
254
255 if ($verbosity >3) {
256 print $outhandle "\t video codec=$vcodec, fps = $fps\n";
257 print $outhandle "\t audio codec=$atype, freq = $afreq Hz, $achan, $arate\n";
258 }
259
260 # Return the specs
261 return ($vtype, $width, $height, $duration, -s $video,
262 $vcodec,$fps,$atype,$afreq,$achan,$arate);
263}
264
265
266sub vob_durations
267{
268 my ($media_base_dir,$title_num,$outhandle) = @_;
269
270 my $filter_re = sprintf("^VTS_%02d_[1-9]\\.VOB\$",$title_num);
271
272 my $duration_info = {};
273
274 if (opendir(VTSIN,$media_base_dir)) {
275 my @vts_title_vobs = grep { $_ =~ m/$filter_re/ } readdir(VTSIN);
276 closedir(VTSIN);
277
278 foreach my $v (@vts_title_vobs) {
279 my $full_v = &util::filename_cat($media_base_dir,$v);
280
281 my ($vtype, $width, $height, $duration, $vsize,
282 $vcodec,$fps,$atype,$afreq,$achan,$arate) = identify($full_v,$outhandle,0);
283
284 my ($vob_num) = ($v =~ m/^VTS_\d\d_(\d)\.VOB$/);
285
286 $duration_info->{$vob_num} = $duration;
287 print STDERR "**** $title_num: $title_num, storing {$vob_num} => $duration\n";
288
289 }
290
291 }
292 else {
293 print $outhandle "Warning: unable to read files in directory $media_base_dir\n";
294 }
295
296 return $duration_info;
297
298}
299
300
301
302sub init_cache_for_file {
303 my $self = shift(@_);
304
305 my ($video_filename) = @_;
306
307 $self->SUPER::init_cache_for_file($video_filename);
308
309
310 my @flvtool2_monitor = ( 'monitor_init' ,"convertutil::monitor_init_unbuffered",
311 'monitor_line' , "VideoConverter::flvtool2_monitor_line",
312 'monitor_deinit' , "convertutil::monitor_deinit_unbuffered" );
313
314 $self->{'flvtool2_monitor'} = \@flvtool2_monitor;
315
316
317}
318
319
320
321sub optional_frame_scale
322{
323 my $self = shift (@_);
324 my ($orig_size,$video_width,$video_height) = @_;
325
326 my $s_opt = "";
327
328 if ($video_width > $video_height) {
329 if ($video_width > $orig_size) {
330 my $scale_factor = $orig_size/$video_width;
331 my $scaled_width = int($video_width * $scale_factor);
332 my $scaled_height = int($video_height * $scale_factor);
333
334 # round to be ensure multiple of 2 (needed by some codecs)
335 $scaled_width = int($scaled_width/2)*2;
336 $scaled_height = int($scaled_height/2)*2;
337
338 $s_opt = "-s ${scaled_width}x${scaled_height}";
339 }
340 # else, video is smaller than requested size, don't scale up
341 }
342 else {
343 if ($video_height > $orig_size) {
344 my $scale_factor = $orig_size/$video_height;
345 my $scaled_width = int($video_width * $scale_factor);
346 my $scaled_height = int($video_height * $scale_factor);
347
348 # round to be ensure multiple of 2 (needed by some codecs)
349 $scaled_width = int($scaled_width/2)*2;
350 $scaled_height = int($scaled_height/2)*2;
351
352 $s_opt = "-s ${scaled_width}x${scaled_height}";
353 }
354 # else, video is smaller than requested size, don't scale up
355
356 }
357
358 return $s_opt;
359}
360
361
362sub keyframe_cmd
363{
364 my $self = shift (@_);
365 my ($ivideo_filename) = @_;
366
367 my $video_ext_dir = &util::filename_cat($ENV{'GSDLHOME'},"ext","video");
368
369 my $output_dir = $self->{'cached_dir'};
370 my $ivideo_root = $self->{'cached_file_root'};
371
372 my $oshot_filename = &util::filename_cat($output_dir,"shots.xml");
373
374 my $exp_duration = $self->{'exp_duration'};
375 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
376
377 my $main_opts = "-y $t_opt";
378
379 my $hive = &util::filename_cat($video_ext_dir,"lib","vhook","hive.so");
380
381 my $oflash_filename = &util::filename_cat($output_dir,"$ivideo_root\_keyframe.flv");
382
383 my $vhook_opts = "$hive -o $oshot_filename -k $output_dir $ivideo_filename";
384
385 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
386 my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);
387
388 my $ffmpeg_cmd = "ffkeyframe $main_opts -vhook \"$vhook_opts\" -i \"$ivideo_filename_gsdlenv\" -an -y \"$oflash_filename_gsdlenv\"";
389
390
391 return ($ffmpeg_cmd,$oflash_filename);
392}
393
394
395sub stream_mp4_video_cmd
396{
397 my $self = shift (@_);
398 my ($ivideo_filename,$streaming_HQ_size, $streaming_HQ_VideoBitrate,
399 $streaming_HQ_AudioBitrate) = @_;
400
401
402 my $output_dir = $self->{'cached_dir'};
403 my $ivideo_root = $self->{'cached_file_root'};
404
405 my $omp4_file = "${ivideo_root}_vHQstream.mp4";
406 my $omp4_filename = &util::filename_cat($output_dir,$omp4_file);
407
408
409 #my $exp_duration = $self->{'exp_duration'};
410 #my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
411
412
413
414 my $size;
415 # Looks for empty string as well as fullsize string
416 if(!$streaming_HQ_size || $streaming_HQ_size eq "fullsize") {
417 $size = "--strict-anamorphic";
418 } else {
419 $size = "-w $streaming_HQ_size --loose-anamorphic";
420 }
421
422
423 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
424 my $omp4_filename_gsdlenv = $self->gsdlhome_independent($omp4_filename);
425
426 my $pre_opts = "--decomb -t 1 -c 1";
427 my $post_opts = "-f mp4 $size -O -e x264 -b $streaming_HQ_VideoBitrate -a 1 -E faac -6 dpl2 -B $streaming_HQ_AudioBitrate -D 0.0 -x ref=2:bframes=2:me-umh -v 1";
428
429 my $handbrake_cmd = "HandbrakeCLI.exe -i \"$ivideo_filename_gsdlenv\" $pre_opts -o \"$omp4_filename_gsdlenv\" $post_opts";
430
431
432 return ($handbrake_cmd,$omp4_filename,$omp4_file);
433}
434
435
436
437sub stream_flv_video_cmd
438{
439 my $self = shift (@_);
440 my ($ivideo_filename,$video_width,$video_height,
441 $streaming_quality,
442 $streaming_bitrate,$streaming_size,
443 $opt_streaming_achan, $opt_streaming_arate) = @_;
444
445 my $streaming_achan
446 = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;
447
448 my $streaming_arate
449 = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;
450
451 my $output_dir = $self->{'cached_dir'};
452 my $ivideo_root = $self->{'cached_file_root'};
453
454 my $oflash_file = "${ivideo_root}_vstream.flv";
455 my $oflash_filename = &util::filename_cat($output_dir,$oflash_file);
456
457 my $s_opt = "";
458 my $media_type = $self->{'media_type'};
459 if ($media_type ne "audio") {
460 $s_opt = $self->optional_frame_scale($streaming_size,$video_width,$video_height);
461 }
462
463
464 my $exp_duration = $self->{'exp_duration'};
465 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
466
467 my $main_opts = "-y $t_opt";
468
469 my $bitrate_opt = "-b $streaming_bitrate";
470 ### my $stream_opts = "-r 25 $s_opt";
471 my $stream_opts .= " $s_opt -ac $streaming_achan -ar $streaming_arate";
472
473 # -flags +ilme+ildct' and maybe '-flags +alt' for interlaced material, and try '-top 0/1'
474
475 my $all_opts = "$main_opts $stream_opts";
476
477 my $ffmpeg_cmd;
478
479 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
480 my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);
481
482 if ($streaming_quality eq "high") {
483
484 my $pass_log_file = &util::filename_cat($output_dir,"$ivideo_root-logpass.txt");
485 if (-e $pass_log_file) {
486 &util::rm($pass_log_file);
487 }
488
489 my $pass_log_file_gsdlenv = $self->gsdlhome_independent($pass_log_file);
490
491 $all_opts .= " -passlogfile \"$pass_log_file_gsdlenv\"";
492
493 my $ffmpeg_cmd_pass1 = "ffmpeg -pass 1 -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
494
495 my $ffmpeg_cmd_pass2 = "ffmpeg -pass 2 -i \"$ivideo_filename_gsdlenv\" $all_opts $bitrate_opt -y \"$oflash_filename_gsdlenv\"";
496 $ffmpeg_cmd = "( $ffmpeg_cmd_pass1 && $ffmpeg_cmd_pass2 )";
497 }
498 else {
499 # single pass
500
501 $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
502 }
503
504 return ($ffmpeg_cmd,$oflash_filename,$oflash_file);
505}
506
507
508
509
510sub stream_flv_audio_cmd
511{
512 # AudioConverter also has a routine for doing this
513 # => merge into one!
514 # **************
515
516 my $self = shift (@_);
517 my ($ivideo_filename, $opt_streaming_achan, $opt_streaming_arate) = @_;
518
519 # Convert either audio or video to streamable audio format
520
521 my $streaming_achan
522 = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;
523
524 my $streaming_arate
525 = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;
526
527 my $output_dir = $self->{'cached_dir'};
528 my $ivideo_root = $self->{'cached_file_root'};
529
530 my $ofla_file = "${ivideo_root}_astream.flv";
531 my $ofla_filename = &util::filename_cat($output_dir,$ofla_file);
532
533 my $exp_duration = $self->{'exp_duration'};
534 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
535
536 my $main_opts = "-vn -y $t_opt";
537
538 my $stream_opts .= " -ac $streaming_achan -ar $streaming_arate";
539
540 my $all_opts = "$main_opts $stream_opts";
541
542 my $ffmpeg_cmd;
543
544 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
545 my $ofla_filename_gsdlenv = $self->gsdlhome_independent($ofla_filename);
546 $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$ofla_filename_gsdlenv\"";
547
548 return ($ffmpeg_cmd,$ofla_filename,$ofla_file);
549}
550
551
552
553
554sub audio_excerpt_cmd
555{
556 my $self = shift (@_);
557 my ($ivoa_filename,$hh,$mm,$ss,$opt_excerpt_len) = @_;
558
559 # ivoa = input video or audio
560
561 my $time_encoded = "$hh:$mm:$ss";
562 my $time_encoded_file = "$hh$mm$ss";
563
564
565 my $output_dir = $self->{'cached_dir'};
566 my $ivoa_root = $self->{'cached_file_root'};
567
568 my $omp3_file = "${ivoa_root}_$time_encoded_file.mp3";
569 my $omp3_filename = &util::filename_cat($output_dir,$omp3_file);
570
571 my $all_opts = "-y -acodec mp3 -ss $time_encoded ";
572
573 if (defined $opt_excerpt_len) {
574 $all_opts .= "-t $opt_excerpt_len ";
575 }
576
577
578 my $ivoa_filename_gsdlenv = $self->gsdlhome_independent($ivoa_filename);
579 my $omp3_filename_gsdlenv = $self->gsdlhome_independent($omp3_filename);
580
581
582 my $ffmpeg_cmd = "ffmpeg -i \"$ivoa_filename_gsdlenv\" $all_opts \"$omp3_filename_gsdlenv\"";
583
584 return ($ffmpeg_cmd,$omp3_filename,$omp3_file);
585}
586
587
588
589sub streamseekable_cmd
590{
591 my $self = shift (@_);
592 my ($oflash_filename) = @_;
593
594 my $output_dir = $self->{'cached_dir'};
595 my $ivideo_root = $self->{'cached_file_root'};
596
597 ## my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
598
599 my $flvtool_cmd = "flvtool2 -vUP \"$oflash_filename\"";
600
601 return ($flvtool_cmd,$oflash_filename);
602}
603
604
605sub streamkeyframes_cmd
606{
607 my $self = shift (@_);
608 my ($oflash_filename,$doc_obj,$section) = @_;
609
610 my $assocfilepath
611 = $doc_obj->get_metadata_element($section,"assocfilepath");
612
613 my $output_dir = $self->{'cached_dir'};
614
615 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
616
617 my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" \"$oflash_filename\"";
618
619 return ($flvtool_cmd,$oflash_filename);
620}
621
622
623sub streamkeyframes_cmd_old
624{
625 my $self = shift (@_);
626 my ($oflash_filename,$doc_obj,$section) = @_;
627
628 my $assocfilepath
629 = $doc_obj->get_metadata_element($section,"assocfilepath");
630
631 my $output_dir = $self->{'cached_dir'};
632
633 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
634
635 my $video_server = $ENV{'GEXT_VIDEO_SERVER'};
636 my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};
637
638 my $collect = $ENV{'GSDLCOLLECTION'};
639
640 print STDERR "**** Warning: need to remove dependency of GEXT_VIDEO_SERVER and _PREFIX\n";
641
642 my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" -thumbLocation:$video_server$video_prefix/collect/$collect/index/assoc/$assocfilepath \"$oflash_filename\"";
643
644
645 return ($flvtool_cmd,$oflash_filename);
646}
647
648
649sub streamcuepts_cmd
650{
651 my $self = shift (@_);
652 my ($oflash_filename) = @_;
653
654 my $output_dir = $self->{'cached_dir'};
655
656 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
657
658 ##my $video_server = $ENV{'GEXT_VIDEO_SERVER'};
659 ##my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};
660
661 ## my $collect = $ENV{'GSDLCOLLECTION'};
662
663 ## my $thumbloc = "$video_server$video_prefix/collect/$collect";
664
665
666# my $flvtool_cmd = "flvtool2 -vUAtP \"$cue_filename\" -thumbLocation:$thumbloc \"$oflash_filename\"";
667
668# my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";
669
670
671
672# my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";
673
674
675## my $flvtool_cmd = "flvtool2 -vAt \"$cue_filename\" -UP \"$oflash_filename\" \"$output_dir/updated.flv\"";
676
677## my $flvtool_cmd = "flvtool2 -vAtU \"$cue_filename\" \"$oflash_filename\" \"$output_dir/updated.flv\"";
678
679 my $flvtool_cmd = "flvtool2 -vAtUP \"$cue_filename\" \"$oflash_filename\"";
680
681 return ($flvtool_cmd,$oflash_filename);
682}
683
684
685sub keyframe_thumbnail_cmd
686{
687 my $self = shift (@_);
688 my ($ivideo_filename,$thumbnailfile,$thumbnailwidth,$thumbnailheight) = @_;
689
690 my $output_dir = $self->{'cached_dir'};
691 my $ivideo_root = $self->{'cached_file_root'};
692
693 my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);
694
695
696 # Try for 4th keyframe, but fall back to 1st if doesn't exist
697 my $key_filename = "${key_filename_prefix}_0003.jpg";
698 $key_filename = "${key_filename_prefix}_0000.jpg" if (!-e $key_filename);
699
700 my $key_filename_gsdlenv = $self->gsdlhome_independent($key_filename);
701 my $thumbnailfile_gsdlenv = $self->gsdlhome_independent($thumbnailfile);
702
703 my $command;
704
705 if (-e $key_filename) {
706 $command = "convert -interlace plane -verbose -geometry $thumbnailwidth"
707 . "x$thumbnailheight \"$key_filename_gsdlenv\" \"$thumbnailfile_gsdlenv\"";
708 }
709 else {
710 # extract_keyframe has either not been switched on, or else had
711 # a problem when running
712 # => extract a from
713 # my $frame_rate = 1.0 / 60.0;
714
715 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
716
717 $command = "ffmpeg -i \"$ivideo_filename_gsdlenv\" -ss 12.5 -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -y \"$thumbnailfile_gsdlenv\"";
718
719 # fmpeg -i input.dv -r 1 -f image2 -s 120x96 images%05d.png
720 }
721
722 return ($command,$thumbnailfile);
723}
724
725
726sub keyframe_montage_cmd
727{
728 my $self = shift (@_);
729 my ($ivideo_filename,$montagefile) = @_;
730
731 my $output_dir = $self->{'cached_dir'};
732 my $ivideo_root = $self->{'cached_file_root'};
733
734 my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);
735
736 my $options = "-tile 10 -geometry 75x62+2+2";
737
738 my $command = "montage $options ${key_filename_prefix}_*.jpg \"$montagefile\"";
739
740 return ($command,$montagefile);
741}
742
743
744
745sub parse_shot_xml
746{
747 my ($self) = shift(@_);
748
749 my $outhandle = $self->{'outhandle'};
750 my $output_dir = $self->{'cached_dir'};
751
752 my $shots_filename = &util::filename_cat($output_dir,"shots.xml");
753
754 eval {
755 $self->{'parser'}->parsefile($shots_filename);
756 };
757
758 if ($@) {
759 print $outhandle "VideoConverter: skipping $shots_filename as not conformant to Hive shot syntax\n" if ($self->{'verbosity'} > 1);
760 print $outhandle "\n Perl Error:\n $@\n" if ($self->{'verbosity'}>2);
761 return 0;
762 }
763
764}
765
766sub associate_keyframes_old
767{
768 my ($self) = shift(@_);
769
770 my ($doc_obj,$section) = @_;
771
772 my $output_dir = $self->{'cached_dir'};
773
774 my $count = 1;
775 foreach my $kframe_file (@{$self->{'keyframe_fnames'}}) {
776
777 my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
778 $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg","image/jpeg",
779 $section);
780 $count++;
781 }
782
783 $doc_obj->add_utf8_metadata($section,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));
784
785
786 # *****
787 # $doc_obj->add_metadata ($section, "thumblist", $self->{'flowplayer_thumblist'});
788
789}
790
791sub associate_keyframes
792{
793 my ($self) = shift(@_);
794
795 my ($doc_obj,$topsection,$timeline) = @_;
796
797 my $output_dir = $self->{'cached_dir'};
798
799 my $count = 1;
800
801 foreach my $t (sort { $timeline->{$a}->{'keyframeindex'} <=> $timeline->{$b}->{'keyframeindex'} } keys %$timeline)
802 {
803 my $kframe_file = $timeline->{$t}->{'thumb'};
804 my $timestamp = $timeline->{$t}->{'timestamp'};
805
806 # create next sub-section to video "document"
807 my $endchild = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
808 $doc_obj->add_utf8_metadata($endchild,"Title","Timestamp $timestamp");
809 $doc_obj->add_utf8_metadata($endchild,"FrameNum",$t);
810
811 my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
812 $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg",
813 "image/jpeg",
814 $endchild);
815
816 $doc_obj->add_utf8_metadata($endchild,"assockeyframe","keyframe$count.jpg");
817
818 $doc_obj->add_utf8_metadata($topsection,"KeyframeTimestamp",$timestamp);
819 $doc_obj->add_utf8_metadata($topsection,"KeyframeFrameNum",$t);
820
821
822
823 $count++;
824 }
825
826 #### $doc_obj->add_utf8_metadata($topsection,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));
827
828 $doc_obj->add_utf8_metadata($topsection,"NumKeyframes",scalar(keys %$timeline));
829
830
831 # *****
832 # $doc_obj->add_metadata ($topsection, "thumblist", $self->{'flowplayer_thumblist'});
833}
834
835
836
837sub enable_audio_streaming
838{
839 my $self = shift (@_);
840 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated) = @_;
841
842 my $section = $doc_obj->get_top_section();
843
844 my $output_dir = $self->{'cached_dir'};
845 my $ivideo_root = $self->{'cached_file_root'};
846
847 # Generate FLV audio-only format for streaming purposes
848
849 my $optionally_run_general_cmd = "run_uncached_general_cmd";
850 if ($self->{'enable_cache'}) {
851 $optionally_run_general_cmd
852 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
853 }
854
855
856 my ($stream_cmd,$ofla_filename,$ofla_file)
857 = $self->stream_flv_audio_cmd($originalfilename || $filename);
858
859
860 my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
861 'message_prefix' => "Stream",
862 'message' => "Generating streamable audio: $ofla_file" };
863
864
865 my ($streamable_regenerated,$streamable_result,$streamable_had_error)
866 = $self->$optionally_run_general_cmd($stream_cmd,$ofla_filename,
867 $streamable_options);
868
869
870 if (!$streamable_had_error) {
871 my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($ofla_filename);
872
873 my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
874 'message_prefix' => "Stream Seekable",
875 'message' => "Reprocessing audio stream to be seekable by timeline: $ofla_file" };
876
877 if ($streamable_regenerated) {
878 $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
879 }
880
881 my $streamable_url = $ofla_file;
882 my $streamable_url_safe = $self->url_safe($streamable_url);
883
884 $doc_obj->add_utf8_metadata ($section, "streamableaudio", $streamable_url_safe);
885 $doc_obj->associate_file($ofla_filename,$ofla_file,"audio/flash",
886 $section);
887 }
888
889 $doc_obj->add_metadata ($section, "audioflashwidth", 400);
890 $doc_obj->add_metadata ($section, "audioflashheight", 22 + 100);
891
892 $self->{'ofla_file'} = $ofla_file;
893 $self->{'ofla_filename'} = $ofla_filename;
894
895 return $streamable_regenerated;
896}
897
898
899
900
901
902sub enable_video_streaming
903{
904 my $self = shift (@_);
905 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
906 $video_width,$video_height) = @_;
907
908 my $section = $doc_obj->get_top_section();
909
910 my $output_dir = $self->{'cached_dir'};
911 my $ivideo_root = $self->{'cached_file_root'};
912
913 # Generate the Flash FLV format for streaming purposes
914
915 my $optionally_run_general_cmd = "run_uncached_general_cmd";
916 if ($self->{'enable_cache'}) {
917 $optionally_run_general_cmd
918 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
919 }
920
921
922 my $streaming_bitrate = $self->{'streamingbitrate'};
923 my $streaming_size = $self->{'streamingsize'};
924
925 my $streaming_quality = "high";
926
927 my ($stream_cmd,$oflash_filename,$oflash_file)
928 = $self->stream_flv_video_cmd($originalfilename || $filename,
929 $video_width,$video_height,
930 $streaming_quality,
931 $streaming_bitrate, $streaming_size);
932
933
934 my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
935 'message_prefix' => "Stream",
936 'message' => "Generating streamable video: $oflash_file" };
937
938
939 my ($streamable_regenerated,$streamable_result,$streamable_had_error)
940 = $self->$optionally_run_general_cmd($stream_cmd,$oflash_filename,$streamable_options);
941
942 if (!$streamable_had_error) {
943 my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($oflash_filename);
944
945 my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
946 'message_prefix' => "Stream Seekable",
947 'message' => "Reprocessing video stream to be seekable by timeline: $oflash_file" };
948
949 if ($streamable_regenerated) {
950 $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
951 }
952
953 my $streamable_url = $oflash_file;
954 my $streamable_url_safe = $self->url_safe($streamable_url);
955
956 $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
957 $doc_obj->associate_file($oflash_filename,$oflash_file,"video/flash",
958 $section);
959 }
960
961
962 #
963 # FlowPlayer.swf height+22 pixels
964 # FlowPlayerBlack.swf height+16 pixels
965 # FlowPlayerThermo.swf height+16 pixels
966 # FlowPlayerWhite.swf height+26 pixels
967
968 my $flashwidth = $video_width;
969 my $flashheight = $video_height + 22;
970
971 if ($self->{'extract_keyframes'}) {
972 $flashheight += 100;
973 }
974
975 $doc_obj->add_metadata ($section, "flashwidth", $flashwidth);
976 $doc_obj->add_metadata ($section, "flashheight", $flashheight);
977
978 $self->{'oflash_file'} = $oflash_file;
979 $self->{'oflash_filename'} = $oflash_filename;
980
981 return $streamable_regenerated;
982}
983
984sub enable_h264_streaming
985{
986 my $self = shift (@_);
987 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
988 $video_width,$video_height) = @_;
989
990
991 my $section = $doc_obj->get_top_section();
992
993 my $output_dir = $self->{'cached_dir'};
994 my $ivideo_root = $self->{'cached_file_root'};
995
996 # Generate the H264 video file format for streaming purposes using the cache feature if used
997
998 my $optionally_run_general_cmd = "run_uncached_general_cmd";
999 if ($self->{'enable_cache'}) {
1000 $optionally_run_general_cmd
1001 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
1002 }
1003
1004
1005 ## my $streaming_bitrate = $self->{'streamingbitrate'};
1006 ## my $streaming_size = $self->{'streamingsize'};
1007 my $streaming_HQ_size = $self->{'streamingHQsize'};
1008 my $streaming_HQ_VideoBitrate = $self->{'streamingHQVideoBitrate'};
1009 my $streaming_HQ_AudioBitrate = $self->{'streamingHQAudioBitrate'};
1010
1011 ## my $streaming_quality = "high";
1012
1013 my ($stream_cmd,$oflash_filename,$oflash_file)
1014 = $self->stream_mp4_video_cmd($originalfilename || $filename,
1015 $streaming_HQ_size,
1016 $streaming_HQ_VideoBitrate,
1017 $streaming_HQ_AudioBitrate);
1018
1019
1020 my $streamable_options = { @{$self->{'handbrake_monitor'}},
1021 'message_prefix' => "MP4 Stream",
1022 'message' => "Generating streamable MP4 video: $oflash_file" };
1023
1024
1025 my ($streamable_regenerated,$streamable_result,$streamable_had_error)
1026 = $self->$optionally_run_general_cmd($stream_cmd,$oflash_filename,$streamable_options);
1027
1028 if (!$streamable_had_error) {
1029# my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($oflash_filename);
1030
1031# my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
1032# 'message_prefix' => "Stream Seekable",
1033# 'message' => "Reprocessing video stream to be seekable by timeline: $oflash_file" };
1034
1035# if ($streamable_regenerated) {
1036# $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
1037# }
1038
1039 my $streamable_url = $oflash_file;
1040 my $streamable_url_safe = $self->url_safe($streamable_url);
1041
1042 #add the metadata for the resulting file that should be open inside the flash video player
1043 $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
1044 $doc_obj->associate_file($oflash_filename,"VideoStream.mp4","video/mp4",
1045 $section);
1046 }
1047
1048
1049 #
1050 # FlowPlayer.swf height+22 pixels
1051 # FlowPlayerBlack.swf height+16 pixels
1052 # FlowPlayerThermo.swf height+16 pixels
1053 # FlowPlayerWhite.swf height+26 pixels
1054
1055 my $flashwidth = $video_width;
1056 my $flashheight = $video_height + 22;
1057
1058 if ($self->{'extract_keyframes'}) {
1059 $flashheight += 100;
1060 }
1061
1062 $doc_obj->add_metadata ($section, "flashwidth", $flashwidth);
1063 $doc_obj->add_metadata ($section, "flashheight", $flashheight);
1064
1065 $self->{'oflash_file'} = $oflash_file;
1066 $self->{'oflash_filename'} = $oflash_filename;
1067
1068 return $streamable_regenerated;
1069
1070
1071}
1072
1073sub enable_full_streaming
1074{
1075 my $self = shift (@_);
1076 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
1077 $video_width,$video_height) = @_;
1078
1079 my $video_streamable_regenerated
1080 = $self->enable_video_streaming($doc_obj,$originalfilename,$filename,
1081 $convertto_regenerated,
1082 $video_width,$video_height);
1083
1084 my $audio_streamable_regenerated
1085 = $self->enable_audio_streaming($doc_obj,$originalfilename,$filename,
1086 $convertto_regenerated);
1087
1088 return ($video_streamable_regenerated || $audio_streamable_regenerated);
1089}
1090
1091
1092
1093sub flvtool2_monitor_line
1094{
1095 my ($line) = @_;
1096
1097 my $had_error = 0;
1098 my $generate_dot = 1;
1099
1100 if ($line =~ m/\s+\- /) {
1101 # ignore tabulated output printed at end of command
1102 $generate_dot = 0;
1103 }
1104
1105 if ($line =~ m/^Error:/i) {
1106 $had_error = 1;
1107 }
1108
1109 return ($had_error,$generate_dot);
1110}
1111
1112
1113
1114
1115
11161;
Note: See TracBrowser for help on using the repository browser.