Beispiel #1
0
        private MediaInfoData GetMediaInfo(string location)
        {
            Logger.ReportInfo("getting media info from " + location);
            MediaInfo     mediaInfo     = new MediaInfo();
            int           i             = mediaInfo.Open(location);
            MediaInfoData mediaInfoData = null;

            if (i != 0)
            {
                int width;
                Int32.TryParse(mediaInfo.Get(StreamKind.Video, 0, "Width"), out width);
                int height;
                Int32.TryParse(mediaInfo.Get(StreamKind.Video, 0, "Height"), out height);
                int videoBitRate;
                Int32.TryParse(mediaInfo.Get(StreamKind.Video, 0, "BitRate"), out videoBitRate);
                int audioBitRate;
                Int32.TryParse(mediaInfo.Get(StreamKind.Audio, 0, "BitRate"), out audioBitRate);
                mediaInfoData = new MediaInfoData
                {
                    VideoCodec   = mediaInfo.Get(StreamKind.Video, 0, "CodecID/Hint"),
                    VideoBitRate = videoBitRate,
                    //MI.Get(StreamKind.Video, 0, "DisplayAspectRatio")),
                    Height = height,
                    Width  = width,
                    //MI.Get(StreamKind.Video, 0, "Duration/String3")),
                    AudioFormat  = mediaInfo.Get(StreamKind.Audio, 0, "Format"),
                    AudioBitRate = audioBitRate
                };
            }
            else
            {
                Logger.ReportInfo("Could not extract media information from " + location);
            }
            mediaInfo.Close();
            return(mediaInfoData);
        }
