###########################################################################
#
# 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' => "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' => "352",
	'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' => "576",
	'reqd' => "no" },
	{ 'name' => "streamingHQVideoBitrate",
	'desc' => "{VideoPlugin.streamingbitrate}",
	'type' => "int",
	'deft' => "426",
	'reqd' => "no" },
	{ 'name' => "streamingHQAudioBitrate",
	'desc' => "{VideoPlugin.streamingbitrate}",
	'type' => "int",
	'deft' => "86",
	'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 'ffmpeg' for video but for consistency keep the Perl
#  method name the same as before


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_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, $vsize,
		$vcodec,$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 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 $size;	
	# Looks for empty string as well as fullsize string
	if(!$streaming_HQ_size || $streaming_HQ_size eq "fullsize") { 
		$size = "--strict-anamorphic";
	} else {
		$size = "-w $streaming_HQ_size --loose-anamorphic";
	}
	

    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
    my $omp4_filename_gsdlenv = $self->gsdlhome_independent($omp4_filename);
	
	my $pre_opts = "--decomb -t 1 -c 1";
	my $post_opts = "-f mp4 $size -O -e x264 -b $streaming_HQ_VideoBitrate -a 1 -E faac -6 dpl2 -B $streaming_HQ_AudioBitrate -D 0.0 -x ref=2:bframes=2:me-umh -v 1";
	
	my $handbrake_cmd = "HandbrakeCLI.exe -i \"$ivideo_filename_gsdlenv\" $pre_opts -o \"$omp4_filename_gsdlenv\" $post_opts";


    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 12.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 ($stream_cmd,$ofla_filename,$ofla_file) 
	= $self->stream_flv_audio_cmd($originalfilename || $filename);
    
    
    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,$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 ($stream_cmd,$oflash_filename,$oflash_file) 
	= $self->stream_flv_video_cmd($originalfilename || $filename,
				     $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,$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_bitrate = $self->{'streamingbitrate'};
   ## my $streaming_size    = $self->{'streamingsize'};
	my $streaming_HQ_size    = $self->{'streamingHQsize'};
	my $streaming_HQ_VideoBitrate    = $self->{'streamingHQVideoBitrate'};
	my $streaming_HQ_AudioBitrate    = $self->{'streamingHQAudioBitrate'};
    
  ##  my $streaming_quality = "high";
    
    my ($stream_cmd,$oflash_filename,$oflash_file) 
	= $self->stream_mp4_video_cmd($originalfilename || $filename,
				     $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,$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);
	
	#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,"VideoStream.mp4","video/mp4",
				 $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_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;
