root/extensions/gsdl-video/trunk/perllib/plugins/VideoConverter.pm @ 19828

Revision 19828, 25.5 KB (checked in by davidb, 10 years ago)

Continued development of plugins to work smoothly with Flash player

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    if ($self->{'media_type'} ne "audio") {
397    $s_opt = $self->optional_frame_scale($streaming_size,$video_width,$video_height);
398    }
399
400
401    my $exp_duration = $self->{'exp_duration'};
402    my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
403
404    my $main_opts = "-y $t_opt";
405
406    my $bitrate_opt = "-b $streaming_bitrate";
407    ### my $stream_opts = "-r 25 $s_opt";
408    my $stream_opts .= " $s_opt -ac $streaming_achan -ar $streaming_arate";
409
410    # -flags +ilme+ildct' and maybe '-flags +alt' for interlaced material, and try '-top 0/1'
411
412    my $all_opts = "$main_opts $stream_opts";
413
414    my $ffmpeg_cmd;
415
416    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
417    my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);
418
419    if ($streaming_quality eq "high") {
420
421    my $pass_log_file = &util::filename_cat($output_dir,"$ivideo_root-logpass.txt");
422    if (-e $pass_log_file) {
423        &util::rm($pass_log_file);
424    }
425
426    my $pass_log_file_gsdlenv = $self->gsdlhome_independent($pass_log_file);
427
428    $all_opts .= " -passlogfile \"$pass_log_file_gsdlenv\"";
429
430    my $ffmpeg_cmd_pass1 = "ffmpeg -pass 1 -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
431
432    my $ffmpeg_cmd_pass2 = "ffmpeg -pass 2 -i \"$ivideo_filename_gsdlenv\" $all_opts $bitrate_opt -y \"$oflash_filename_gsdlenv\"";
433    $ffmpeg_cmd = "( $ffmpeg_cmd_pass1 && $ffmpeg_cmd_pass2 )";
434    }
435    else {
436    # single pass
437
438    $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
439    }
440
441    return ($ffmpeg_cmd,$oflash_filename,$oflash_file);
442}
443
444
445
446
447sub stream_flv_audio_cmd
448{
449    # AudioConverter also has a routine for doing this
450    # => merge into one!
451    # **************
452
453    my $self = shift (@_);
454    my ($ivideo_filename, $opt_streaming_achan, $opt_streaming_arate) = @_;
455
456    # Convert either audio or video to streamable audio format
457
458    my $streaming_achan
459    = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;
460
461    my $streaming_arate
462    = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;
463
464    my $output_dir = $self->{'cached_dir'};
465    my $ivideo_root = $self->{'cached_file_root'};
466
467    my $ofla_file = "${ivideo_root}_astream.flv";
468    my $ofla_filename = &util::filename_cat($output_dir,$ofla_file);
469
470    my $exp_duration = $self->{'exp_duration'};
471    my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
472
473    my $main_opts = "-vn -y $t_opt";
474
475    my $stream_opts .= " -ac $streaming_achan -ar $streaming_arate";
476
477    my $all_opts = "$main_opts $stream_opts";
478
479    my $ffmpeg_cmd;
480
481    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
482    my $ofla_filename_gsdlenv = $self->gsdlhome_independent($ofla_filename);
483    $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$ofla_filename_gsdlenv\"";
484
485    return ($ffmpeg_cmd,$ofla_filename,$ofla_file);
486}
487
488
489
490
491sub audio_excerpt_cmd
492{
493    my $self = shift (@_);
494    my ($ivoa_filename,$hh,$mm,$ss,$opt_excerpt_len) = @_;
495
496    # ivoa = input video or audio
497
498    my $time_encoded = "$hh:$mm:$ss";
499    my $time_encoded_file = "$hh$mm$ss";
500   
501   
502    my $output_dir = $self->{'cached_dir'};
503    my $ivoa_root = $self->{'cached_file_root'};
504
505    my $omp3_file = "${ivoa_root}_$time_encoded_file.mp3";
506    my $omp3_filename = &util::filename_cat($output_dir,$omp3_file);
507
508    my $all_opts = "-y -acodec mp3 -ss $time_encoded ";
509
510    if (defined $opt_excerpt_len) {
511    $all_opts .= "-t $opt_excerpt_len ";
512    }
513
514
515    my $ivoa_filename_gsdlenv = $self->gsdlhome_independent($ivoa_filename);
516    my $omp3_filename_gsdlenv = $self->gsdlhome_independent($omp3_filename);
517
518
519    my $ffmpeg_cmd = "ffmpeg -i \"$ivoa_filename_gsdlenv\" $all_opts  \"$omp3_filename_gsdlenv\"";
520
521    return ($ffmpeg_cmd,$omp3_filename,$omp3_file);
522}
523
524
525
526sub streamseekable_cmd
527{
528    my $self = shift (@_);
529    my ($oflash_filename) = @_;
530
531    my $output_dir = $self->{'cached_dir'};
532    my $ivideo_root = $self->{'cached_file_root'};
533
534    my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
535
536    my $flvtool_cmd = "flvtool2 -vUP \"$oflash_filename\"";
537
538    return ($flvtool_cmd,$oflash_filename);
539}
540
541
542sub streamkeyframes_cmd
543{
544    my $self = shift (@_);
545    my ($oflash_filename,$doc_obj,$section) = @_;
546
547    my $assocfilepath
548    = $doc_obj->get_metadata_element($section,"assocfilepath");
549
550    my $output_dir = $self->{'cached_dir'};
551
552    my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
553
554    my $video_server = $ENV{'GEXT_VIDEO_SERVER'};
555    my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};
556
557    my $collect = $ENV{'GSDLCOLLECTION'};
558 
559    print STDERR "**** Warning: need to remove dependency of GEXT_VIDEO_SERVER and _PREFIX\n";
560
561    my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" -thumbLocation:$video_server$video_prefix/collect/$collect/index/assoc/$assocfilepath \"$oflash_filename\"";
562
563
564    return ($flvtool_cmd,$oflash_filename);
565}
566
567
568sub streamcuepts_cmd
569{
570    my $self = shift (@_);
571    my ($oflash_filename) = @_;
572
573    my $output_dir = $self->{'cached_dir'};
574
575    my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
576
577    my $video_server = $ENV{'GEXT_VIDEO_SERVER'};
578    my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};
579
580    my $collect = $ENV{'GSDLCOLLECTION'};
581
582    ## my $thumbloc = "$video_server$video_prefix/collect/$collect";
583
584
585#    my $flvtool_cmd = "flvtool2 -vUAtP \"$cue_filename\" -thumbLocation:$thumbloc \"$oflash_filename\"";
586
587#    my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";
588
589
590
591#    my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";
592
593
594##    my $flvtool_cmd = "flvtool2 -vAt \"$cue_filename\" -UP \"$oflash_filename\" \"$output_dir/updated.flv\"";
595
596##    my $flvtool_cmd = "flvtool2 -vAtU \"$cue_filename\" \"$oflash_filename\" \"$output_dir/updated.flv\"";
597
598    my $flvtool_cmd = "flvtool2 -vAtUP \"$cue_filename\" \"$oflash_filename\"";
599
600    return ($flvtool_cmd,$oflash_filename);
601}
602
603
604sub keyframe_thumbnail_cmd
605{
606    my $self = shift (@_);
607    my ($ivideo_filename,$thumbnailfile,$thumbnailwidth,$thumbnailheight) = @_;
608
609    my $output_dir = $self->{'cached_dir'};
610    my $ivideo_root = $self->{'cached_file_root'};
611
612    my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);
613
614
615    # Try for 4th keyframe, but fall back to 1st if doesn't exist
616    my $key_filename = "${key_filename_prefix}_0003.jpg";
617    $key_filename = "${key_filename_prefix}_0000.jpg" if (!-e $key_filename);
618
619    my $key_filename_gsdlenv = $self->gsdlhome_independent($key_filename);
620    my $thumbnailfile_gsdlenv = $self->gsdlhome_independent($thumbnailfile);
621
622    my $command;
623
624    if (-e $key_filename) {
625    $command = "convert -interlace plane -verbose -geometry $thumbnailwidth"
626        . "x$thumbnailheight \"$key_filename_gsdlenv\" \"$thumbnailfile_gsdlenv\"";
627    }
628    else {
629    # extractkeyframe has either not been switched on, or else had
630    # a problem when running
631    # => extract a from
632    # my $frame_rate = 1.0 / 60.0;
633   
634    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
635
636
637
638    $command = "ffmpeg -i \"$ivideo_filename_gsdlenv\"  -ss 12.5 -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -y \"$thumbnailfile_gsdlenv\"";
639
640    # fmpeg -i input.dv -r 1 -f image2 -s 120x96 images%05d.png
641    }
642
643    return ($command,$thumbnailfile);
644}
645
646
647sub keyframe_montage_cmd
648{
649    my $self = shift (@_);
650    my ($ivideo_filename,$montagefile) = @_;
651
652    my $output_dir = $self->{'cached_dir'};
653    my $ivideo_root = $self->{'cached_file_root'};
654
655    my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);
656
657    my $options = "-tile 10  -geometry 75x62+2+2";
658
659    my $command = "montage $options ${key_filename_prefix}_*.jpg \"$montagefile\"";
660
661    return ($command,$montagefile);
662}
663
664
665
666sub parse_shot_xml
667{
668    my ($self) = shift(@_);
669
670    my $outhandle = $self->{'outhandle'};
671    my $output_dir = $self->{'cached_dir'};
672
673    my $shots_filename = &util::filename_cat($output_dir,"shots.xml");
674
675    eval {
676    $self->{'parser'}->parsefile($shots_filename);
677    };
678   
679    if ($@) {
680    print $outhandle "VideoConverter: skipping $shots_filename as not conformant to Hive shot syntax\n" if ($self->{'verbosity'} > 1);
681    print $outhandle "\n Perl Error:\n $@\n" if ($self->{'verbosity'}>2);
682    return 0;
683    }
684
685}
686
687sub associate_keyframes_old
688{
689    my ($self) = shift(@_);
690
691    my ($doc_obj,$section) = @_;
692
693    my $output_dir = $self->{'cached_dir'};
694
695    my $count = 1;
696    foreach my $kframe_file (@{$self->{'keyframe_fnames'}}) {
697
698    my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
699    $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg","image/jpeg",
700                 $section);
701    $count++;
702    }
703
704    $doc_obj->add_utf8_metadata($section,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));
705
706
707    # *****
708    # $doc_obj->add_metadata ($section, "thumblist", $self->{'flowplayer_thumblist'});
709
710}
711
712sub associate_keyframes
713{
714    my ($self) = shift(@_);
715
716    my ($doc_obj,$section) = @_;
717
718    my $output_dir = $self->{'cached_dir'};
719    my $timeline = $self->{'keyframe_timeline'};
720   
721    my $count = 1;
722
723    foreach my $t (sort { $timeline->{$a}->{'keyframeindex'} <=> $timeline->{$b}->{'keyframeindex'} } keys %$timeline)
724    {
725    my $kframe_file = $timeline->{$t}->{'thumb'};
726    my $timestamp = $timeline->{$t}->{'timestamp'};
727
728    my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
729    $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg","image/jpeg",
730                 $section);
731    $doc_obj->add_utf8_metadata($section,"KeyframeTimestamp",$timestamp);
732
733    $count++;
734    }
735
736    $doc_obj->add_utf8_metadata($section,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));
737
738
739    # *****
740    # $doc_obj->add_metadata ($section, "thumblist", $self->{'flowplayer_thumblist'});
741}
742
743
744
745sub enable_audio_streaming
746{
747    my $self = shift (@_);
748    my ($doc_obj,$originalfilename,$filename,$convertto_regenerated) = @_;
749
750    my $section = $doc_obj->get_top_section();
751
752    my $output_dir   = $self->{'cached_dir'};
753    my $ivideo_root  = $self->{'cached_file_root'};
754   
755    # Generate FLV audio-only format for streaming purposes
756
757    my $optionally_run_general_cmd = "run_uncached_general_cmd";
758    if ($self->{'enable_cache'}) {
759    $optionally_run_general_cmd
760        = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
761    }
762
763   
764    my ($stream_cmd,$ofla_filename,$ofla_file)
765    = $self->stream_flv_audio_cmd($originalfilename || $filename);
766   
767   
768    my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
769                   'message_prefix' => "Stream",
770                   'message' => "Generating streamable audio: $ofla_file" };
771   
772
773    my ($streamable_regenerated,$streamable_result,$streamable_had_error)
774    = $self->$optionally_run_general_cmd($stream_cmd,$ofla_filename,
775                         $streamable_options);
776
777
778    if (!$streamable_had_error) {
779    my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($ofla_filename);
780   
781    my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
782                       'message_prefix' => "Stream Seekable",
783                       'message' => "Reprocessing audio stream to be seekable by timeline: $ofla_file" };
784   
785    if ($streamable_regenerated) {
786        $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
787    }
788   
789    my $streamable_url = $ofla_file;
790    my $streamable_url_safe = $self->url_safe($streamable_url);
791
792    $doc_obj->add_utf8_metadata ($section, "streamableaudio", $streamable_url_safe);
793    $doc_obj->associate_file($ofla_filename,$ofla_file,"audio/flash",
794                 $section);
795    }
796
797    $doc_obj->add_metadata ($section, "audioflashwidth",    400);
798    $doc_obj->add_metadata ($section, "audioflashheight",   22 + 100);
799
800    $self->{'ofla_file'} = $ofla_file;
801    $self->{'ofla_filename'} = $ofla_filename;
802
803    return $streamable_regenerated;
804}
805
806
807
808
809
810sub enable_video_streaming
811{
812    my $self = shift (@_);
813    my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
814    $video_width,$video_height) = @_;
815
816    my $section = $doc_obj->get_top_section();
817
818    my $output_dir   = $self->{'cached_dir'};
819    my $ivideo_root  = $self->{'cached_file_root'};
820   
821    # Generate the Flash FLV format for streaming purposes
822
823    my $optionally_run_general_cmd = "run_uncached_general_cmd";
824    if ($self->{'enable_cache'}) {
825    $optionally_run_general_cmd
826        = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
827    }
828
829
830    my $streaming_bitrate = $self->{'streamingbitrate'};
831    my $streaming_size    = $self->{'streamingsize'};
832   
833    my $streaming_quality = "high";
834   
835    my ($stream_cmd,$oflash_filename,$oflash_file)
836    = $self->stream_flv_video_cmd($originalfilename || $filename,
837                     $video_width,$video_height,
838                     $streaming_quality,
839                     $streaming_bitrate, $streaming_size);
840   
841   
842    my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
843                   'message_prefix' => "Stream",
844                   'message' => "Generating streamable video: $oflash_file" };
845   
846
847    my ($streamable_regenerated,$streamable_result,$streamable_had_error)
848    = $self->$optionally_run_general_cmd($stream_cmd,$oflash_filename,$streamable_options);
849   
850    if (!$streamable_had_error) {
851    my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($oflash_filename);
852   
853    my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
854                       'message_prefix' => "Stream Seekable",
855                       'message' => "Reprocessing video stream to be seekable by timeline: $oflash_file" };
856   
857    if ($streamable_regenerated) {
858        $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
859    }
860   
861    my $streamable_url = $oflash_file;
862    my $streamable_url_safe = $self->url_safe($streamable_url);
863   
864    $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
865    $doc_obj->associate_file($oflash_filename,$oflash_file,"video/flash",
866                 $section);
867    }
868
869
870    #
871    # FlowPlayer.swf       height+22 pixels
872    # FlowPlayerBlack.swf  height+16 pixels
873    # FlowPlayerThermo.swf height+16 pixels
874    # FlowPlayerWhite.swf  height+26 pixels
875
876    my $flashwidth = $video_width;
877    my $flashheight = $video_height + 22;
878
879    if ($self->{'extractkeyframes'}) {
880    $flashheight += 100;
881    }
882
883    $doc_obj->add_metadata ($section, "flashwidth",    $flashwidth);
884    $doc_obj->add_metadata ($section, "flashheight",   $flashheight);
885     
886    $self->{'oflash_file'} = $oflash_file;
887    $self->{'oflash_filename'} = $oflash_filename;
888
889    return $streamable_regenerated;
890}
891
892
893sub enable_full_streaming
894{
895    my $self = shift (@_);
896    my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
897    $video_width,$video_height) = @_;
898
899    my $video_streamable_regenerated
900    = $self->enable_video_streaming($doc_obj,$originalfilename,$filename,
901                    $convertto_regenerated,
902                    $video_width,$video_height);
903
904    my $audio_streamable_regenerated
905    = $self->enable_audio_streaming($doc_obj,$originalfilename,$filename,
906                    $convertto_regenerated);
907
908    return ($video_streamable_regenerated || $audio_streamable_regenerated);
909}
910
911
912
913sub flvtool2_monitor_line
914{
915    my ($line) = @_;
916
917    my $had_error = 0;
918    my $generate_dot = 1;
919
920    if ($line =~ m/\s+\- /) {
921    # ignore tabulated output printed at end of command
922    $generate_dot = 0;
923    }
924   
925    if ($line =~ m/^Error:/i) {
926    $had_error = 1;
927    }
928
929    return ($had_error,$generate_dot);
930}
931
932
933
934
935
9361;
Note: See TracBrowser for help on using the browser.