Beispiel #2
0
        private List <string> ExtractCriteriaItems(string criteria, MovieInfo _info)
        {
            List <string> _criteriaItems = null;

            if (_info != null)
            {
                object _reflectedObject = _info;

                // try to get propertyinfo for the criteria
                Type _t = _info.GetType();

                PropertyInfo _p = _t.GetProperty(criteria);
                // property not found on MovieInfo object
                if (_p == null)
                {
                    // maybe it is a MediaInfoData property?
                    _p = _t.GetProperty("MediaInfo");
                    if (_p != null)
                    {
                        // get MediaInfoData object from the MovieInfo
                        MediaInfoData _mdata = _t.GetProperty("MediaInfo").GetValue(_reflectedObject, null) as MediaInfoData;
                        if (_mdata != null)
                        {
                            // the object to get prop value is now MediaInfoData
                            _reflectedObject = _mdata;
                            // try to get Property for the criteria
                            _p = _mdata.GetType().GetProperty(criteria);
                        }
                    }
                }

                if (_p != null)
                {
                    if (_p.PropertyType == typeof(List <string>))
                    {
                        // list of strings, split it and use individual item
                        _criteriaItems = _p.GetValue(_reflectedObject, null) as List <string>;
                        if (_criteriaItems != null)
                        {
                            _criteriaItems = (from c in _criteriaItems
                                              select c.Trim()).ToList <string>();
                        }
                    }
                    else
                    {
                        if (_p.PropertyType == typeof(string))
                        {
                            string _value = _p.GetValue(_reflectedObject, null) as string;
                            _value = _value != null?_value.Trim() : _value;

                            if (criteria == "Name" || criteria == "OriginalTitle") // Name is always splitted by 1st char
                            {
                                _value = string.IsNullOrEmpty(_value) ? null :  _value.Substring(0, 1).ToUpperInvariant();
                            }
                            if (!string.IsNullOrEmpty(_value))
                            {
                                // string, use it directly (if not Rating)
                                if (criteria == "Rating")
                                {
                                    // in this case try to convert to integer and use only integer part
                                    if (_info.dRating > 0)
                                    {
                                        _value = ((int)_info.dRating).ToString();
                                    }
                                    else
                                    {
                                        _value = null;
                                    }
                                }

                                if (!string.IsNullOrEmpty(_value))
                                {
                                    _criteriaItems = new List <string>()
                                    {
                                        _value
                                    };
                                }
                            }
                        }
                    }
                }
            }

            return(_criteriaItems);
        }
 private MediaInfoData Merge(MediaInfoData original, MediaInfoData acquired)
 {
     if (original == null)
     {
         return(acquired);
     }
     if (original.PluginData.AudioBitRate == 0)
     {
         original.PluginData.AudioBitRate = acquired.AudioBitRate;
     }
     if (original.PluginData.AudioChannelCount == "")
     {
         original.PluginData.AudioChannelCount = acquired.AudioChannelCount;
     }
     if (original.PluginData.AudioFormat == "")
     {
         original.PluginData.AudioProfile = acquired.AudioProfile;
         original.PluginData.AudioFormat  = acquired.AudioFormat;
     }
     if (original.PluginData.AudioStreamCount == 0)
     {
         original.PluginData.AudioStreamCount = acquired.AudioStreamCount;
     }
     if (original.PluginData.AudioLanguages == "")
     {
         original.PluginData.AudioLanguages = acquired.AudioLanguages;
     }
     if (original.PluginData.Height == 0)
     {
         original.PluginData.Height = acquired.Height;
     }
     if (original.PluginData.RunTime == 0)
     {
         original.PluginData.RunTime = acquired.RunTime;
     }
     if (original.PluginData.Subtitles == "")
     {
         original.PluginData.Subtitles = acquired.Subtitles;
     }
     if (original.PluginData.VideoBitRate == 0)
     {
         original.PluginData.VideoBitRate = acquired.VideoBitRate;
     }
     if (original.PluginData.VideoCodec == "")
     {
         original.PluginData.VideoCodec = acquired.VideoCodec;
     }
     if (original.PluginData.VideoFPS == "")
     {
         original.PluginData.VideoFPS = acquired.VideoFPS;
     }
     if (original.PluginData.ScanType == "")
     {
         original.PluginData.ScanType = acquired.ScanType;
     }
     if (original.PluginData.Width == 0)
     {
         original.PluginData.Width = acquired.Width;
     }
     return(original);
 }
        private MediaInfoData GetMediaInfo(string location, MediaType mediaType)
        {
            Logger.ReportInfo("Getting media info from " + location);
            MediaInfoLib.MediaInfo mediaInfo = new MediaInfoLib.MediaInfo();
            mediaInfo.Option("ParseSpeed", "0.2");
            int           i             = mediaInfo.Open(location);
            MediaInfoData mediaInfoData = null;

            if (i != 0)
            {
                string subtitles = mediaInfo.Get(StreamKind.General, 0, "Text_Language_List");
                string scanType  = mediaInfo.Get(StreamKind.Video, 0, "ScanType");
                int    width;
                Int32.TryParse(mediaInfo.Get(StreamKind.Video, 0, "Width"), out width);
                int height;
                Int32.TryParse(mediaInfo.Get(StreamKind.Video, 0, "Height"), out height);
                int videoBitRate;
                Int32.TryParse(mediaInfo.Get(StreamKind.Video, 0, "BitRate"), out videoBitRate);

                int    audioBitRate;
                string aBitRate = mediaInfo.Get(StreamKind.Audio, 0, "BitRate");
                int    ABindex  = aBitRate.IndexOf(" /");
                if (ABindex > 0)
                {
                    aBitRate = aBitRate.Remove(ABindex);
                }
                Int32.TryParse(aBitRate, out audioBitRate);

                int runTime;
                Int32.TryParse(mediaInfo.Get(StreamKind.General, 0, "PlayTime"), out runTime);
                int streamCount;
                Int32.TryParse(mediaInfo.Get(StreamKind.Audio, 0, "StreamCount"), out streamCount);

                string audioChannels = mediaInfo.Get(StreamKind.Audio, 0, "Channel(s)");
                int    ACindex       = audioChannels.IndexOf(" /");
                if (ACindex > 0)
                {
                    audioChannels = audioChannels.Remove(ACindex);
                }

                string audioLanguages = mediaInfo.Get(StreamKind.General, 0, "Audio_Language_List");

                string videoFrameRate = mediaInfo.Get(StreamKind.Video, 0, "FrameRate");

                string audioProfile = mediaInfo.Get(StreamKind.Audio, 0, "Format_Profile");
                int    APindex      = audioProfile.IndexOf(" /");
                if (APindex > 0)
                {
                    audioProfile = audioProfile.Remove(APindex);
                }



                mediaInfoData = new MediaInfoData
                {
                    PluginData = new MediaInfoData.MIData()
                    {
                        VideoCodec   = mediaInfo.Get(StreamKind.Video, 0, "Codec/String"),
                        VideoBitRate = videoBitRate,
                        //MI.Get(StreamKind.Video, 0, "DisplayAspectRatio")),
                        Height = height,
                        Width  = width,
                        //MI.Get(StreamKind.Video, 0, "Duration/String3")),
                        AudioFormat       = mediaInfo.Get(StreamKind.Audio, 0, "Format"),
                        AudioBitRate      = audioBitRate,
                        RunTime           = (runTime / 60000),
                        AudioStreamCount  = streamCount,
                        AudioChannelCount = audioChannels.Trim(),
                        AudioProfile      = audioProfile.Trim(),
                        VideoFPS          = videoFrameRate,
                        AudioLanguages    = audioLanguages,
                        Subtitles         = subtitles,
                        ScanType          = scanType
                    }
                };
            }
            else
            {
                Logger.ReportInfo("Could not extract media information from " + location);
            }
            if (mediaType == MediaType.DVD && i != 0)
            {
                mediaInfo.Close();
                location = location.Replace("0.IFO", "1.vob");
                Logger.ReportInfo("Getting additional media info from " + location);
                mediaInfo.Option("ParseSpeed", "0.0");
                i = mediaInfo.Open(location);
                if (i != 0)
                {
                    int videoBitRate;
                    Int32.TryParse(mediaInfo.Get(StreamKind.Video, 0, "BitRate"), out videoBitRate);

                    int    audioBitRate;
                    string aBitRate = mediaInfo.Get(StreamKind.Audio, 0, "BitRate");
                    int    ABindex  = aBitRate.IndexOf(" /");
                    if (ABindex > 0)
                    {
                        aBitRate = aBitRate.Remove(ABindex);
                    }
                    Int32.TryParse(aBitRate, out audioBitRate);
                    string scanType = mediaInfo.Get(StreamKind.Video, 0, "ScanType");

                    mediaInfoData.PluginData.AudioBitRate = audioBitRate;
                    mediaInfoData.PluginData.VideoBitRate = videoBitRate;
                    mediaInfoData.PluginData.ScanType     = scanType;
                }
                else
                {
                    Logger.ReportInfo("Could not extract additional media info from " + location);
                }
            }
            mediaInfo.Close();
            return(mediaInfoData);
        }
        public override void Fetch()
        {
            filename = FindVideoFile();
            Video video = Item as Video;

            if (video == null)
            {
                return;
            }
            //if data exists in our file - just get it from there
            string dataFile = Item is Episode?
                              Path.Combine(Path.Combine(Path.GetDirectoryName(Item.Path), "metadata"), Path.GetFileNameWithoutExtension(Item.Path) + "-mediainfo.data") :
                                  Path.Combine(Item.Path, "mediainfo.data");

            if (File.Exists(dataFile))
            {
                Logger.ReportVerbose("Reading MediaInfo from data file...");
                using (var fs = MediaBrowser.Library.Filesystem.ProtectedFileStream.OpenSharedReader(dataFile))
                {
                    MediaInfoData data = Serializer.Deserialize <object>(fs) as MediaInfoData;
                    video.MediaInfo = Merge(video.MediaInfo, data);
                    fs.Close();
                }
                return;
            }
            if (!enabled)
            {
                Logger.ReportInfo("MediaInfo is disabled - probably due to previous error");
                return;
            }
            if (!video.ContainsRippedMedia || (Kernel.LoadContext == MBLoadContext.Service && Plugin.PluginOptions.Instance.AllowBDRips && (video.MediaType == MediaType.BluRay || video.MediaType == MediaType.DVD)))
            {
                if (video.MediaType == MediaType.Wtv)
                {
                    return;
                }
                if (Plugin.PluginOptions.Instance.BadFiles.Contains(filename))
                {
                    Logger.ReportInfo("MediaInfo not scanning known bad file: " + filename);
                    return;
                }

                using (new MediaBrowser.Util.Profiler("Media Info extraction"))
                {
                    int timeout = Kernel.LoadContext == MBLoadContext.Core ? 30000 : Plugin.ServiceTimeout; //only allow 30 seconds in core but configurable for service
                    if (filename != null)
                    {
                        try
                        {
                            var mi = GetMediaInfo(filename, video.MediaType);
                            Async.RunWithTimeout(() => video.MediaInfo = Merge(video.MediaInfo, mi), timeout);
                            //now save it in a file
                            if (Plugin.PluginOptions.Instance.SaveToFile)
                            {
                                var stream = new MemoryStream();
                                Serializer.Serialize <object>(stream, mi);
                                Kernel.IgnoreFileSystemMods = true;
                                File.WriteAllBytes(dataFile, stream.ToArray());
                                Kernel.IgnoreFileSystemMods = false;
                            }
                        }
                        catch (TimeoutException)
                        {
                            Logger.ReportError("MediaInfo extraction timed-out (" + timeout + "ms) for " + Item.Name + " file: " + filename);
                            //if this is the first timeout we've had, add this file to the bad file list
                            if (!hasTimedOut)
                            {
                                if (Kernel.LoadContext == MBLoadContext.Service)
                                {
                                    //only blacklist files that hung with the longer timeout
                                    Plugin.PluginOptions.Instance.BadFiles.Add(filename);
                                    Plugin.PluginOptions.Save();
                                }
                                hasTimedOut = true;
                                enabled     = false; //no telling how long the MI dll is going to be hung.  Best to not try it again this session
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.ReportException("Error in MediaInfoProvider.", e);
                        }
                    }
                    else
                    {
                        Logger.ReportInfo("MediaInfo unable to find file to process for " + Item.Name);
                    }
                }
                if (video.MediaInfo.RunTime > 0)
                {
                    video.RunningTime = video.MediaInfo.RunTime;
                }
            }
        }