source: gs2-extensions/video-and-audio/trunk/src/perllib/plugins/VideoConverter.pm@ 23407

Last change on this file since 23407 was 23407, checked in by max, 13 years ago

Switched to using a hash map to transfer video level metadata between 'indentify' type call rather than an array/tuple.

Now we also deinterlace the video when extracting the thumbnail.

File size: 43.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' => "keep_original_video",
50 'desc' => "Copy the original video file in the collection as an associated file.",
51 'type' => "enum",
52 'list' => [{'name' => "true", 'desc' => "Include the original video file in the collection"},
53 {'name' => "false", 'desc' => "Do not copy the original video file in the collection"}],
54 'deft' => "false",
55 'reqd' => "no" },
56 { 'name' => "thumbnailsize",
57 'desc' => "{ImageConverter.thumbnailsize}",
58 'type' => "int",
59 'deft' => "100",
60 'range' => "1,",
61 'reqd' => "no" },
62 { 'name' => "thumbnailtype",
63 'desc' => "{ImageConverter.thumbnailtype}",
64 'type' => "string",
65 'deft' => "jpg",
66 'reqd' => "no" },
67 { 'name' => "noscaleup",
68 'desc' => "{ImageConverter.noscaleup}",
69 'type' => "flag",
70 'reqd' => "no" } );
71
72
73my @screenview_arguments = (
74 { 'name' => "create_screenview",
75 'desc' => "{ImageConverter.create_screenview}",
76 'type' => "enum",
77 'list' => [{'name' => "true", 'desc' => "{common.true}"},
78 {'name' => "false", 'desc' => "{common.false}"}],
79 'deft' => "true",
80 'reqd' => "no" },
81 { 'name' => "screenviewsize",
82 'desc' => "{ImageConverter.screenviewsize}",
83 'type' => "int",
84 'deft' => "720",
85 'range' => "1,",
86 'reqd' => "no" },
87 { 'name' => "screenviewtype",
88 'desc' => "{ImageConverter.screenviewtype}",
89 'type' => "string",
90 'deft' => "jpg",
91 'reqd' => "no" } );
92
93my $arguments = [
94 @thumb_arguments,
95 @screenview_arguments,
96
97 { 'name' => "converttotype",
98 'desc' => "{ImageConverter.converttotype}",
99 'type' => "string",
100 'deft' => "",
101 'reqd' => "no" },
102
103
104 { 'name' => "converttosize",
105 'desc' => "{VideoPlugin.converttosize}",
106 'type' => "int",
107 'deft' => "",
108## 'deft' => "352",
109 'reqd' => "no" },
110 { 'name' => "converttobitrate",
111 'desc' => "{VideoPlugin.converttobitrate}",
112 'type' => "string",
113 'deft' => "200k",
114 'reqd' => "no" },
115 { 'name' => "extract_keyframes",
116 'desc' => "{VideoPlugin.extractkeyframes}",
117 'type' => "flag",
118 'deft' => "0",
119 'reqd' => "no" },
120 { 'name' => "keep_keyframes",
121 'desc' => "{VideoPlugin.keep_keyframes}",
122 'type' => "string",
123 'deft' => "all",
124 'reqd' => "no" },
125 { 'name' => "streamingsize",
126 'desc' => "{VideoPlugin.streamingsize}",
127 'type' => "int",
128 'deft' => "352",
129 'reqd' => "no" },
130 { 'name' => "streamingbitrate",
131 'desc' => "{VideoPlugin.streamingbitrate}",
132 'type' => "string",
133 'deft' => "200k",
134 'reqd' => "no" },
135 { 'name' => "streamingHQsize",
136 'desc' => "{VideoPlugin.streamingsize}",
137 'type' => "int",
138 'deft' => "720",
139 'reqd' => "no" },
140 { 'name' => "streamingHQVideoBitrate",
141 'desc' => "{VideoPlugin.streamingbitrate}",
142 'type' => "int",
143 'deft' => "496",
144 'reqd' => "no" },
145 { 'name' => "streamingHQAudioBitrate",
146 'desc' => "{VideoPlugin.streamingbitrate}",
147 'type' => "int",
148 'deft' => "80",
149 'reqd' => "no" },
150 { 'name' => "videoDeinterlacingFilter",
151 'desc' => "Activate a deinterlacing filter to increase the quality of TV footage",
152 'type' => "enum",
153 'list' => [{'name' => "true", 'desc' => "{common.true}"},
154 {'name' => "false", 'desc' => "{common.false}"}],
155 'deft' => "false",
156 'reqd' => "no" },
157 { 'name' => "getMaximumMetadata",
158 'desc' => "Extract the maximum technical information of each video",
159 'type' => "enum",
160 'list' => [{'name' => "true", 'desc' => "{common.true}"},
161 {'name' => "false", 'desc' => "{common.false}"}],
162 'deft' => "false",
163 'reqd' => "no" },
164
165 { 'name' => "minimumsize",
166 'desc' => "{ImageConverter.minimumsize}",
167 'type' => "int",
168 'deft' => "100",
169 'range' => "1,",
170 'reqd' => "no" },
171
172 ];
173
174my $options = { 'name' => "VideoConverter",
175 'desc' => "{VideoConverter.desc}",
176 'abstract' => "yes",
177 'inherits' => "yes",
178 'args' => $arguments };
179
180sub new {
181 my ($class) = shift (@_);
182 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
183 push(@$pluginlist, $class);
184
185 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
186 push(@{$hashArgOptLists->{"OptList"}},$options);
187
188 my $self = new MultimediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
189
190
191 return bless $self, $class;
192
193}
194
195
196# Here we use 'MediaInfo' for video and ask for a specific specification
197sub mediaInfo_Inform_Cmd {
198 my ($video, $parameter, $outhandle, $verbosity) = @_;
199
200 # Use MediaInfo CLI to get the file spec according to the requested parameter
201 my $command = "mediainfo --Inform=\"$parameter\" \"$video\"";
202
203 print $outhandle " $command\n" if ($verbosity > 2);
204 my $result = '';
205 $result = `$command 2>&1`;
206 chomp $result;
207 print $outhandle " $result\n" if ($verbosity > 4);
208
209 # Return the asked spec
210 return ($result);
211}
212
213# Here we use mediaInfo to get all the possible metadata in XML form that could then be parsed
214# the result will vary depending on the file type and how it was encoded
215sub mediaInfo_XML_Cmd {
216 my ($video, $outhandle, $verbosity) = @_;
217
218 # Use MediaInfo CLI to get the file spec according to the requested parameter
219 my $command = "mediainfo --Output=XML \"$video\"";
220
221 print $outhandle " $command\n" if ($verbosity > 2);
222 my $result = '';
223 $result = `$command 2>&1`;
224 print $outhandle " $result\n" if ($verbosity > 4);
225
226 # Return the asked spec
227 return ($result);
228}
229
230# Here we parse the video specs contained in the XML text in order to create a metadata entry for each field
231# that mediainfo is giving back, this can vary from media file to media file.
232sub mediaInfo_parse_XML {
233 my ($xmlTxt, $outhandle, $verbosity) = @_;
234
235 my @parts = split(/<track\s+type=\"(.*?)\">/is,$xmlTxt);
236
237 shift @parts; # Skip preamble
238 my $metadata_table = {};
239
240 while (@parts) {
241 my $type = shift(@parts);
242 my $vals = shift(@parts);
243 my @fieldlist=();
244 while ($vals =~ s/<(.*?)>(.*?)<\/\1>//) {
245 my $metaname = $1;
246 my $metaval = $2;
247 my $fullmetaname = "mi.$type\^$metaname";
248 $metadata_table->{$fullmetaname}= $metaval;
249 push(@fieldlist,$fullmetaname);
250 }
251
252 $metadata_table->{"mi.${type}Fields"}= join(",",@fieldlist);
253 }
254
255 return $metadata_table;
256
257 while (@parts) {
258 my $type = shift(@parts);
259 my $vals = shift(@parts);
260 my @fieldlist=();
261 while ($vals =~ s/<(.*?)>(.*?)<\/\1>//) {
262 my $metaname = $1;
263 my $metaval = $2;
264 my $fullmetaname = "mi.$type\^$metaname";
265 if($metaname ne "Complete_name") #ignore this field as it is pointing to the cache directory
266 {
267 $metadata_table->{$fullmetaname}= $metaval;
268 push(@fieldlist,$fullmetaname);
269 }
270 }
271 $metadata_table->{"mi.${type}Fields"}= join(",",@fieldlist);
272 }
273 return $metadata_table;
274
275}
276
277sub identifyMI {
278 my ($video, $outhandle, $verbosity) = @_;
279
280 # Use the MediaInfo command to get the file specs
281 my $command = "MediaInfo \"$video\"";
282
283 print $outhandle " $command\n" if ($verbosity > 2);
284 my $result = '';
285 $result = `$command 2>&1`;
286 print $outhandle " $result\n" if ($verbosity > 4);
287
288 # Read the type, width, and height etc. from media file
289 # This could be done in a more efficient way by only calling the application MediaInfo once and
290 # giving all the parameters inside a special macro file then parsing the results, however
291 # on most operating system when the application is called once it is then cached for subsequent calls.
292 # General info
293 my $vtype = mediaInfo_Inform_Cmd($video,"General;%Format%",$outhandle,0); #Container of the video e.g. MPEG-TS
294 my $duration = mediaInfo_Inform_Cmd($video,"General;%Duration%",$outhandle,0); #Duration of the video in ms e.g. 5545841
295 my $durationDisplay = mediaInfo_Inform_Cmd($video,"General;%Duration/String%",$outhandle,0); #Duration of the video in minutes and secs or hours and minutes e.g. 25m 12s or 2h 26m
296 my $filesize = mediaInfo_Inform_Cmd($video,"General;%FileSize%",$outhandle,0); #File size in bytes e.g. 1024
297 my $filesizeDisplay = mediaInfo_Inform_Cmd($video,"General;%FileSize/String%",$outhandle,0); #File size with measure e.g. 25.6 MiB
298 my $orateDisplay = mediaInfo_Inform_Cmd($video,"General;%OverallBitRate/String%",$outhandle,0); #General file bitrate with measure e.g. 7 538 Kbps
299 # Video info
300 my $vcodec = mediaInfo_Inform_Cmd($video,"Video;%Format%",$outhandle,0); #Codec used for the video compression e.g. AVC
301 my $vrate = mediaInfo_Inform_Cmd($video,"Video;%BitRate%",$outhandle,0); #Bitrate used for the video stream in bps e.g. 546165
302 my $width = mediaInfo_Inform_Cmd($video,"Video;%Width%",$outhandle,0); #Width of the video in pixels e.g. 1920
303 my $height = mediaInfo_Inform_Cmd($video,"Video;%Height%",$outhandle,0); #Height of the video in pixels e.g. 1080
304 my $fps = mediaInfo_Inform_Cmd($video,"Video;%FrameRate%",$outhandle,0); #Video frames per second e.g. 30
305 # Audio info
306 my $acodec = mediaInfo_Inform_Cmd($video,"Audio;%Format%",$outhandle,0); # Audio codec used for the compression e.g. AAC
307 my $afreq = mediaInfo_Inform_Cmd($video,"Audio;%SamplingRate/String%",$outhandle,0); #Sampling rate used for audio encoding e.g. 48000
308 my $achan = mediaInfo_Inform_Cmd($video,"Audio;%Channel(s)%",$outhandle,0); #Number of channels e.g. 2
309 my $arate = mediaInfo_Inform_Cmd($video,"Audio;%BitRate%",$outhandle,0); #Audio bitrate for the audio stream in bps e.g. 65436
310 my $atracks = mediaInfo_Inform_Cmd($video,"Audio;%StreamCount%",$outhandle,0); #number of audio tracks in this video file e.g. 1
311 # Subtitles info
312 my $stype = mediaInfo_Inform_Cmd($video,"Text;%Format%",$outhandle,0); # Format used for the subtitles e.g. DVB Subtitle
313 my $slang = mediaInfo_Inform_Cmd($video,"Text;%Language/String%",$outhandle,0); # Language used for the subtitles e.g. English
314
315
316 my $xmlTxt = mediaInfo_XML_Cmd ($video,$outhandle,0);
317 my $metadata_table = mediaInfo_parse_XML($xmlTxt,$outhandle,0);
318
319 # Return the specs
320 my $identify_vals = { "vtype" => $vtype, "width" => $width, "height" => $height, "duration" => $duration, "filesize" => $filesize, "vcodec" => $vcodec,
321 "fps" => $fps, "acodec" => $acodec, "afreq" => $afreq, "achan" => $achan, "arate" => $arate, "metadata_table" => $metadata_table, "durationDisplay" => $durationDisplay,
322 "filesizeDisplay" => $filesizeDisplay, "orateDisplay" => $orateDisplay, "vrate" => $vrate, "stype" => $stype, "slang" => $slang, "atracks" => $atracks };
323
324 return $identify_vals;
325}
326
327# Here we use 'ffmpeg' for video and ask for a specific specification
328# This is now deprecated this will be replaced by identifyMI in the future
329sub identify {
330 my ($video, $outhandle, $verbosity) = @_;
331
332 # Use the ffmpeg command to get the file specs
333 my $command = "ffmpeg -i \"$video\"";
334
335 print $outhandle " $command\n" if ($verbosity > 2);
336 my $result = '';
337 $result = `$command 2>&1`;
338 print $outhandle " $result\n" if ($verbosity > 4);
339
340 # Read the type, width, and height etc.
341 my $vtype = 'unknown';
342 my $vcodec = 'unknown';
343 my $width = 'unknown';
344 my $height = 'unknown';
345 my $fps = 'unknown';
346
347 my $atype = 'unknown';
348 my $afreq = 'unknown';
349 my $achan = 'unknown';
350 my $arate = 'unknown';
351
352 my $video_safe = quotemeta $video;
353
354 # strip off everything up to filename
355 $result =~ s/^.*\'$video_safe\'://s;
356
357## if ($result =~ m/Video: (.*?) fps/m) {
358 if ($result =~ m/^\s+Stream.*?Video: (.*?)$/m) {
359 my $video_info = $1;
360 $video_info =~ s/\s*\[.*?\]//g;
361
362 my @video_fields = split(/,\s*/,$video_info);
363
364 $vtype = shift @video_fields;
365
366 if ($video_fields[0] !~ m/(\d+)x(\d+)/) {
367 $vcodec = shift @video_fields;
368 }
369 my $video_dim = shift @video_fields;
370
371 ($width,$height) = ($video_dim =~ m/(\d+)x(\d+)/);
372
373 if ($video_fields[0] =~ m/(\d+)\s+tbr$/) {
374 $fps = $1;
375 }
376
377# if ($video_info =~ m/([^,]+),(?: ([^,]+),)? (\d+)x(\d+),.*?(\d+\.\d+)/)
378# {
379# $vtype = $1;
380# $vcodec = $2 if defined $2;
381# $width = $3;
382# $height = $4;
383# $fps = $5;
384# }
385 }
386
387 if ($result =~ m/Audio: (\w+), (\d+) Hz, (\w+)(?:, (\d+.*))?/m) {
388 $atype = $1;
389 $afreq = $2;
390 $achan = $3;
391 $arate = $4 if (defined $4);
392 }
393
394 # Read the duration
395 my $duration = "unknown";
396 if ($result =~ m/Duration: (\d+:\d+:\d+\.\d+)/m) {
397 $duration = $1;
398 }
399 print $outhandle " file: $video:\t $vtype, $width, $height, $duration\n"
400 if ($verbosity > 2);
401
402 if ($verbosity >3) {
403 print $outhandle "\t video codec=$vcodec, fps = $fps\n";
404 print $outhandle "\t audio codec=$atype, freq = $afreq Hz, $achan, $arate\n";
405 }
406
407 # Return the specs
408 return ($vtype, $width, $height, $duration, -s $video,
409 $vcodec,$fps,$atype,$afreq,$achan,$arate);
410}
411
412
413sub vob_durations
414{
415 my ($media_base_dir,$title_num,$outhandle) = @_;
416
417 my $filter_re = sprintf("^VTS_%02d_[1-9]\\.VOB\$",$title_num);
418
419 my $duration_info = {};
420
421 if (opendir(VTSIN,$media_base_dir)) {
422 my @vts_title_vobs = grep { $_ =~ m/$filter_re/ } readdir(VTSIN);
423 closedir(VTSIN);
424
425 foreach my $v (@vts_title_vobs) {
426 my $full_v = &util::filename_cat($media_base_dir,$v);
427
428 my ($vtype, $width, $height, $duration, $vsize,
429 $vcodec ,$fps ,$atype ,$afreq ,$achan ,$arate) = identify($full_v,$outhandle,0);
430
431 my ($vob_num) = ($v =~ m/^VTS_\d\d_(\d)\.VOB$/);
432
433 $duration_info->{$vob_num} = $duration;
434 print STDERR "**** $title_num: $title_num, storing {$vob_num} => $duration\n";
435
436 }
437
438 }
439 else {
440 print $outhandle "Warning: unable to read files in directory $media_base_dir\n";
441 }
442
443 return $duration_info;
444
445}
446
447
448
449sub init_cache_for_file {
450 my $self = shift(@_);
451
452 my ($video_filename) = @_;
453
454 $self->SUPER::init_cache_for_file($video_filename);
455
456
457 my @flvtool2_monitor = ( 'monitor_init' ,"convertutil::monitor_init_unbuffered",
458 'monitor_line' , "VideoConverter::flvtool2_monitor_line",
459 'monitor_deinit' , "convertutil::monitor_deinit_unbuffered" );
460
461 $self->{'flvtool2_monitor'} = \@flvtool2_monitor;
462
463
464}
465
466
467
468sub optional_frame_scale
469{
470 my $self = shift (@_);
471 my ($orig_size,$video_width,$video_height) = @_;
472
473 my $s_opt = "";
474
475 if ($video_width > $video_height) {
476 if ($video_width > $orig_size) {
477 my $scale_factor = $orig_size/$video_width;
478 my $scaled_width = int($video_width * $scale_factor);
479 my $scaled_height = int($video_height * $scale_factor);
480
481 # round to be ensure multiple of 2 (needed by some codecs)
482 $scaled_width = int($scaled_width/2)*2;
483 $scaled_height = int($scaled_height/2)*2;
484
485 $s_opt = "-s ${scaled_width}x${scaled_height}";
486 }
487 # else, video is smaller than requested size, don't scale up
488 }
489 else {
490 if ($video_height > $orig_size) {
491 my $scale_factor = $orig_size/$video_height;
492 my $scaled_width = int($video_width * $scale_factor);
493 my $scaled_height = int($video_height * $scale_factor);
494
495 # round to be ensure multiple of 2 (needed by some codecs)
496 $scaled_width = int($scaled_width/2)*2;
497 $scaled_height = int($scaled_height/2)*2;
498
499 $s_opt = "-s ${scaled_width}x${scaled_height}";
500 }
501 # else, video is smaller than requested size, don't scale up
502
503 }
504
505 return $s_opt;
506}
507
508
509sub keyframe_cmd
510{
511 my $self = shift (@_);
512 my ($ivideo_filename) = @_;
513
514 my $video_ext_dir = &util::filename_cat($ENV{'GSDLHOME'},"ext","video");
515
516 my $output_dir = $self->{'cached_dir'};
517 my $ivideo_root = $self->{'cached_file_root'};
518
519 my $oshot_filename = &util::filename_cat($output_dir,"shots.xml");
520
521 my $exp_duration = $self->{'exp_duration'};
522 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
523
524 my $main_opts = "-y $t_opt";
525
526 my $hive = &util::filename_cat($video_ext_dir,"lib","vhook","hive.so");
527
528 my $oflash_filename = &util::filename_cat($output_dir,"$ivideo_root\_keyframe.flv");
529
530 my $vhook_opts = "$hive -o $oshot_filename -k $output_dir $ivideo_filename";
531
532 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
533 my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);
534
535 my $ffmpeg_cmd = "ffkeyframe $main_opts -vhook \"$vhook_opts\" -i \"$ivideo_filename_gsdlenv\" -an -y \"$oflash_filename_gsdlenv\"";
536
537
538 return ($ffmpeg_cmd,$oflash_filename);
539}
540
541sub get_ovideo_file
542{
543 my $self = shift (@_);
544 my ($stream_type) = @_;
545
546 my $ivideo_root = $self->{'cached_file_root'};
547 my $ofile;
548
549 if ($stream_type eq "flv")
550 {
551 $ofile = "${ivideo_root}_vstream.flv";
552 }
553 else
554 {
555 $ofile = "${ivideo_root}_vHQstream.mp4";
556 }
557
558 return $ofile;
559}
560
561sub get_ovideo_filename
562{
563 my $self = shift (@_);
564 my ($stream_type) = @_;
565
566 my $output_dir = $self->{'cached_dir'};
567
568 my $ofile = $self->get_ovideo_file($stream_type);
569
570 my $ofilename = &util::filename_cat($output_dir,$ofile);
571
572 return $ofilename;
573}
574
575# Generate the command to use with Handbrake to recompress a video using h264/aac compatible with Flash Player 10
576sub stream_mp4_video_cmd
577{
578 my $self = shift (@_);
579 my ($ivideo_filename,$streaming_HQ_size, $streaming_HQ_VideoBitrate,
580 $streaming_HQ_AudioBitrate) = @_;
581
582 my $output_dir = $self->{'cached_dir'};
583 my $ivideo_root = $self->{'cached_file_root'};
584
585 my $omp4_file = "${ivideo_root}_vHQstream.mp4";
586 my $omp4_filename = &util::filename_cat($output_dir,$omp4_file);
587
588
589 #my $exp_duration = $self->{'exp_duration'};
590 #my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
591
592 my $deinterlace = $self->{'videoDeinterlacingFilter'};
593 my $video_processing_parameters;
594 # Use specific resizing algorythms depending if we need to downsize the video resolution or not
595 if(!$streaming_HQ_size || $streaming_HQ_size eq "fullsize") {
596 $video_processing_parameters = "--strict-anamorphic";
597 } else {
598 $video_processing_parameters = "-w $streaming_HQ_size --loose-anamorphic";
599 }
600 # Use the deinterlace video filter if enabled in the plugin
601 if ($deinterlace eq "true")
602 {
603 $video_processing_parameters .= " --decomb";
604 }
605
606 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
607 my $omp4_filename_gsdlenv = $self->gsdlhome_independent($omp4_filename);
608
609 # Generate the pre and post video parameters for Handbrake v0.9.4
610 my $pre_opts = "-t 1 -c 1";
611 my $post_opts = "-f mp4 -O $video_processing_parameters -e x264 -b $streaming_HQ_VideoBitrate -a 1 -E faac -6 dpl2 -R Auto -B $streaming_HQ_AudioBitrate -D 0.0 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0";
612
613 my $handbrake_cmd = "HandbrakeCLI.exe -i \"$ivideo_filename_gsdlenv\" $pre_opts -o \"$omp4_filename_gsdlenv\" $post_opts";
614
615 #print STDERR "****\nHandbrake command: $handbrake_cmd\n****\n";
616
617 return ($handbrake_cmd,$omp4_filename,$omp4_file);
618}
619
620
621
622sub stream_flv_video_cmd
623{
624 my $self = shift (@_);
625 my ($ivideo_filename,$video_width,$video_height,
626 $streaming_quality,
627 $streaming_bitrate,$streaming_size,
628 $opt_streaming_achan, $opt_streaming_arate) = @_;
629
630 my $streaming_achan
631 = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;
632
633 my $streaming_arate
634 = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;
635
636 my $output_dir = $self->{'cached_dir'};
637 my $ivideo_root = $self->{'cached_file_root'};
638
639 my $oflash_file = "${ivideo_root}_vstream.flv";
640 my $oflash_filename = &util::filename_cat($output_dir,$oflash_file);
641
642 my $s_opt = "";
643 my $media_type = $self->{'media_type'};
644 if ($media_type ne "audio") {
645 $s_opt = $self->optional_frame_scale($streaming_size,$video_width,$video_height);
646 }
647
648
649 my $exp_duration = $self->{'exp_duration'};
650 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
651
652 my $main_opts = "-y $t_opt";
653
654 my $bitrate_opt = "-b $streaming_bitrate";
655 ### my $stream_opts = "-r 25 $s_opt";
656 my $stream_opts .= " $s_opt -ac $streaming_achan -ar $streaming_arate";
657
658 # -flags +ilme+ildct' and maybe '-flags +alt' for interlaced material, and try '-top 0/1'
659
660 my $all_opts = "$main_opts $stream_opts";
661
662 my $ffmpeg_cmd;
663
664 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
665 my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename);
666
667 if ($streaming_quality eq "high") {
668
669 my $pass_log_file = &util::filename_cat($output_dir,"$ivideo_root-logpass.txt");
670 if (-e $pass_log_file) {
671 &util::rm($pass_log_file);
672 }
673
674 my $pass_log_file_gsdlenv = $self->gsdlhome_independent($pass_log_file);
675
676 $all_opts .= " -passlogfile \"$pass_log_file_gsdlenv\"";
677
678 my $ffmpeg_cmd_pass1 = "ffmpeg -pass 1 -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
679
680 my $ffmpeg_cmd_pass2 = "ffmpeg -pass 2 -i \"$ivideo_filename_gsdlenv\" $all_opts $bitrate_opt -y \"$oflash_filename_gsdlenv\"";
681 $ffmpeg_cmd = "( $ffmpeg_cmd_pass1 && $ffmpeg_cmd_pass2 )";
682 }
683 else {
684 # single pass
685
686 $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\"";
687 }
688
689 return ($ffmpeg_cmd,$oflash_filename,$oflash_file);
690}
691
692
693
694
695sub stream_flv_audio_cmd
696{
697 # AudioConverter also has a routine for doing this
698 # => merge into one!
699 # **************
700
701 my $self = shift (@_);
702 my ($ivideo_filename, $opt_streaming_achan, $opt_streaming_arate) = @_;
703
704 # Convert either audio or video to streamable audio format
705
706 my $streaming_achan
707 = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2;
708
709 my $streaming_arate
710 = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050;
711
712 my $output_dir = $self->{'cached_dir'};
713 my $ivideo_root = $self->{'cached_file_root'};
714
715 my $ofla_file = "${ivideo_root}_astream.flv";
716 my $ofla_filename = &util::filename_cat($output_dir,$ofla_file);
717
718 my $exp_duration = $self->{'exp_duration'};
719 my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : "";
720
721 my $main_opts = "-vn -y $t_opt";
722
723 my $stream_opts .= " -ac $streaming_achan -ar $streaming_arate";
724
725 my $all_opts = "$main_opts $stream_opts";
726
727 my $ffmpeg_cmd;
728
729 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
730 my $ofla_filename_gsdlenv = $self->gsdlhome_independent($ofla_filename);
731 $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$ofla_filename_gsdlenv\"";
732
733 return ($ffmpeg_cmd,$ofla_filename,$ofla_file);
734}
735
736
737
738
739sub audio_excerpt_cmd
740{
741 my $self = shift (@_);
742 my ($ivoa_filename,$hh,$mm,$ss,$opt_excerpt_len) = @_;
743
744 # ivoa = input video or audio
745
746 my $time_encoded = "$hh:$mm:$ss";
747 my $time_encoded_file = "$hh$mm$ss";
748
749
750 my $output_dir = $self->{'cached_dir'};
751 my $ivoa_root = $self->{'cached_file_root'};
752
753 my $omp3_file = "${ivoa_root}_$time_encoded_file.mp3";
754 my $omp3_filename = &util::filename_cat($output_dir,$omp3_file);
755
756 my $all_opts = "-y -acodec mp3 -ss $time_encoded ";
757
758 if (defined $opt_excerpt_len) {
759 $all_opts .= "-t $opt_excerpt_len ";
760 }
761
762
763 my $ivoa_filename_gsdlenv = $self->gsdlhome_independent($ivoa_filename);
764 my $omp3_filename_gsdlenv = $self->gsdlhome_independent($omp3_filename);
765
766
767 my $ffmpeg_cmd = "ffmpeg -i \"$ivoa_filename_gsdlenv\" $all_opts \"$omp3_filename_gsdlenv\"";
768
769 return ($ffmpeg_cmd,$omp3_filename,$omp3_file);
770}
771
772
773
774sub streamseekable_cmd
775{
776 my $self = shift (@_);
777 my ($oflash_filename) = @_;
778
779 my $output_dir = $self->{'cached_dir'};
780 my $ivideo_root = $self->{'cached_file_root'};
781
782 ## my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
783
784 my $flvtool_cmd = "flvtool2 -vUP \"$oflash_filename\"";
785
786 return ($flvtool_cmd,$oflash_filename);
787}
788
789
790sub streamkeyframes_cmd
791{
792 my $self = shift (@_);
793 my ($oflash_filename,$doc_obj,$section) = @_;
794
795 my $assocfilepath
796 = $doc_obj->get_metadata_element($section,"assocfilepath");
797
798 my $output_dir = $self->{'cached_dir'};
799
800 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
801
802 my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" \"$oflash_filename\"";
803
804 return ($flvtool_cmd,$oflash_filename);
805}
806
807
808sub streamkeyframes_cmd_old
809{
810 my $self = shift (@_);
811 my ($oflash_filename,$doc_obj,$section) = @_;
812
813 my $assocfilepath
814 = $doc_obj->get_metadata_element($section,"assocfilepath");
815
816 my $output_dir = $self->{'cached_dir'};
817
818 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
819
820 my $video_server = $ENV{'GEXT_VIDEO_SERVER'};
821 my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};
822
823 my $collect = $ENV{'GSDLCOLLECTION'};
824
825 print STDERR "**** Warning: need to remove dependency of GEXT_VIDEO_SERVER and _PREFIX\n";
826
827 my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" -thumbLocation:$video_server$video_prefix/collect/$collect/index/assoc/$assocfilepath \"$oflash_filename\"";
828
829
830 return ($flvtool_cmd,$oflash_filename);
831}
832
833
834sub streamcuepts_cmd
835{
836 my $self = shift (@_);
837 my ($oflash_filename) = @_;
838
839 my $output_dir = $self->{'cached_dir'};
840
841 my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml");
842
843 ##my $video_server = $ENV{'GEXT_VIDEO_SERVER'};
844 ##my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'};
845
846 ## my $collect = $ENV{'GSDLCOLLECTION'};
847
848 ## my $thumbloc = "$video_server$video_prefix/collect/$collect";
849
850
851# my $flvtool_cmd = "flvtool2 -vUAtP \"$cue_filename\" -thumbLocation:$thumbloc \"$oflash_filename\"";
852
853# my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";
854
855
856
857# my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\"";
858
859
860## my $flvtool_cmd = "flvtool2 -vAt \"$cue_filename\" -UP \"$oflash_filename\" \"$output_dir/updated.flv\"";
861
862## my $flvtool_cmd = "flvtool2 -vAtU \"$cue_filename\" \"$oflash_filename\" \"$output_dir/updated.flv\"";
863
864 my $flvtool_cmd = "flvtool2 -vAtUP \"$cue_filename\" \"$oflash_filename\"";
865
866 return ($flvtool_cmd,$oflash_filename);
867}
868
869
870sub keyframe_thumbnail_cmd
871{
872 my $self = shift (@_);
873 my ($ivideo_filename,$thumbnailfile,$thumbnailwidth,$thumbnailheight) = @_;
874
875 my $output_dir = $self->{'cached_dir'};
876 my $ivideo_root = $self->{'cached_file_root'};
877
878 my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);
879
880
881 # Try for 4th keyframe, but fall back to 1st if doesn't exist
882 my $key_filename = "${key_filename_prefix}_0003.jpg";
883 $key_filename = "${key_filename_prefix}_0000.jpg" if (!-e $key_filename);
884
885 my $key_filename_gsdlenv = $self->gsdlhome_independent($key_filename);
886 my $thumbnailfile_gsdlenv = $self->gsdlhome_independent($thumbnailfile);
887
888 my $command;
889
890 if (-e $key_filename) {
891 $command = "convert -interlace plane -verbose -geometry $thumbnailwidth"
892 . "x$thumbnailheight \"$key_filename_gsdlenv\" \"$thumbnailfile_gsdlenv\"";
893 }
894 else {
895 # extract_keyframe has either not been switched on, or else had
896 # a problem when running
897 # => extract a from
898 # my $frame_rate = 1.0 / 60.0;
899
900 my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
901
902 $command = "ffmpeg -i \"$ivideo_filename_gsdlenv\" -ss 4.5 -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -deinterlace -y \"$thumbnailfile_gsdlenv\"";
903
904 # fmpeg -i input.dv -r 1 -f image2 -s 120x96 images%05d.png
905 }
906
907 return ($command,$thumbnailfile);
908}
909
910
911sub keyframe_montage_cmd
912{
913 my $self = shift (@_);
914 my ($ivideo_filename,$montagefile) = @_;
915
916 my $output_dir = $self->{'cached_dir'};
917 my $ivideo_root = $self->{'cached_file_root'};
918
919 my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root);
920
921 my $options = "-tile 10 -geometry 75x62+2+2";
922
923 my $command = "montage $options ${key_filename_prefix}_*.jpg \"$montagefile\"";
924
925 return ($command,$montagefile);
926}
927
928
929
930sub parse_shot_xml
931{
932 my ($self) = shift(@_);
933
934 my $outhandle = $self->{'outhandle'};
935 my $output_dir = $self->{'cached_dir'};
936
937 my $shots_filename = &util::filename_cat($output_dir,"shots.xml");
938
939 eval {
940 $self->{'parser'}->parsefile($shots_filename);
941 };
942
943 if ($@) {
944 print $outhandle "VideoConverter: skipping $shots_filename as not conformant to Hive shot syntax\n" if ($self->{'verbosity'} > 1);
945 print $outhandle "\n Perl Error:\n $@\n" if ($self->{'verbosity'}>2);
946 return 0;
947 }
948
949}
950
951sub associate_keyframes_old
952{
953 my ($self) = shift(@_);
954
955 my ($doc_obj,$section) = @_;
956
957 my $output_dir = $self->{'cached_dir'};
958
959 my $count = 1;
960 foreach my $kframe_file (@{$self->{'keyframe_fnames'}}) {
961
962 my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
963 $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg","image/jpeg",
964 $section);
965 $count++;
966 }
967
968 $doc_obj->add_utf8_metadata($section,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));
969
970
971 # *****
972 # $doc_obj->add_metadata ($section, "thumblist", $self->{'flowplayer_thumblist'});
973
974}
975
976sub associate_keyframes
977{
978 my ($self) = shift(@_);
979
980 my ($doc_obj,$topsection,$timeline) = @_;
981
982 my $output_dir = $self->{'cached_dir'};
983
984 my $count = 1;
985
986 foreach my $t (sort { $timeline->{$a}->{'keyframeindex'} <=> $timeline->{$b}->{'keyframeindex'} } keys %$timeline)
987 {
988 my $kframe_file = $timeline->{$t}->{'thumb'};
989 my $timestamp = $timeline->{$t}->{'timestamp'};
990
991 # create next sub-section to video "document"
992 my $endchild = $doc_obj->insert_section($doc_obj->get_end_child($topsection));
993 $doc_obj->add_utf8_metadata($endchild,"Title","Timestamp $timestamp");
994 $doc_obj->add_utf8_metadata($endchild,"FrameNum",$t);
995
996 my $kframe_filename = &util::filename_cat($output_dir,$kframe_file);
997 $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg",
998 "image/jpeg",
999 $endchild);
1000
1001 $doc_obj->add_utf8_metadata($endchild,"assockeyframe","keyframe$count.jpg");
1002
1003 $doc_obj->add_utf8_metadata($topsection,"KeyframeTimestamp",$timestamp);
1004 $doc_obj->add_utf8_metadata($topsection,"KeyframeFrameNum",$t);
1005
1006
1007
1008 $count++;
1009 }
1010
1011 #### $doc_obj->add_utf8_metadata($topsection,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}}));
1012
1013 $doc_obj->add_utf8_metadata($topsection,"NumKeyframes",scalar(keys %$timeline));
1014
1015
1016 # *****
1017 # $doc_obj->add_metadata ($topsection, "thumblist", $self->{'flowplayer_thumblist'});
1018}
1019
1020
1021
1022sub enable_audio_streaming
1023{
1024 my $self = shift (@_);
1025 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated) = @_;
1026
1027 my $section = $doc_obj->get_top_section();
1028
1029 my $output_dir = $self->{'cached_dir'};
1030 my $ivideo_root = $self->{'cached_file_root'};
1031
1032 # Generate FLV audio-only format for streaming purposes
1033
1034 my $optionally_run_general_cmd = "run_uncached_general_cmd";
1035 if ($self->{'enable_cache'}) {
1036 $optionally_run_general_cmd
1037 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
1038 }
1039
1040
1041 my $ifilename = $originalfilename || $filename;
1042
1043 my ($stream_cmd,$ofla_filename,$ofla_file)
1044 = $self->stream_flv_audio_cmd($ifilename);
1045
1046
1047 my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
1048 'message_prefix' => "Stream",
1049 'message' => "Generating streamable audio: $ofla_file" };
1050
1051
1052 my ($streamable_regenerated,$streamable_result,$streamable_had_error)
1053 = $self->$optionally_run_general_cmd($stream_cmd,
1054 $ifilename,$ofla_filename,
1055 $streamable_options);
1056
1057
1058 if (!$streamable_had_error) {
1059 my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($ofla_filename);
1060
1061 my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
1062 'message_prefix' => "Stream Seekable",
1063 'message' => "Reprocessing audio stream to be seekable by timeline: $ofla_file" };
1064
1065 if ($streamable_regenerated) {
1066 $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
1067 }
1068
1069 my $streamable_url = $ofla_file;
1070 my $streamable_url_safe = $self->url_safe($streamable_url);
1071
1072 $doc_obj->add_utf8_metadata ($section, "streamableaudio", $streamable_url_safe);
1073 $doc_obj->associate_file($ofla_filename,$ofla_file,"audio/flash",
1074 $section);
1075 }
1076
1077 $doc_obj->add_metadata ($section, "audioflashwidth", 400);
1078 $doc_obj->add_metadata ($section, "audioflashheight", 22 + 100);
1079
1080 $self->{'ofla_file'} = $ofla_file;
1081 $self->{'ofla_filename'} = $ofla_filename;
1082
1083 return $streamable_regenerated;
1084}
1085
1086
1087
1088
1089
1090sub enable_video_streaming
1091{
1092 my $self = shift (@_);
1093 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
1094 $video_width,$video_height) = @_;
1095
1096 my $section = $doc_obj->get_top_section();
1097
1098 my $output_dir = $self->{'cached_dir'};
1099 my $ivideo_root = $self->{'cached_file_root'};
1100
1101 # Generate the Flash FLV format for streaming purposes
1102
1103 my $optionally_run_general_cmd = "run_uncached_general_cmd";
1104 if ($self->{'enable_cache'}) {
1105 $optionally_run_general_cmd
1106 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
1107 }
1108
1109
1110 my $streaming_bitrate = $self->{'streamingbitrate'};
1111 my $streaming_size = $self->{'streamingsize'};
1112
1113 my $streaming_quality = "high";
1114
1115 my $ifilename = $originalfilename || $filename;
1116
1117 my ($stream_cmd,$oflash_filename,$oflash_file)
1118 = $self->stream_flv_video_cmd($ifilename,
1119 $video_width,$video_height,
1120 $streaming_quality,
1121 $streaming_bitrate, $streaming_size);
1122
1123
1124 my $streamable_options = { @{$self->{'ffmpeg_monitor'}},
1125 'message_prefix' => "Stream",
1126 'message' => "Generating streamable video: $oflash_file" };
1127
1128
1129 my ($streamable_regenerated,$streamable_result,$streamable_had_error)
1130 = $self->$optionally_run_general_cmd($stream_cmd,
1131 $ifilename,$oflash_filename,
1132 $streamable_options);
1133
1134 if (!$streamable_had_error) {
1135 my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($oflash_filename);
1136
1137 my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
1138 'message_prefix' => "Stream Seekable",
1139 'message' => "Reprocessing video stream to be seekable by timeline: $oflash_file" };
1140
1141 if ($streamable_regenerated) {
1142 $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
1143 }
1144
1145 my $streamable_url = $oflash_file;
1146 my $streamable_url_safe = $self->url_safe($streamable_url);
1147
1148 $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
1149 $doc_obj->associate_file($oflash_filename,$oflash_file,"video/flash",
1150 $section);
1151 }
1152
1153
1154 #
1155 # FlowPlayer.swf height+22 pixels
1156 # FlowPlayerBlack.swf height+16 pixels
1157 # FlowPlayerThermo.swf height+16 pixels
1158 # FlowPlayerWhite.swf height+26 pixels
1159
1160 my $flashwidth = $video_width;
1161 my $flashheight = $video_height + 22;
1162
1163 if ($self->{'extract_keyframes'}) {
1164 $flashheight += 100;
1165 }
1166
1167 $doc_obj->add_metadata ($section, "flashwidth", $flashwidth);
1168 $doc_obj->add_metadata ($section, "flashheight", $flashheight);
1169
1170 $self->{'oflash_file'} = $oflash_file;
1171 $self->{'oflash_filename'} = $oflash_filename;
1172
1173 return $streamable_regenerated;
1174}
1175
1176sub enable_direct_streaming
1177{
1178 my $self = shift (@_);
1179 my ($doc_obj,$originalfilename,$filename,$file,
1180 $video_width,$video_height) = @_;
1181
1182 my $section = $doc_obj->get_top_section();
1183
1184 my $ifilename = $originalfilename || $filename;
1185 #my $ifile = $self->gsdlhome_independent($ifilename);
1186 my $ifile = $file;
1187
1188 print STDERR "Using the original video file for direct streaming: $ifile\n";
1189
1190 my $streamable_url = $ifile;
1191 my $streamable_url_safe = $self->url_safe($streamable_url);
1192
1193 my $outhandle = $self->{'outhandle'};
1194
1195 my $identify_vals = identifyMI($ifilename,$outhandle,0);
1196
1197 #Add the most common metadata extracted by MediaInfo from the streamable video file
1198 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFormat", $identify_vals->{'vtype'});
1199 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFileSize", $identify_vals->{'filesizeDisplay'});
1200 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFileSizeBytes", $identify_vals->{'filesize'});
1201 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingWidth", $identify_vals->{'width'});
1202 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingHeight", $identify_vals->{'height'});
1203 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingDuration", $identify_vals->{'durationDisplay'});
1204 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingFPS", $identify_vals->{'fps'});
1205 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoCodec", $identify_vals->{'vcodec'});
1206 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoBitrate", $identify_vals->{'vrate'});
1207 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioCodec", $identify_vals->{'acodec'});
1208 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioBitrate", $identify_vals->{'arate'});
1209 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioTracks", $identify_vals->{'atracks'});
1210 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingOverallBitrate", $identify_vals->{'orateDisplay'});
1211 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingSubtitleType", $identify_vals->{'stype'});
1212 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingSubtitleLanguage", $identify_vals->{'slang'});
1213
1214 my $get_max_metadata = $self->{'getMaximumMetadata'};
1215 if ($get_max_metadata eq "true"){
1216 my $metadata_table = $identify_vals->{'metadata_table'};
1217 foreach my $metaname(keys %$metadata_table)
1218 {
1219 my $metaval = $identify_vals->{'metadata_table'}->{$metaname};
1220 $doc_obj->add_utf8_metadata ($section, $metaname, $metaval);
1221 }
1222 }
1223
1224 #add the metadata for the resulting file that should be open inside the flash video player
1225 $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
1226 $doc_obj->associate_file($ifilename,$ifile,"application/octet-stream",$section);
1227
1228 my $flashwidth = $video_width;
1229 my $flashheight = $video_height + 22;
1230
1231 if ($self->{'extract_keyframes'}) {
1232 $flashheight += 100;
1233 }
1234
1235 $doc_obj->add_metadata ($section, "flashwidth", $flashwidth);
1236 $doc_obj->add_metadata ($section, "flashheight", $flashheight);
1237
1238 $self->{'oflash_file'} = $ifile;
1239 $self->{'oflash_filename'} = $ifilename;
1240
1241}
1242
1243
1244sub enable_h264_streaming
1245{
1246 my $self = shift (@_);
1247 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
1248 $video_width,$video_height) = @_;
1249
1250
1251 my $section = $doc_obj->get_top_section();
1252
1253 my $output_dir = $self->{'cached_dir'};
1254 my $ivideo_root = $self->{'cached_file_root'};
1255
1256 # Generate the H264 video file format for streaming purposes using the cache feature if used
1257
1258 my $optionally_run_general_cmd = "run_uncached_general_cmd";
1259 if ($self->{'enable_cache'}) {
1260 $optionally_run_general_cmd
1261 = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd";
1262 }
1263
1264 my $streaming_HQ_size = $self->{'streamingHQsize'};
1265 my $streaming_HQ_VideoBitrate = $self->{'streamingHQVideoBitrate'};
1266 my $streaming_HQ_AudioBitrate = $self->{'streamingHQAudioBitrate'};
1267
1268 my $ifilename = $originalfilename || $filename;
1269 my ($stream_cmd,$oflash_filename,$oflash_file)
1270 = $self->stream_mp4_video_cmd($ifilename,
1271 $streaming_HQ_size,
1272 $streaming_HQ_VideoBitrate,
1273 $streaming_HQ_AudioBitrate);
1274
1275
1276 my $streamable_options = { @{$self->{'handbrake_monitor'}},
1277 'message_prefix' => "MP4 Stream",
1278 'message' => "Generating streamable MP4 video: $oflash_file" };
1279
1280
1281 my ($streamable_regenerated,$streamable_result,$streamable_had_error)
1282 = $self->$optionally_run_general_cmd($stream_cmd,
1283 $ifilename,$oflash_filename,
1284 $streamable_options);
1285
1286 if (!$streamable_had_error) {
1287
1288
1289 my $streamable_url = $oflash_file;
1290 my $streamable_url_safe = $self->url_safe($streamable_url);
1291
1292 my $outhandle = $self->{'outhandle'};
1293
1294 my $identify_vals = identifyMI($oflash_filename,$outhandle,0);
1295
1296 #Add the most common metadata extracted by MediaInfo from the streamable video file
1297 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFormat", $identify_vals->{'vtype'});
1298 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFileSize", $identify_vals->{'filesizeDisplay'});
1299 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingWidth", $identify_vals->{'width'});
1300 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingHeight", $identify_vals->{'height'});
1301 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingDuration", $identify_vals->{'durationDisplay'});
1302 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingFPS", $identify_vals->{'fps'});
1303 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoCodec", $identify_vals->{'vcodec'});
1304 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoBitrate", $identify_vals->{'vrate'});
1305 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioCodec", $identify_vals->{'acodec'});
1306 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioBitrate", $identify_vals->{'arate'});
1307 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingOverallBitrate", $identify_vals->{'orateDisplay'});
1308 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingSubtitleType", $identify_vals->{'stype'});
1309 $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingSubtitleLangue", $identify_vals->{'slang'});
1310
1311 my $get_max_metadata = $self->{'getMaximumMetadata'};
1312 if ($get_max_metadata eq "true"){
1313 my $metadata_table = $identify_vals->{'metadata_table'};
1314 foreach my $metaname(keys %$metadata_table)
1315 {
1316 my $metaval = $identify_vals->{'metadata_table'}->{$metaname};
1317 $doc_obj->add_utf8_metadata ($section, $metaname, $metaval);
1318 }
1319 }
1320
1321 #add the metadata for the resulting file that should be open inside the flash video player
1322 $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
1323 $doc_obj->associate_file($oflash_filename,$oflash_file,"video/mp4",
1324 $section);
1325 }
1326
1327 my $flashwidth = $video_width;
1328 my $flashheight = $video_height + 22;
1329
1330 if ($self->{'extract_keyframes'}) {
1331 $flashheight += 100;
1332 }
1333
1334 $doc_obj->add_metadata ($section, "flashwidth", $flashwidth);
1335 $doc_obj->add_metadata ($section, "flashheight", $flashheight);
1336
1337 $self->{'oflash_file'} = $oflash_file;
1338 $self->{'oflash_filename'} = $oflash_filename;
1339
1340 return $streamable_regenerated;
1341
1342
1343}
1344
1345sub enable_full_streaming
1346{
1347 my $self = shift (@_);
1348 my ($doc_obj,$originalfilename,$filename,$convertto_regenerated,
1349 $video_width,$video_height) = @_;
1350
1351 my $video_streamable_regenerated
1352 = $self->enable_video_streaming($doc_obj,$originalfilename,$filename,
1353 $convertto_regenerated,
1354 $video_width,$video_height);
1355
1356 my $audio_streamable_regenerated
1357 = $self->enable_audio_streaming($doc_obj,$originalfilename,$filename,
1358 $convertto_regenerated);
1359
1360 return ($video_streamable_regenerated || $audio_streamable_regenerated);
1361}
1362
1363
1364
1365sub flvtool2_monitor_line
1366{
1367 my ($line) = @_;
1368
1369 my $had_error = 0;
1370 my $generate_dot = 1;
1371
1372 if ($line =~ m/\s+\- /) {
1373 # ignore tabulated output printed at end of command
1374 $generate_dot = 0;
1375 }
1376
1377 if ($line =~ m/^Error:/i) {
1378 $had_error = 1;
1379 }
1380
1381 return ($had_error,$generate_dot);
1382}
1383
1384
1385
1386
1387
13881;
Note: See TracBrowser for help on using the repository browser.