########################################################################### # # VideoConverter - helper plugin that does video (and audio) conversion using ffmpeg # # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright (C) 2008 New Zealand Digital Library Project # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ########################################################################### package VideoConverter; use MultimediaConverter; use strict; no strict 'refs'; # allow filehandles to be variables and viceversa use gsprintf 'gsprintf'; BEGIN { @VideoConverter::ISA = ('MultimediaConverter'); } my @thumb_arguments = ( { 'name' => "create_thumbnail", 'desc' => "{ImageConverter.create_thumbnail}", 'type' => "enum", 'list' => [{'name' => "true", 'desc' => "{common.true}"}, {'name' => "false", 'desc' => "{common.false}"}], 'deft' => "true", 'reqd' => "no" }, { 'name' => "keep_original_video", 'desc' => "Copy the original video file in the collection as an associated file.", 'type' => "enum", 'list' => [{'name' => "true", 'desc' => "Include the original video file in the collection"}, {'name' => "false", 'desc' => "Do not copy the original video file in the collection"}], 'deft' => "false", 'reqd' => "no" }, { 'name' => "thumbnailsize", 'desc' => "{ImageConverter.thumbnailsize}", 'type' => "int", 'deft' => "100", 'range' => "1,", 'reqd' => "no" }, { 'name' => "thumbnailtype", 'desc' => "{ImageConverter.thumbnailtype}", 'type' => "string", 'deft' => "jpg", 'reqd' => "no" }, { 'name' => "noscaleup", 'desc' => "{ImageConverter.noscaleup}", 'type' => "flag", 'reqd' => "no" } ); my @screenview_arguments = ( { 'name' => "create_screenview", 'desc' => "{ImageConverter.create_screenview}", 'type' => "enum", 'list' => [{'name' => "true", 'desc' => "{common.true}"}, {'name' => "false", 'desc' => "{common.false}"}], 'deft' => "true", 'reqd' => "no" }, { 'name' => "screenviewsize", 'desc' => "{ImageConverter.screenviewsize}", 'type' => "int", 'deft' => "720", 'range' => "1,", 'reqd' => "no" }, { 'name' => "screenviewtype", 'desc' => "{ImageConverter.screenviewtype}", 'type' => "string", 'deft' => "jpg", 'reqd' => "no" } ); my $arguments = [ @thumb_arguments, @screenview_arguments, { 'name' => "converttotype", 'desc' => "{ImageConverter.converttotype}", 'type' => "string", 'deft' => "", 'reqd' => "no" }, { 'name' => "converttosize", 'desc' => "{VideoPlugin.converttosize}", 'type' => "int", 'deft' => "", ## 'deft' => "352", 'reqd' => "no" }, { 'name' => "converttobitrate", 'desc' => "{VideoPlugin.converttobitrate}", 'type' => "string", 'deft' => "200k", 'reqd' => "no" }, { 'name' => "extract_keyframes", 'desc' => "{VideoPlugin.extractkeyframes}", 'type' => "flag", 'deft' => "0", 'reqd' => "no" }, { 'name' => "keep_keyframes", 'desc' => "{VideoPlugin.keep_keyframes}", 'type' => "string", 'deft' => "all", 'reqd' => "no" }, { 'name' => "streamingsize", 'desc' => "{VideoPlugin.streamingsize}", 'type' => "int", 'deft' => "352", 'reqd' => "no" }, { 'name' => "streamingbitrate", 'desc' => "{VideoPlugin.streamingbitrate}", 'type' => "string", 'deft' => "200k", 'reqd' => "no" }, { 'name' => "streamingHQsize", 'desc' => "{VideoPlugin.streamingsize}", 'type' => "int", 'deft' => "720", 'reqd' => "no" }, { 'name' => "streamingHQVideoBitrate", 'desc' => "{VideoPlugin.streamingbitrate}", 'type' => "int", 'deft' => "496", 'reqd' => "no" }, { 'name' => "streamingHQAudioBitrate", 'desc' => "{VideoPlugin.streamingbitrate}", 'type' => "int", 'deft' => "80", 'reqd' => "no" }, { 'name' => "videoDeinterlacingFilter", 'desc' => "Activate a deinterlacing filter to increase the quality of TV footage", 'type' => "enum", 'list' => [{'name' => "true", 'desc' => "{common.true}"}, {'name' => "false", 'desc' => "{common.false}"}], 'deft' => "false", 'reqd' => "no" }, { 'name' => "getMaximumMetadata", 'desc' => "Extract the maximum technical information of each video", 'type' => "enum", 'list' => [{'name' => "true", 'desc' => "{common.true}"}, {'name' => "false", 'desc' => "{common.false}"}], 'deft' => "false", 'reqd' => "no" }, { 'name' => "minimumsize", 'desc' => "{ImageConverter.minimumsize}", 'type' => "int", 'deft' => "100", 'range' => "1,", 'reqd' => "no" }, ]; my $options = { 'name' => "VideoConverter", 'desc' => "{VideoConverter.desc}", 'abstract' => "yes", 'inherits' => "yes", 'args' => $arguments }; sub new { my ($class) = shift (@_); my ($pluginlist,$inputargs,$hashArgOptLists) = @_; push(@$pluginlist, $class); push(@{$hashArgOptLists->{"ArgList"}},@{$arguments}); push(@{$hashArgOptLists->{"OptList"}},$options); my $self = new MultimediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1); return bless $self, $class; } # Discover the characteristics of a video file. # Equivalent step to that in ImagePlugin that uses ImageMagicks's # 'indentify' utility # Here we use 'MediaInfo' for video but for consistency keep the Perl # method name the same as before sub mediaInfo_Inform_Cmd { my ($video, $parameter, $outhandle, $verbosity) = @_; # Use MediaInfo CLI to get the file spec according to the requested parameter my $command = "mediainfo --Inform=\"$parameter\" \"$video\""; print $outhandle " $command\n" if ($verbosity > 2); my $result = ''; $result = `$command 2>&1`; chomp $result; print $outhandle " $result\n" if ($verbosity > 4); # Return the asked spec return ($result); } # Here we use mediaInfo to get all the possible metadata in XML form that could then be parsed #the result will vary depending on the file type and how it was encoded sub mediaInfo_XML_Cmd { my ($video, $outhandle, $verbosity) = @_; # Use MediaInfo CLI to get the file spec according to the requested parameter my $command = "mediainfo --Output=XML \"$video\""; print $outhandle " $command\n" if ($verbosity > 2); my $result = ''; $result = `$command 2>&1`; print $outhandle " $result\n" if ($verbosity > 4); # Return the asked spec return ($result); } #Here we parse the video specs contained in the XML text in order to create a metadata entry for each field sub mediaInfo_parse_XML { my ($xmlTxt, $outhandle, $verbosity) = @_; my @parts = split(//is,$xmlTxt); shift @parts; # Skip preamble my $metadata_table = {}; while (@parts) { my $type = shift(@parts); my $vals = shift(@parts); my @fieldlist=(); while ($vals =~ s/<(.*?)>(.*?)<\/\1>//) { my $metaname = $1; my $metaval = $2; my $fullmetaname = "mi.$type\^$metaname"; $metadata_table->{$fullmetaname}= $metaval; push(@fieldlist,$fullmetaname); } $metadata_table->{"mi.${type}Fields"}= join(",",@fieldlist); } return $metadata_table; while (@parts) { my $type = shift(@parts); my $vals = shift(@parts); my @fieldlist=(); while ($vals =~ s/<(.*?)>(.*?)<\/\1>//) { my $metaname = $1; my $metaval = $2; my $fullmetaname = "mi.$type\^$metaname"; if($metaname ne "Complete_name") #ignore this field as it is pointing to the cache directory { $metadata_table->{$fullmetaname}= $metaval; push(@fieldlist,$fullmetaname); } } $metadata_table->{"mi.${type}Fields"}= join(",",@fieldlist); } return $metadata_table; } sub identifyMI { my ($video, $outhandle, $verbosity) = @_; # Use the ffmpeg command to get the file specs my $command = "ffmpeg -i \"$video\""; print $outhandle " $command\n" if ($verbosity > 2); my $result = ''; $result = `$command 2>&1`; print $outhandle " $result\n" if ($verbosity > 4); # Read the type, width, and height etc. # This could be done in a more efficient way by only calling the application mediainfo once and # giving all the parameters then parsing the results, however on most operating system # once the application is called once it is then cached for subsequent calls. my $vtype = mediaInfo_Inform_Cmd($video,"General;%Format%",$outhandle,0); my $duration = mediaInfo_Inform_Cmd($video,"General;%Duration%",$outhandle,0); my $durationDisplay = mediaInfo_Inform_Cmd($video,"General;%Duration/String1%",$outhandle,0); my $filesize = mediaInfo_Inform_Cmd($video,"General;%FileSize/String%",$outhandle,0); my $vcodec = mediaInfo_Inform_Cmd($video,"Video;%Format%",$outhandle,0); my $vrate = mediaInfo_Inform_Cmd($video,"Video;%BitRate%",$outhandle,0); my $width = mediaInfo_Inform_Cmd($video,"Video;%Width%",$outhandle,0); my $height = mediaInfo_Inform_Cmd($video,"Video;%Height%",$outhandle,0); my $fps = mediaInfo_Inform_Cmd($video,"Video;%FrameRate%",$outhandle,0); my $atype = mediaInfo_Inform_Cmd($video,"Audio;%Format%",$outhandle,0); my $afreq = mediaInfo_Inform_Cmd($video,"Audio;%SamplingRate/String%",$outhandle,0); my $achan = mediaInfo_Inform_Cmd($video,"Audio;%Channel(s)%",$outhandle,0); my $arate = mediaInfo_Inform_Cmd($video,"Audio;%BitRate%",$outhandle,0); my $xmlTxt = mediaInfo_XML_Cmd ($video,$outhandle,0); my $metadata_table = mediaInfo_parse_XML($xmlTxt,$outhandle,0); # Return the specs return ($vtype,$width,$height,$duration,$durationDisplay,$filesize, $vcodec,$vrate,$fps,$atype,$afreq,$achan,$arate,$metadata_table); } sub identify { my ($video, $outhandle, $verbosity) = @_; # Use the ffmpeg command to get the file specs my $command = "ffmpeg -i \"$video\""; print $outhandle " $command\n" if ($verbosity > 2); my $result = ''; $result = `$command 2>&1`; print $outhandle " $result\n" if ($verbosity > 4); # Read the type, width, and height etc. my $vtype = 'unknown'; my $vcodec = 'unknown'; my $width = 'unknown'; my $height = 'unknown'; my $fps = 'unknown'; my $atype = 'unknown'; my $afreq = 'unknown'; my $achan = 'unknown'; my $arate = 'unknown'; my $video_safe = quotemeta $video; # strip off everything up to filename $result =~ s/^.*\'$video_safe\'://s; ## if ($result =~ m/Video: (.*?) fps/m) { if ($result =~ m/^\s+Stream.*?Video: (.*?)$/m) { my $video_info = $1; $video_info =~ s/\s*\[.*?\]//g; my @video_fields = split(/,\s*/,$video_info); $vtype = shift @video_fields; if ($video_fields[0] !~ m/(\d+)x(\d+)/) { $vcodec = shift @video_fields; } my $video_dim = shift @video_fields; ($width,$height) = ($video_dim =~ m/(\d+)x(\d+)/); if ($video_fields[0] =~ m/(\d+)\s+tbr$/) { $fps = $1; } # if ($video_info =~ m/([^,]+),(?: ([^,]+),)? (\d+)x(\d+),.*?(\d+\.\d+)/) # { # $vtype = $1; # $vcodec = $2 if defined $2; # $width = $3; # $height = $4; # $fps = $5; # } } if ($result =~ m/Audio: (\w+), (\d+) Hz, (\w+)(?:, (\d+.*))?/m) { $atype = $1; $afreq = $2; $achan = $3; $arate = $4 if (defined $4); } # Read the duration my $duration = "unknown"; if ($result =~ m/Duration: (\d+:\d+:\d+\.\d+)/m) { $duration = $1; } print $outhandle " file: $video:\t $vtype, $width, $height, $duration\n" if ($verbosity > 2); if ($verbosity >3) { print $outhandle "\t video codec=$vcodec, fps = $fps\n"; print $outhandle "\t audio codec=$atype, freq = $afreq Hz, $achan, $arate\n"; } # Return the specs return ($vtype, $width, $height, $duration, -s $video, $vcodec,$fps,$atype,$afreq,$achan,$arate); } sub vob_durations { my ($media_base_dir,$title_num,$outhandle) = @_; my $filter_re = sprintf("^VTS_%02d_[1-9]\\.VOB\$",$title_num); my $duration_info = {}; if (opendir(VTSIN,$media_base_dir)) { my @vts_title_vobs = grep { $_ =~ m/$filter_re/ } readdir(VTSIN); closedir(VTSIN); foreach my $v (@vts_title_vobs) { my $full_v = &util::filename_cat($media_base_dir,$v); my ($vtype, $width, $height, $duration, $durationDisplay, $vsize, $vcodec,$vrate,$fps,$atype,$afreq,$achan,$arate) = identify($full_v,$outhandle,0); my ($vob_num) = ($v =~ m/^VTS_\d\d_(\d)\.VOB$/); $duration_info->{$vob_num} = $duration; print STDERR "**** $title_num: $title_num, storing {$vob_num} => $duration\n"; } } else { print $outhandle "Warning: unable to read files in directory $media_base_dir\n"; } return $duration_info; } sub init_cache_for_file { my $self = shift(@_); my ($video_filename) = @_; $self->SUPER::init_cache_for_file($video_filename); my @flvtool2_monitor = ( 'monitor_init' ,"convertutil::monitor_init_unbuffered", 'monitor_line' , "VideoConverter::flvtool2_monitor_line", 'monitor_deinit' , "convertutil::monitor_deinit_unbuffered" ); $self->{'flvtool2_monitor'} = \@flvtool2_monitor; } sub optional_frame_scale { my $self = shift (@_); my ($orig_size,$video_width,$video_height) = @_; my $s_opt = ""; if ($video_width > $video_height) { if ($video_width > $orig_size) { my $scale_factor = $orig_size/$video_width; my $scaled_width = int($video_width * $scale_factor); my $scaled_height = int($video_height * $scale_factor); # round to be ensure multiple of 2 (needed by some codecs) $scaled_width = int($scaled_width/2)*2; $scaled_height = int($scaled_height/2)*2; $s_opt = "-s ${scaled_width}x${scaled_height}"; } # else, video is smaller than requested size, don't scale up } else { if ($video_height > $orig_size) { my $scale_factor = $orig_size/$video_height; my $scaled_width = int($video_width * $scale_factor); my $scaled_height = int($video_height * $scale_factor); # round to be ensure multiple of 2 (needed by some codecs) $scaled_width = int($scaled_width/2)*2; $scaled_height = int($scaled_height/2)*2; $s_opt = "-s ${scaled_width}x${scaled_height}"; } # else, video is smaller than requested size, don't scale up } return $s_opt; } sub keyframe_cmd { my $self = shift (@_); my ($ivideo_filename) = @_; my $video_ext_dir = &util::filename_cat($ENV{'GSDLHOME'},"ext","video"); my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; my $oshot_filename = &util::filename_cat($output_dir,"shots.xml"); my $exp_duration = $self->{'exp_duration'}; my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : ""; my $main_opts = "-y $t_opt"; my $hive = &util::filename_cat($video_ext_dir,"lib","vhook","hive.so"); my $oflash_filename = &util::filename_cat($output_dir,"$ivideo_root\_keyframe.flv"); my $vhook_opts = "$hive -o $oshot_filename -k $output_dir $ivideo_filename"; my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename); my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename); my $ffmpeg_cmd = "ffkeyframe $main_opts -vhook \"$vhook_opts\" -i \"$ivideo_filename_gsdlenv\" -an -y \"$oflash_filename_gsdlenv\""; return ($ffmpeg_cmd,$oflash_filename); } sub get_ovideo_file { my $self = shift (@_); my ($stream_type) = @_; my $ivideo_root = $self->{'cached_file_root'}; my $ofile; if ($stream_type eq "flv") { $ofile = "${ivideo_root}_vstream.flv"; } else { $ofile = "${ivideo_root}_vHQstream.mp4"; } return $ofile; } sub get_ovideo_filename { my $self = shift (@_); my ($stream_type) = @_; my $output_dir = $self->{'cached_dir'}; my $ofile = $self->get_ovideo_file($stream_type); my $ofilename = &util::filename_cat($output_dir,$ofile); return $ofilename; } # Generate the command to use with Handbrake to recompress a video using h264/aac compatible with Flash Player 10 sub stream_mp4_video_cmd { my $self = shift (@_); my ($ivideo_filename,$streaming_HQ_size, $streaming_HQ_VideoBitrate, $streaming_HQ_AudioBitrate) = @_; my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; my $omp4_file = "${ivideo_root}_vHQstream.mp4"; my $omp4_filename = &util::filename_cat($output_dir,$omp4_file); #my $exp_duration = $self->{'exp_duration'}; #my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : ""; my $deinterlace = $self->{'videoDeinterlacingFilter'}; my $video_processing_parameters; # Use specific resizing algorythms depending if we need to downsize the video resolution or not if(!$streaming_HQ_size || $streaming_HQ_size eq "fullsize") { $video_processing_parameters = "--strict-anamorphic"; } else { $video_processing_parameters = "-w $streaming_HQ_size --loose-anamorphic"; } # Use the deinterlace video filter if enabled in the plugin if ($deinterlace eq "true") { $video_processing_parameters .= " --decomb"; } my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename); my $omp4_filename_gsdlenv = $self->gsdlhome_independent($omp4_filename); # Generate the pre and post video parameters for Handbrake v0.9.4 my $pre_opts = "-t 1 -c 1"; 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"; my $handbrake_cmd = "HandbrakeCLI.exe -i \"$ivideo_filename_gsdlenv\" $pre_opts -o \"$omp4_filename_gsdlenv\" $post_opts"; #print STDERR "****\nHandbrake command: $handbrake_cmd\n****\n"; return ($handbrake_cmd,$omp4_filename,$omp4_file); } sub stream_flv_video_cmd { my $self = shift (@_); my ($ivideo_filename,$video_width,$video_height, $streaming_quality, $streaming_bitrate,$streaming_size, $opt_streaming_achan, $opt_streaming_arate) = @_; my $streaming_achan = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2; my $streaming_arate = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050; my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; my $oflash_file = "${ivideo_root}_vstream.flv"; my $oflash_filename = &util::filename_cat($output_dir,$oflash_file); my $s_opt = ""; my $media_type = $self->{'media_type'}; if ($media_type ne "audio") { $s_opt = $self->optional_frame_scale($streaming_size,$video_width,$video_height); } my $exp_duration = $self->{'exp_duration'}; my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : ""; my $main_opts = "-y $t_opt"; my $bitrate_opt = "-b $streaming_bitrate"; ### my $stream_opts = "-r 25 $s_opt"; my $stream_opts .= " $s_opt -ac $streaming_achan -ar $streaming_arate"; # -flags +ilme+ildct' and maybe '-flags +alt' for interlaced material, and try '-top 0/1' my $all_opts = "$main_opts $stream_opts"; my $ffmpeg_cmd; my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename); my $oflash_filename_gsdlenv = $self->gsdlhome_independent($oflash_filename); if ($streaming_quality eq "high") { my $pass_log_file = &util::filename_cat($output_dir,"$ivideo_root-logpass.txt"); if (-e $pass_log_file) { &util::rm($pass_log_file); } my $pass_log_file_gsdlenv = $self->gsdlhome_independent($pass_log_file); $all_opts .= " -passlogfile \"$pass_log_file_gsdlenv\""; my $ffmpeg_cmd_pass1 = "ffmpeg -pass 1 -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\""; my $ffmpeg_cmd_pass2 = "ffmpeg -pass 2 -i \"$ivideo_filename_gsdlenv\" $all_opts $bitrate_opt -y \"$oflash_filename_gsdlenv\""; $ffmpeg_cmd = "( $ffmpeg_cmd_pass1 && $ffmpeg_cmd_pass2 )"; } else { # single pass $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$oflash_filename_gsdlenv\""; } return ($ffmpeg_cmd,$oflash_filename,$oflash_file); } sub stream_flv_audio_cmd { # AudioConverter also has a routine for doing this # => merge into one! # ************** my $self = shift (@_); my ($ivideo_filename, $opt_streaming_achan, $opt_streaming_arate) = @_; # Convert either audio or video to streamable audio format my $streaming_achan = (defined $opt_streaming_achan) ? $opt_streaming_achan : 2; my $streaming_arate = (defined $opt_streaming_arate) ? $opt_streaming_arate : 22050; my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; my $ofla_file = "${ivideo_root}_astream.flv"; my $ofla_filename = &util::filename_cat($output_dir,$ofla_file); my $exp_duration = $self->{'exp_duration'}; my $t_opt = (defined $exp_duration) ? "-t $exp_duration" : ""; my $main_opts = "-vn -y $t_opt"; my $stream_opts .= " -ac $streaming_achan -ar $streaming_arate"; my $all_opts = "$main_opts $stream_opts"; my $ffmpeg_cmd; my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename); my $ofla_filename_gsdlenv = $self->gsdlhome_independent($ofla_filename); $ffmpeg_cmd = "ffmpeg -i \"$ivideo_filename_gsdlenv\" $all_opts -y \"$ofla_filename_gsdlenv\""; return ($ffmpeg_cmd,$ofla_filename,$ofla_file); } sub audio_excerpt_cmd { my $self = shift (@_); my ($ivoa_filename,$hh,$mm,$ss,$opt_excerpt_len) = @_; # ivoa = input video or audio my $time_encoded = "$hh:$mm:$ss"; my $time_encoded_file = "$hh$mm$ss"; my $output_dir = $self->{'cached_dir'}; my $ivoa_root = $self->{'cached_file_root'}; my $omp3_file = "${ivoa_root}_$time_encoded_file.mp3"; my $omp3_filename = &util::filename_cat($output_dir,$omp3_file); my $all_opts = "-y -acodec mp3 -ss $time_encoded "; if (defined $opt_excerpt_len) { $all_opts .= "-t $opt_excerpt_len "; } my $ivoa_filename_gsdlenv = $self->gsdlhome_independent($ivoa_filename); my $omp3_filename_gsdlenv = $self->gsdlhome_independent($omp3_filename); my $ffmpeg_cmd = "ffmpeg -i \"$ivoa_filename_gsdlenv\" $all_opts \"$omp3_filename_gsdlenv\""; return ($ffmpeg_cmd,$omp3_filename,$omp3_file); } sub streamseekable_cmd { my $self = shift (@_); my ($oflash_filename) = @_; my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; ## my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml"); my $flvtool_cmd = "flvtool2 -vUP \"$oflash_filename\""; return ($flvtool_cmd,$oflash_filename); } sub streamkeyframes_cmd { my $self = shift (@_); my ($oflash_filename,$doc_obj,$section) = @_; my $assocfilepath = $doc_obj->get_metadata_element($section,"assocfilepath"); my $output_dir = $self->{'cached_dir'}; my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml"); my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" \"$oflash_filename\""; return ($flvtool_cmd,$oflash_filename); } sub streamkeyframes_cmd_old { my $self = shift (@_); my ($oflash_filename,$doc_obj,$section) = @_; my $assocfilepath = $doc_obj->get_metadata_element($section,"assocfilepath"); my $output_dir = $self->{'cached_dir'}; my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml"); my $video_server = $ENV{'GEXT_VIDEO_SERVER'}; my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'}; my $collect = $ENV{'GSDLCOLLECTION'}; print STDERR "**** Warning: need to remove dependency of GEXT_VIDEO_SERVER and _PREFIX\n"; my $flvtool_cmd = "flvtool2 -vAUtP \"$cue_filename\" -thumbLocation:$video_server$video_prefix/collect/$collect/index/assoc/$assocfilepath \"$oflash_filename\""; return ($flvtool_cmd,$oflash_filename); } sub streamcuepts_cmd { my $self = shift (@_); my ($oflash_filename) = @_; my $output_dir = $self->{'cached_dir'}; my $cue_filename = &util::filename_cat($output_dir,"on_cue.xml"); ##my $video_server = $ENV{'GEXT_VIDEO_SERVER'}; ##my $video_prefix = $ENV{'GEXT_VIDEO_PREFIX'}; ## my $collect = $ENV{'GSDLCOLLECTION'}; ## my $thumbloc = "$video_server$video_prefix/collect/$collect"; # my $flvtool_cmd = "flvtool2 -vUAtP \"$cue_filename\" -thumbLocation:$thumbloc \"$oflash_filename\""; # my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\""; # my $flvtool_cmd = "flvtool2 -vUAt \"$cue_filename\" \"$oflash_filename\""; ## my $flvtool_cmd = "flvtool2 -vAt \"$cue_filename\" -UP \"$oflash_filename\" \"$output_dir/updated.flv\""; ## my $flvtool_cmd = "flvtool2 -vAtU \"$cue_filename\" \"$oflash_filename\" \"$output_dir/updated.flv\""; my $flvtool_cmd = "flvtool2 -vAtUP \"$cue_filename\" \"$oflash_filename\""; return ($flvtool_cmd,$oflash_filename); } sub keyframe_thumbnail_cmd { my $self = shift (@_); my ($ivideo_filename,$thumbnailfile,$thumbnailwidth,$thumbnailheight) = @_; my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root); # Try for 4th keyframe, but fall back to 1st if doesn't exist my $key_filename = "${key_filename_prefix}_0003.jpg"; $key_filename = "${key_filename_prefix}_0000.jpg" if (!-e $key_filename); my $key_filename_gsdlenv = $self->gsdlhome_independent($key_filename); my $thumbnailfile_gsdlenv = $self->gsdlhome_independent($thumbnailfile); my $command; if (-e $key_filename) { $command = "convert -interlace plane -verbose -geometry $thumbnailwidth" . "x$thumbnailheight \"$key_filename_gsdlenv\" \"$thumbnailfile_gsdlenv\""; } else { # extract_keyframe has either not been switched on, or else had # a problem when running # => extract a from # my $frame_rate = 1.0 / 60.0; my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename); $command = "ffmpeg -i \"$ivideo_filename_gsdlenv\" -ss 3.5 -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -y \"$thumbnailfile_gsdlenv\""; # fmpeg -i input.dv -r 1 -f image2 -s 120x96 images%05d.png } return ($command,$thumbnailfile); } sub keyframe_montage_cmd { my $self = shift (@_); my ($ivideo_filename,$montagefile) = @_; my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; my $key_filename_prefix = &util::filename_cat($output_dir,$ivideo_root); my $options = "-tile 10 -geometry 75x62+2+2"; my $command = "montage $options ${key_filename_prefix}_*.jpg \"$montagefile\""; return ($command,$montagefile); } sub parse_shot_xml { my ($self) = shift(@_); my $outhandle = $self->{'outhandle'}; my $output_dir = $self->{'cached_dir'}; my $shots_filename = &util::filename_cat($output_dir,"shots.xml"); eval { $self->{'parser'}->parsefile($shots_filename); }; if ($@) { print $outhandle "VideoConverter: skipping $shots_filename as not conformant to Hive shot syntax\n" if ($self->{'verbosity'} > 1); print $outhandle "\n Perl Error:\n $@\n" if ($self->{'verbosity'}>2); return 0; } } sub associate_keyframes_old { my ($self) = shift(@_); my ($doc_obj,$section) = @_; my $output_dir = $self->{'cached_dir'}; my $count = 1; foreach my $kframe_file (@{$self->{'keyframe_fnames'}}) { my $kframe_filename = &util::filename_cat($output_dir,$kframe_file); $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg","image/jpeg", $section); $count++; } $doc_obj->add_utf8_metadata($section,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}})); # ***** # $doc_obj->add_metadata ($section, "thumblist", $self->{'flowplayer_thumblist'}); } sub associate_keyframes { my ($self) = shift(@_); my ($doc_obj,$topsection,$timeline) = @_; my $output_dir = $self->{'cached_dir'}; my $count = 1; foreach my $t (sort { $timeline->{$a}->{'keyframeindex'} <=> $timeline->{$b}->{'keyframeindex'} } keys %$timeline) { my $kframe_file = $timeline->{$t}->{'thumb'}; my $timestamp = $timeline->{$t}->{'timestamp'}; # create next sub-section to video "document" my $endchild = $doc_obj->insert_section($doc_obj->get_end_child($topsection)); $doc_obj->add_utf8_metadata($endchild,"Title","Timestamp $timestamp"); $doc_obj->add_utf8_metadata($endchild,"FrameNum",$t); my $kframe_filename = &util::filename_cat($output_dir,$kframe_file); $doc_obj->associate_file($kframe_filename,"keyframe$count.jpg", "image/jpeg", $endchild); $doc_obj->add_utf8_metadata($endchild,"assockeyframe","keyframe$count.jpg"); $doc_obj->add_utf8_metadata($topsection,"KeyframeTimestamp",$timestamp); $doc_obj->add_utf8_metadata($topsection,"KeyframeFrameNum",$t); $count++; } #### $doc_obj->add_utf8_metadata($topsection,"NumKeyframes",scalar(@{$self->{'keyframe_fnames'}})); $doc_obj->add_utf8_metadata($topsection,"NumKeyframes",scalar(keys %$timeline)); # ***** # $doc_obj->add_metadata ($topsection, "thumblist", $self->{'flowplayer_thumblist'}); } sub enable_audio_streaming { my $self = shift (@_); my ($doc_obj,$originalfilename,$filename,$convertto_regenerated) = @_; my $section = $doc_obj->get_top_section(); my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; # Generate FLV audio-only format for streaming purposes my $optionally_run_general_cmd = "run_uncached_general_cmd"; if ($self->{'enable_cache'}) { $optionally_run_general_cmd = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd"; } my $ifilename = $originalfilename || $filename; my ($stream_cmd,$ofla_filename,$ofla_file) = $self->stream_flv_audio_cmd($ifilename); my $streamable_options = { @{$self->{'ffmpeg_monitor'}}, 'message_prefix' => "Stream", 'message' => "Generating streamable audio: $ofla_file" }; my ($streamable_regenerated,$streamable_result,$streamable_had_error) = $self->$optionally_run_general_cmd($stream_cmd, $ifilename,$ofla_filename, $streamable_options); if (!$streamable_had_error) { my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($ofla_filename); my $streamseekable_options = { @{$self->{'flvtool2_monitor'}}, 'message_prefix' => "Stream Seekable", 'message' => "Reprocessing audio stream to be seekable by timeline: $ofla_file" }; if ($streamable_regenerated) { $self->run_general_cmd($streamseekable_cmd,$streamseekable_options); } my $streamable_url = $ofla_file; my $streamable_url_safe = $self->url_safe($streamable_url); $doc_obj->add_utf8_metadata ($section, "streamableaudio", $streamable_url_safe); $doc_obj->associate_file($ofla_filename,$ofla_file,"audio/flash", $section); } $doc_obj->add_metadata ($section, "audioflashwidth", 400); $doc_obj->add_metadata ($section, "audioflashheight", 22 + 100); $self->{'ofla_file'} = $ofla_file; $self->{'ofla_filename'} = $ofla_filename; return $streamable_regenerated; } sub enable_video_streaming { my $self = shift (@_); my ($doc_obj,$originalfilename,$filename,$convertto_regenerated, $video_width,$video_height) = @_; my $section = $doc_obj->get_top_section(); my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; # Generate the Flash FLV format for streaming purposes my $optionally_run_general_cmd = "run_uncached_general_cmd"; if ($self->{'enable_cache'}) { $optionally_run_general_cmd = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd"; } my $streaming_bitrate = $self->{'streamingbitrate'}; my $streaming_size = $self->{'streamingsize'}; my $streaming_quality = "high"; my $ifilename = $originalfilename || $filename; my ($stream_cmd,$oflash_filename,$oflash_file) = $self->stream_flv_video_cmd($ifilename, $video_width,$video_height, $streaming_quality, $streaming_bitrate, $streaming_size); my $streamable_options = { @{$self->{'ffmpeg_monitor'}}, 'message_prefix' => "Stream", 'message' => "Generating streamable video: $oflash_file" }; my ($streamable_regenerated,$streamable_result,$streamable_had_error) = $self->$optionally_run_general_cmd($stream_cmd, $ifilename,$oflash_filename, $streamable_options); if (!$streamable_had_error) { my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($oflash_filename); my $streamseekable_options = { @{$self->{'flvtool2_monitor'}}, 'message_prefix' => "Stream Seekable", 'message' => "Reprocessing video stream to be seekable by timeline: $oflash_file" }; if ($streamable_regenerated) { $self->run_general_cmd($streamseekable_cmd,$streamseekable_options); } my $streamable_url = $oflash_file; my $streamable_url_safe = $self->url_safe($streamable_url); $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe); $doc_obj->associate_file($oflash_filename,$oflash_file,"video/flash", $section); } # # FlowPlayer.swf height+22 pixels # FlowPlayerBlack.swf height+16 pixels # FlowPlayerThermo.swf height+16 pixels # FlowPlayerWhite.swf height+26 pixels my $flashwidth = $video_width; my $flashheight = $video_height + 22; if ($self->{'extract_keyframes'}) { $flashheight += 100; } $doc_obj->add_metadata ($section, "flashwidth", $flashwidth); $doc_obj->add_metadata ($section, "flashheight", $flashheight); $self->{'oflash_file'} = $oflash_file; $self->{'oflash_filename'} = $oflash_filename; return $streamable_regenerated; } sub enable_h264_streaming { my $self = shift (@_); my ($doc_obj,$originalfilename,$filename,$convertto_regenerated, $video_width,$video_height) = @_; my $section = $doc_obj->get_top_section(); my $output_dir = $self->{'cached_dir'}; my $ivideo_root = $self->{'cached_file_root'}; # Generate the H264 video file format for streaming purposes using the cache feature if used my $optionally_run_general_cmd = "run_uncached_general_cmd"; if ($self->{'enable_cache'}) { $optionally_run_general_cmd = ($convertto_regenerated) ? "regenerate_general_cmd" : "run_cached_general_cmd"; } my $streaming_HQ_size = $self->{'streamingHQsize'}; my $streaming_HQ_VideoBitrate = $self->{'streamingHQVideoBitrate'}; my $streaming_HQ_AudioBitrate = $self->{'streamingHQAudioBitrate'}; my $ifilename = $originalfilename || $filename; my ($stream_cmd,$oflash_filename,$oflash_file) = $self->stream_mp4_video_cmd($ifilename, $streaming_HQ_size, $streaming_HQ_VideoBitrate, $streaming_HQ_AudioBitrate); my $streamable_options = { @{$self->{'handbrake_monitor'}}, 'message_prefix' => "MP4 Stream", 'message' => "Generating streamable MP4 video: $oflash_file" }; my ($streamable_regenerated,$streamable_result,$streamable_had_error) = $self->$optionally_run_general_cmd($stream_cmd, $ifilename,$oflash_filename, $streamable_options); if (!$streamable_had_error) { my $streamable_url = $oflash_file; my $streamable_url_safe = $self->url_safe($streamable_url); my $outhandle = $self->{'outhandle'}; my ($vtype, $width, $height, $duration, $durationDisplay, $vsize, $vcodec,$vrate,$fps,$atype,$afreq,$achan,$arate,$metadata_table) = identifyMI($oflash_filename,$outhandle,0); #Add the most common metadata extracted by MediaInfo from the streamable video file $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFormat", $vtype); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFileSize", $vsize); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingWidth", $width); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingHeight", $height); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingDuration", $durationDisplay); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingFPS", $fps); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoCodec", $vcodec); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoBitrate", $vrate); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioCodec", $atype); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioBitrate", $arate); $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingFileBitrate", $vrate + $arate); my $get_max_metadata = $self->{'getMaximumMetadata'}; if ($get_max_metadata eq "true"){ foreach my $metaname(keys %$metadata_table) { my $metaval = $metadata_table->{$metaname}; $doc_obj->add_utf8_metadata ($section, $metaname, $metaval); } } #add the metadata for the resulting file that should be open inside the flash video player $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe); $doc_obj->associate_file($oflash_filename,$oflash_file,"video/mp4", $section); } my $flashwidth = $video_width; my $flashheight = $video_height + 22; if ($self->{'extract_keyframes'}) { $flashheight += 100; } $doc_obj->add_metadata ($section, "flashwidth", $flashwidth); $doc_obj->add_metadata ($section, "flashheight", $flashheight); $self->{'oflash_file'} = $oflash_file; $self->{'oflash_filename'} = $oflash_filename; return $streamable_regenerated; } sub enable_full_streaming { my $self = shift (@_); my ($doc_obj,$originalfilename,$filename,$convertto_regenerated, $video_width,$video_height) = @_; my $video_streamable_regenerated = $self->enable_video_streaming($doc_obj,$originalfilename,$filename, $convertto_regenerated, $video_width,$video_height); my $audio_streamable_regenerated = $self->enable_audio_streaming($doc_obj,$originalfilename,$filename, $convertto_regenerated); return ($video_streamable_regenerated || $audio_streamable_regenerated); } sub flvtool2_monitor_line { my ($line) = @_; my $had_error = 0; my $generate_dot = 1; if ($line =~ m/\s+\- /) { # ignore tabulated output printed at end of command $generate_dot = 0; } if ($line =~ m/^Error:/i) { $had_error = 1; } return ($had_error,$generate_dot); } 1;