private static WebMediaInfo DoLoadMediaInfo(string source, bool ignoreMemoryCache) { try { if (source == null || !File.Exists(source)) { Log.Warn("GetMediaInfo: File {0} doesn't exist or is not accessible", source); return(null); } // check cache if (!ignoreMemoryCache && memoryCache.ContainsKey(source)) { return(memoryCache[source]); } /* Loosely based upon MediaInfoWrapper.cs (mediaportal/Core/Player) from MediaPortal trunk r27491 as of 15 June 2011 * * Using the whole wrapper from MediaPortal is quite much porting work as it's cluttered with calls to other MP code. Referencing * MediaPortal.Core.dll also isn't an option as that pulls in a big tree of dependencies. * * TODO: Needs error handling * TODO: No support for DVDs yet * TODO: Aspect ratio doesn't work properly yet */ MediaInfo info = new MediaInfo(); info.Option("ParseSpeed", "0.3"); info.Open(source); WebMediaInfo retinfo = new WebMediaInfo(); retinfo.Container = info.Get(StreamKind.General, 0, "Format"); // video retinfo.VideoStreams = new List <WebVideoStream>(); int videoStreams = info.Count_Get(StreamKind.Video); for (int i = 0; i < videoStreams; i++) { retinfo.Duration = retinfo.Duration == 0 ? (long)StringToInt(info.Get(StreamKind.Video, i, "Duration")) : retinfo.Duration; string val = info.Get(StreamKind.Video, i, "DisplayAspectRatio"); retinfo.VideoStreams.Add(new WebVideoStream() { Codec = info.Get(StreamKind.Video, i, "Codec/String"), DisplayAspectRatio = StringToDecimal(info.Get(StreamKind.Video, i, "DisplayAspectRatio")), DisplayAspectRatioString = info.Get(StreamKind.Video, i, "DisplayAspectRatio/String"), Width = StringToInt(info.Get(StreamKind.Video, i, "Width")), Height = StringToInt(info.Get(StreamKind.Video, i, "Height")), ID = StringToInt(info.Get(StreamKind.Video, i, "ID")), Index = i }); } // audio codecs retinfo.AudioStreams = new List <WebAudioStream>(); int audioStreams = info.Count_Get(StreamKind.Audio); for (int i = 0; i < audioStreams; i++) { retinfo.Duration = retinfo.Duration == 0 ? (long)StringToInt(info.Get(StreamKind.Audio, i, "Duration")) : retinfo.Duration; retinfo.AudioStreams.Add(new WebAudioStream() { Channels = StringToInt(info.Get(StreamKind.Audio, i, "Channels")), Codec = info.Get(StreamKind.Audio, i, "Codec/String"), ID = StringToInt(info.Get(StreamKind.Audio, i, "ID")), Language = info.Get(StreamKind.Audio, i, "Language"), LanguageFull = info.Get(StreamKind.Audio, i, "Language/String"), Title = info.Get(StreamKind.Audio, i, "Title"), Index = i }); } // subtitle codecs retinfo.SubtitleStreams = new List <WebSubtitleStream>(); int subtitleStreams = info.Count_Get(StreamKind.Text); int scodecnr = 0; for (scodecnr = 0; scodecnr < subtitleStreams; scodecnr++) { retinfo.SubtitleStreams.Add(new WebSubtitleStream() { Language = info.Get(StreamKind.Text, scodecnr, "Language"), LanguageFull = info.Get(StreamKind.Text, scodecnr, "Language/String"), ID = StringToInt(info.Get(StreamKind.Text, scodecnr, "ID")), Index = scodecnr, Filename = null }); } // get max subtitle id var list = retinfo.SubtitleStreams.Select(x => x.ID); int lastId = list.Count() == 0 ? 0 : list.Max(); // standard name of external subtitle files string subfile = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + ".srt"); if (File.Exists(subfile)) { retinfo.SubtitleStreams.Add(new WebSubtitleStream() { Language = "ext", LanguageFull = "External subtitle file", ID = ++lastId, // a file with so many streams seems quite unlikely to me Index = ++scodecnr, Filename = subfile }); } // language in subtitle filename var files = Directory.GetFiles(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + ".*.srt"); foreach (var file in files) { string basename = Path.GetFileName(file).Substring(Path.GetFileNameWithoutExtension(source).Length); string tag = basename.Substring(1, basename.Length - 5); retinfo.SubtitleStreams.Add(new WebSubtitleStream() { Language = LookupCountryCode(tag), LanguageFull = tag, ID = ++lastId, Index = ++scodecnr, Filename = file }); } // return info.Close(); if (!memoryCache.ContainsKey(source)) { memoryCache.Add(source, retinfo); } else { memoryCache[source] = retinfo; } return(retinfo); } catch (Exception ex) { Log.Error("Error parsing MediaInfo for " + source, ex); return(null); } }
private static WebMediaInfo DoLoadMediaInfo(string source, bool ignoreCache) { try { if (source == null || !File.Exists(source)) { Log.Warn("GetMediaInfo: File " + source + " doesn't exist or is not accessible"); return null; } // check cache if (!ignoreCache && Cache.ContainsKey(source)) return Cache[source]; /* Loosely based upon MediaInfoWrapper.cs (mediaportal/Core/Player) from MediaPortal trunk r27491 as of 15 June 2011 * * Using the whole wrapper from MediaPortal is quite much porting work as it's cluttered with calls to other MP code. Referencing * MediaPortal.Core.dll also isn't an option as that pulls in a big tree of dependencies. * * TODO: Needs error handling * TODO: No support for DVDs yet * TODO: Aspect ratio doesn't work properly yet */ MediaInfo info = new MediaInfo(); info.Option("ParseSpeed", "0.3"); info.Open(source); WebMediaInfo retinfo = new WebMediaInfo(); retinfo.Container = info.Get(StreamKind.General, 0, "Format"); // video retinfo.VideoStreams = new List<WebVideoStream>(); int videoStreams = info.Count_Get(StreamKind.Video); for (int i = 0; i < videoStreams; i++) { retinfo.Duration = retinfo.Duration == 0 ? (long)StringToInt(info.Get(StreamKind.Video, i, "Duration")) : retinfo.Duration; string val = info.Get(StreamKind.Video, i, "DisplayAspectRatio"); retinfo.VideoStreams.Add(new WebVideoStream() { Codec = info.Get(StreamKind.Video, i, "Codec/String"), DisplayAspectRatio = StringToDecimal(info.Get(StreamKind.Video, i, "DisplayAspectRatio")), DisplayAspectRatioString = info.Get(StreamKind.Video, i, "DisplayAspectRatio/String"), Width = StringToInt(info.Get(StreamKind.Video, i, "Width")), Height = StringToInt(info.Get(StreamKind.Video, i, "Height")), ID = StringToInt(info.Get(StreamKind.Video, i, "ID")), Index = i }); } // audio codecs retinfo.AudioStreams = new List<WebAudioStream>(); int audioStreams = info.Count_Get(StreamKind.Audio); for (int i = 0; i < audioStreams; i++) { retinfo.Duration = retinfo.Duration == 0 ? (long)StringToInt(info.Get(StreamKind.Audio, i, "Duration")) : retinfo.Duration; retinfo.AudioStreams.Add(new WebAudioStream() { Channels = StringToInt(info.Get(StreamKind.Audio, i, "Channels")), Codec = info.Get(StreamKind.Audio, i, "Codec/String"), ID = StringToInt(info.Get(StreamKind.Audio, i, "ID")), Language = info.Get(StreamKind.Audio, i, "Language"), LanguageFull = info.Get(StreamKind.Audio, i, "Language/String"), Title = info.Get(StreamKind.Audio, i, "Title"), Index = i }); } // subtitle codecs retinfo.SubtitleStreams = new List<WebSubtitleStream>(); int subtitleStreams = info.Count_Get(StreamKind.Text); int scodecnr = 0; for (scodecnr = 0; scodecnr < subtitleStreams; scodecnr++) { retinfo.SubtitleStreams.Add(new WebSubtitleStream() { Language = info.Get(StreamKind.Text, scodecnr, "Language"), LanguageFull = info.Get(StreamKind.Text, scodecnr, "Language/String"), ID = StringToInt(info.Get(StreamKind.Text, scodecnr, "ID")), Index = scodecnr, Filename = null }); } // get max subtitle id var list = retinfo.SubtitleStreams.Select(x => x.ID); int lastId = list.Count() == 0 ? 0 : list.Max(); // standard name of external subtitle files string subfile = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + ".srt"); if (File.Exists(subfile)) { retinfo.SubtitleStreams.Add(new WebSubtitleStream() { Language = "ext", LanguageFull = "External subtitle file", ID = ++lastId, // a file with so many streams seems quite unlikely to me Index = ++scodecnr, Filename = subfile }); } // language in subtitle filename var files = Directory.GetFiles(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + ".*.srt"); foreach (var file in files) { string basename = Path.GetFileName(file).Substring(Path.GetFileNameWithoutExtension(source).Length); string tag = basename.Substring(1, basename.Length - 5); retinfo.SubtitleStreams.Add(new WebSubtitleStream() { Language = LookupCountryCode(tag), LanguageFull = tag, ID = ++lastId, Index = ++scodecnr, Filename = file }); } // return info.Close(); if (!Cache.ContainsKey(source)) { Cache.Add(source, retinfo); } else { Cache[source] = retinfo; } return retinfo; } catch (Exception ex) { Log.Error("Error parsing MediaInfo for " + source, ex); return null; } }
public static WebMediaInfo GetMediaInfo(string source, bool ignoreCache) { try { if (source == null || !File.Exists(source)) { Log.Warn("GetMediaInfo: File " + source + " doesn't exist or is not accessible"); return null; } // check cache if (!ignoreCache && Cache.ContainsKey(source)) return Cache[source]; /* Loosely based upon MediaInfoWrapper.cs (mediaportal/Core/Player) from MediaPortal trunk r27491 as of 15 June 2011 * * Using the whole wrapper from MediaPortal is quite much porting work as it's cluttered with calls to other MP code. Referencing * MediaPortal.Core.dll also isn't an option as that pulls in a big tree of dependencies. * * TODO: Needs error handling * TODO: No support for DVDs yet * TODO: Aspect ratio doesn't work properly yet */ MediaInfo info = new MediaInfo(); info.Option("ParseSpeed", "0.3"); info.Open(source); WebMediaInfo retinfo = new WebMediaInfo(); // video retinfo.VideoStreams = new List<WebVideoStream>(); int videoStreams = info.Count_Get(StreamKind.Video); for (int i = 0; i < videoStreams; i++) { retinfo.Duration = retinfo.Duration == 0 ? (long)StringToInt(info.Get(StreamKind.Video, i, "Duration")) : retinfo.Duration; retinfo.VideoStreams.Add(new WebVideoStream() { Codec = info.Get(StreamKind.Video, i, "Codec/String"), DisplayAspectRatio = Decimal.Parse(info.Get(StreamKind.Video, i, "DisplayAspectRatio"), System.Globalization.CultureInfo.InvariantCulture), DisplayAspectRatioString = info.Get(StreamKind.Video, i, "DisplayAspectRatio/String"), Width = StringToInt(info.Get(StreamKind.Video, i, "Width")), Height = StringToInt(info.Get(StreamKind.Video, i, "Height")), ID = StringToInt(info.Get(StreamKind.Video, i, "ID")), Index = i }); } // audio codecs retinfo.AudioStreams = new List<WebAudioStream>(); int audioStreams = info.Count_Get(StreamKind.Audio); for (int i = 0; i < audioStreams; i++) { retinfo.Duration = retinfo.Duration == 0 ? (long)StringToInt(info.Get(StreamKind.Audio, i, "Duration")) : retinfo.Duration; retinfo.AudioStreams.Add(new WebAudioStream() { Channels = StringToInt(info.Get(StreamKind.Audio, i, "Channels")), Codec = info.Get(StreamKind.Audio, i, "Codec/String"), ID = StringToInt(info.Get(StreamKind.Audio, i, "ID")), Language = info.Get(StreamKind.Audio, i, "Language"), LanguageFull = info.Get(StreamKind.Audio, i, "Language/String"), Title = info.Get(StreamKind.Audio, i, "Title"), Index = i }); } // subtitle codecs retinfo.SubtitleStreams = new List<WebSubtitleStream>(); int subtitleStreams = info.Count_Get(StreamKind.Text); int scodecnr = 0; for (scodecnr = 0; scodecnr < subtitleStreams; scodecnr++) { retinfo.SubtitleStreams.Add(new WebSubtitleStream() { Language = info.Get(StreamKind.Text, scodecnr, "Language"), LanguageFull = info.Get(StreamKind.Text, scodecnr, "Language/String"), ID = StringToInt(info.Get(StreamKind.Text, scodecnr, "ID")), Index = scodecnr, Filename = null }); } // only support usual naming convention for external files for now string subfile = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + ".srt"); if (File.Exists(subfile)) { retinfo.SubtitleStreams.Add(new WebSubtitleStream() { Language = "ext", LanguageFull = "External subtitle file", ID = 500, // a file with so many streams seems quite unlikely to me Index = ++scodecnr, Filename = subfile }); } // return info.Close(); if (!Cache.ContainsKey(source)) { Cache.Add(source, retinfo); } else { Cache[source] = retinfo; } return retinfo; } catch (Exception ex) { Log.Error("Error parsing MediaInfo for " + source, ex); return null; } }