Esempio n. 1
0
 protected override MediaInfo GetInfo(string fullFilename)
 {
     VideoInfo result = new VideoInfo();
     Tool tool = new Tool(Tool.Which.FFprobe, new string[] { "-hide_banner", "-print_format xml=fully_qualified=1", "-show_format", "-show_streams", "-show_error", Tool.Escape(fullFilename) }, new int[] { 0 });
     Tool.Result toolResult = tool.Run();
     if (toolResult.Error != null)
     {
         throw toolResult.Error;
     }
     FFprobe.ffprobeType ffp;
     using (MemoryStream ms = new MemoryStream())
     {
         byte[] bytes = System.Text.Encoding.UTF8.GetBytes(toolResult.StdOut);
         ms.Write(bytes, 0, bytes.Length);
         ms.Position = 0L;
         using (XmlReader xr = XmlReader.Create(ms))
         {
             ffp = (MediaData.FFprobe.ffprobeType)FFmpegVideoProcessor.FFProbeDeserializer.Deserialize(xr);
         }
     }
     if (ffp.error != null)
     {
         throw new Exception(ffp.error.@string);
     }
     if (ffp.format.tag != null)
     {
         foreach (FFprobe.tagType tag in ffp.format.tag)
         {
             switch (tag.key)
             {
                 case "creation_time":
                     DateTime? dt = FFmpegVideoProcessor.ParseCreationTimeTag(tag.value);
                     if (!dt.HasValue)
                     {
                         throw new Exception(string.Format(i18n.Invalid_tag_X_value_V, tag.key, tag.value));
                     }
                     result.AddAlternativeMetadataTimestamp(string.Format(i18n.GlobalTag_X, tag.key), dt.Value);
                     break;
                 case "location":
                 case "location-eng":
                     Position position = FFmpegVideoProcessor.ParseLocationTag(tag.value);
                     if (position == null)
                     {
                         throw new Exception(string.Format(i18n.Invalid_tag_X_value_V, tag.key, tag.value));
                     }
                     if (result.Position == null || (result.Position.Lat == 0M && result.Position.Lng == 0M))
                     {
                         result.Position = position;
                     }
                     else if (position.Lat != 0M && position.Lng != 0M && (position.Lat != result.Position.Lat || position.Lng != result.Position.Lng))
                     {
                         throw new Exception(i18n.Multiple_GPS_positions_found);
                     }
                     break;
             }
         }
     }
     if (ffp.streams != null)
     {
         foreach (FFprobe.streamType stream in ffp.streams)
         {
             switch (stream.codec_type)
             {
                 case "audio":
                     result.AudioTracks++;
                     break;
                 case "video":
                     result.VideoTracks++;
                     break;
                 default:
                     throw new Exception(string.Format(i18n.Unsupported_stream_type_X, stream.codec_type));
             }
             if (stream.tag != null)
             {
                 foreach (FFprobe.tagType tag in stream.tag)
                 {
                     switch (tag.key)
                     {
                         case "creation_time":
                             DateTime? dt = FFmpegVideoProcessor.ParseCreationTimeTag(tag.value);
                             if (!dt.HasValue)
                             {
                                 throw new Exception(string.Format(i18n.Invalid_tag_X_value_V, tag.key, tag.value));
                             }
                             result.AddAlternativeMetadataTimestamp(string.Format(i18n.StreamTag_X, tag.key), dt.Value);
                             break;
                         case "location":
                         case "location-eng":
                             Position position = FFmpegVideoProcessor.ParseLocationTag(tag.value);
                             if (position == null)
                             {
                                 throw new Exception(string.Format(i18n.Invalid_tag_X_value_V, tag.key, tag.value));
                             }
                             if (result.Position == null || (result.Position.Lat == 0 && result.Position.Lng == 0))
                             {
                                 result.Position = position;
                             }
                             else if (position.Lat != 0 && position.Lng != 0 && (position.Lat != result.Position.Lat || position.Lng != result.Position.Lng))
                             {
                                 throw new Exception(i18n.Multiple_GPS_positions_found);
                             }
                             break;
                     }
                 }
             }
         }
     }
     if (result.VideoTracks == 0 && result.AudioTracks == 0)
     {
         throw new Exception(i18n.No_video_audio);
     }
     return result;
 }