Changeset 21824


Ignore:
Timestamp:
2010-03-26T12:20:42+13:00 (11 years ago)
Author:
max
Message:

Allow to keep original video file
Uses MediaInfo instead of ffmpeg to extract technical metadata
Uses the original file name to create compressed filename
Optimisations for speed and use of HandBrake 0.9.4 Syntax

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/video/trunk/perllib/plugins/VideoConverter.pm

    r21606 r21824  
    4747    'deft' => "true",
    4848    '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" },
    4956      { 'name' => "thumbnailsize",
    5057    'desc' => "{ImageConverter.thumbnailsize}",
     
    7582    'desc' => "{ImageConverter.screenviewsize}",
    7683    'type' => "int",
    77     'deft' => "352",
     84    'deft' => "576",
    7885    'range' => "1,",
    7986    'reqd' => "no" },
     
    134141    'desc' => "{VideoPlugin.streamingbitrate}",
    135142    'type' => "int",
    136     'deft' => "426",
     143    'deft' => "432",
    137144    'reqd' => "no" },
    138145    { 'name' => "streamingHQAudioBitrate",
    139146    'desc' => "{VideoPlugin.streamingbitrate}",
    140147    'type' => "int",
    141     'deft' => "86",
     148    'deft' => "80",
    142149    'reqd' => "no" },
    143150
     
    180187#  'indentify' utility
    181188
    182 #  Here we use 'ffmpeg' for video but for consistency keep the Perl
     189#  Here we use 'MediaInfo' for video but for consistency keep the Perl
    183190#  method name the same as before
    184191
     192sub mediaInfo_Inform_Cmd {
     193    my ($video, $parameter, $outhandle, $verbosity) = @_;
     194   
     195    # Use MediaInfo CLI to get the file spec according to the requested parameter
     196    my $command = "mediainfo --Inform=\"$parameter\" \"$video\"";
     197
     198    print $outhandle "  $command\n" if ($verbosity > 2);
     199    my $result = '';
     200    $result = `$command 2>&1`;
     201    chomp $result;
     202    print $outhandle "  $result\n" if ($verbosity > 4);
     203   
     204    # Return the asked spec
     205    return ($result);
     206}
     207
     208# Here we use mediaInfo to get all the possible metadata in XML form that could then be parsed
     209#the result will vary depending on the file type and how it was encoded
     210sub mediaInfo_XML_Cmd   {
     211    my ($video, $outhandle, $verbosity) = @_;
     212   
     213    # Use MediaInfo CLI to get the file spec according to the requested parameter
     214    my $command = "mediainfo --Output=XML \"$video\"";
     215
     216    print $outhandle "  $command\n" if ($verbosity > 2);
     217    my $result = '';
     218    $result = `$command 2>&1`;
     219    print $outhandle "  $result\n" if ($verbosity > 4);
     220   
     221    # Return the asked spec
     222    return ($result);
     223}
     224
     225#Here we parse the video specs contained in the XML text in order to create a metadata entry for each field
     226sub mediaInfo_parse_XML     {
     227    my ($xmlTxt, $outhandle, $verbosity) = @_;
     228   
     229    my @parts = split(/<track\s+type=\"(.*?)\">/is,$xmlTxt);
     230   
     231    shift @parts; # Skip preamble
     232    my $metadata_table = {};
     233
     234    while (@parts) {
     235        my $type = shift(@parts);
     236        my $vals = shift(@parts);
     237        my @fieldlist=();
     238        while ($vals =~ s/<(.*?)>(.*?)<\/\1>//) {
     239          my $metaname = $1;
     240          my $metaval  = $2;
     241          my $fullmetaname = "mi.$type\^$metaname";
     242          $metadata_table->{$fullmetaname}= $metaval;
     243          push(@fieldlist,$fullmetaname);
     244        }
     245       
     246        $metadata_table->{"mi.${type}Fields"}= join(",",@fieldlist);
     247       
     248    }
     249   
     250    return $metadata_table;
     251
     252}
    185253
    186254sub identify {
     255    my ($video, $outhandle, $verbosity) = @_;
     256
     257    # Use the ffmpeg command to get the file specs
     258    my $command = "ffmpeg -i \"$video\"";
     259
     260    print $outhandle "  $command\n" if ($verbosity > 2);
     261    my $result = '';
     262    $result = `$command 2>&1`;
     263    print $outhandle "  $result\n" if ($verbosity > 4);
     264
     265    # Read the type, width, and height etc.
     266    # This could be done in a more efficient way by only calling the application mediainfo once and
     267    # giving all the parameters then parsing the results, however on most operating system
     268    # once the application is called once it is then cached for subsequent calls.
     269    my $vtype =  mediaInfo_Inform_Cmd($video,"General;%Format%",$outhandle,0);
     270    my $duration = mediaInfo_Inform_Cmd($video,"General;%Duration%",$outhandle,0);
     271    my $durationDisplay = mediaInfo_Inform_Cmd($video,"General;%Duration/String1%",$outhandle,0);
     272    my $filesize = mediaInfo_Inform_Cmd($video,"General;%FileSize/String%",$outhandle,0);
     273    my $vcodec = mediaInfo_Inform_Cmd($video,"Video;%Format%",$outhandle,0);
     274    my $vrate = mediaInfo_Inform_Cmd($video,"Video;%BitRate%",$outhandle,0);
     275    my $width =  mediaInfo_Inform_Cmd($video,"Video;%Width%",$outhandle,0);
     276    my $height = mediaInfo_Inform_Cmd($video,"Video;%Height%",$outhandle,0);
     277    my $fps    = mediaInfo_Inform_Cmd($video,"Video;%FrameRate%",$outhandle,0);
     278    my $atype =  mediaInfo_Inform_Cmd($video,"Audio;%Format%",$outhandle,0);
     279    my $afreq =  mediaInfo_Inform_Cmd($video,"Audio;%SamplingRate/String%",$outhandle,0);
     280    my $achan =  mediaInfo_Inform_Cmd($video,"Audio;%Channel(s)%",$outhandle,0);
     281    my $arate =  mediaInfo_Inform_Cmd($video,"Audio;%BitRate%",$outhandle,0);
     282
     283    my $xmlTxt =  mediaInfo_XML_Cmd ($video,$outhandle,0);
     284   
     285    my $metadata_table = mediaInfo_parse_XML($xmlTxt,$outhandle,0);
     286   
     287    # Return the specs
     288    return ($vtype,$width,$height,$duration,$durationDisplay,$filesize,
     289        $vcodec,$vrate,$fps,$atype,$afreq,$achan,$arate,$metadata_table);
     290}
     291
     292
     293sub identify2 {
    187294    my ($video, $outhandle, $verbosity) = @_;
    188295
     
    227334
    228335    ($width,$height) = ($video_dim =~ m/(\d+)x(\d+)/);
     336   
     337    if ($video_fields[0] =~ m/(\d+)\s+tbr$/) {
     338        $fps = $1;
     339    }
    229340
    230341#   if ($video_info =~ m/([^,]+),(?: ([^,]+),)? (\d+)x(\d+),.*?(\d+\.\d+)/)
     
    279390        my $full_v = &util::filename_cat($media_base_dir,$v);
    280391
    281         my ($vtype, $width, $height, $duration, $vsize,
    282         $vcodec,$fps,$atype,$afreq,$achan,$arate) = identify($full_v,$outhandle,0);
     392        my ($vtype, $width, $height, $duration, $durationDisplay, $vsize,
     393        $vcodec,$vrate,$fps,$atype,$afreq,$achan,$arate) = identify($full_v,$outhandle,0);
    283394
    284395        my ($vob_num) = ($v =~ m/^VTS_\d\d_(\d)\.VOB$/);
     
    392503}
    393504
     505sub get_ovideo_file
     506{
     507    my $self = shift (@_);
     508    my ($stream_type) = @_;
     509   
     510    my $ivideo_root = $self->{'cached_file_root'};
     511    my $ofile;
     512   
     513    if ($stream_type eq "flv")
     514    {
     515        $ofile = "${ivideo_root}_vstream.flv";
     516    }
     517    else
     518    {
     519        $ofile = "${ivideo_root}_vHQstream.mp4";
     520    }
     521   
     522    return $ofile;
     523}
     524
     525sub get_ovideo_filename
     526{
     527    my $self = shift (@_);
     528    my ($stream_type) = @_;
     529   
     530    my $output_dir = $self->{'cached_dir'};
     531
     532    my $ofile = $self->get_ovideo_file($stream_type);
     533       
     534    my $ofilename = &util::filename_cat($output_dir,$ofile);
     535   
     536    return $ofilename;
     537}
    394538
    395539sub stream_mp4_video_cmd
     
    424568    my $omp4_filename_gsdlenv = $self->gsdlhome_independent($omp4_filename);
    425569   
    426     my $pre_opts = "--decomb -t 1 -c 1";
    427     my $post_opts = "-f mp4 $size -O -e x264 -b $streaming_HQ_VideoBitrate -a 1 -E faac -6 dpl2 -B $streaming_HQ_AudioBitrate -D 0.0 -x ref=2:bframes=2:me-umh -v 1";
    428    
     570    #my $pre_opts = "--decomb -t 1 -c 1";
     571    #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";
     572   
     573    my $pre_opts = "-t 1 -c 1";
     574    my $post_opts = "-f mp4 -O $size --decomb -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";
     575
    429576    my $handbrake_cmd = "HandbrakeCLI.exe -i \"$ivideo_filename_gsdlenv\" $pre_opts -o \"$omp4_filename_gsdlenv\" $post_opts";
    430577
     
    715862    my $ivideo_filename_gsdlenv = $self->gsdlhome_independent($ivideo_filename);
    716863
    717     $command = "ffmpeg -i \"$ivideo_filename_gsdlenv\"  -ss 12.5 -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -y \"$thumbnailfile_gsdlenv\"";
     864    $command = "ffmpeg -i \"$ivideo_filename_gsdlenv\"  -ss 3.5 -vframes 1 -f image2 -s ${thumbnailwidth}x${thumbnailheight} -y \"$thumbnailfile_gsdlenv\"";
    718865
    719866    # fmpeg -i input.dv -r 1 -f image2 -s 120x96 images%05d.png
     
    10021149    }
    10031150
    1004 
    1005    ## my $streaming_bitrate = $self->{'streamingbitrate'};
    1006    ## my $streaming_size    = $self->{'streamingsize'};
    10071151    my $streaming_HQ_size    = $self->{'streamingHQsize'};
    10081152    my $streaming_HQ_VideoBitrate    = $self->{'streamingHQVideoBitrate'};
    10091153    my $streaming_HQ_AudioBitrate    = $self->{'streamingHQAudioBitrate'};
    1010    
    1011   ##  my $streaming_quality = "high";
    1012    
     1154
    10131155    my ($stream_cmd,$oflash_filename,$oflash_file)
    10141156    = $self->stream_mp4_video_cmd($originalfilename || $filename,
     
    10271169   
    10281170    if (!$streamable_had_error) {
    1029 #   my ($streamseekable_cmd,$ostreamseekable_filename) = $self->streamseekable_cmd($oflash_filename);
    1030    
    1031 #   my $streamseekable_options = { @{$self->{'flvtool2_monitor'}},
    1032 #                      'message_prefix' => "Stream Seekable",
    1033 #                      'message' => "Reprocessing video stream to be seekable by timeline: $oflash_file" };
    1034    
    1035 #   if ($streamable_regenerated) {
    1036 #       $self->run_general_cmd($streamseekable_cmd,$streamseekable_options);
    1037 #   }
     1171
    10381172   
    10391173    my $streamable_url = $oflash_file;
    10401174    my $streamable_url_safe = $self->url_safe($streamable_url);
    10411175   
     1176    my $outhandle = $self->{'outhandle'};
     1177
     1178    my ($vtype, $width, $height, $duration, $durationDisplay, $vsize,
     1179        $vcodec,$vrate,$fps,$atype,$afreq,$achan,$arate,$metadata_table) = identify($oflash_filename,$outhandle,0);
     1180       
     1181    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFormat", $vtype);
     1182    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoFileSize", $vsize);
     1183    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingWidth", $width);
     1184    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingHeight", $height);
     1185    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingDuration", $durationDisplay);
     1186    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingFPS", $fps);
     1187    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoCodec", $vcodec);
     1188    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingVideoBitrate", $vrate);
     1189    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioCodec", $atype);
     1190    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingAudioBitrate", $arate);
     1191    $doc_obj->add_utf8_metadata ($section, "tv.HQStreamingFileBitrate", $vrate + $arate);
     1192   
     1193    foreach my $metaname(keys %$metadata_table)
     1194    {
     1195        my $metaval = $metadata_table->{$metaname};
     1196       
     1197        $doc_obj->add_utf8_metadata ($section, $metaname, $metaval);
     1198    }
     1199   
    10421200    #add the metadata for the resulting file that should be open inside the flash video player
    10431201    $doc_obj->add_utf8_metadata ($section, "streamablevideo", $streamable_url_safe);
    1044     $doc_obj->associate_file($oflash_filename,"VideoStream.mp4","video/mp4",
     1202    $doc_obj->associate_file($oflash_filename,$oflash_file,"video/mp4",
    10451203                 $section);
    10461204    }
    10471205
    10481206
    1049     #
    1050     # FlowPlayer.swf       height+22 pixels
    1051     # FlowPlayerBlack.swf  height+16 pixels
    1052     # FlowPlayerThermo.swf height+16 pixels
    1053     # FlowPlayerWhite.swf  height+26 pixels
     1207
    10541208
    10551209    my $flashwidth = $video_width;
Note: See TracChangeset for help on using the changeset viewer.