public static Media Convert(string filename) { int ex = 0; MediaInfo mi = new MediaInfo(); if (mi == null) return null; try { if (!File.Exists(filename)) return null; } catch (Exception) { return null; } try { mi.Open(filename); ex = 1; Media m = new Media(); Part p = new Part(); Stream VideoStream = null; int video_count = mi.GetInt(StreamKind.General, 0, "VideoCount"); int audio_count = mi.GetInt(StreamKind.General, 0, "AudioCount"); int text_count = mi.GetInt(StreamKind.General, 0, "TextCount"); m.Duration = p.Duration = mi.Get(StreamKind.General, 0, "Duration"); m.Container = p.Container = TranslateContainer(mi.Get(StreamKind.General, 0, "Format")); string codid = mi.Get(StreamKind.General, 0, "CodecID"); if ((!string.IsNullOrEmpty(codid)) && (codid.Trim().ToLower() == "qt")) m.Container = p.Container= "mov"; int brate = mi.GetInt(StreamKind.General, 0, "BitRate"); if (brate != 0) m.Bitrate = Math.Round(brate / 1000F).ToString(CultureInfo.InvariantCulture); p.Size = mi.Get(StreamKind.General, 0, "FileSize"); //m.Id = p.Id = mi.Get(StreamKind.General, 0, "UniqueID"); ex = 2; List<Stream> streams = new List<Stream>(); int iidx = 0; if (video_count > 0) { for (int x = 0; x < video_count; x++) { Stream s = TranslateVideoStream(mi, x); if (x == 0) { VideoStream = s; m.Width = s.Width; m.Height = s.Height; if (!string.IsNullOrEmpty(m.Height)) { if (!string.IsNullOrEmpty(m.Width)) { m.VideoResolution = GetResolution(int.Parse(m.Width),int.Parse(m.Height)); m.AspectRatio = GetAspectRatio(float.Parse(m.Width), float.Parse(m.Height), s.PA); } } if (!string.IsNullOrEmpty(s.FrameRate)) { float fr = System.Convert.ToSingle(s.FrameRate); m.VideoFrameRate = ((int)Math.Round(fr)).ToString(CultureInfo.InvariantCulture); if (!string.IsNullOrEmpty(s.ScanType)) { if (s.ScanType.ToLower().Contains("int")) m.VideoFrameRate += "i"; else m.VideoFrameRate += "p"; } else m.VideoFrameRate += "p"; if ((m.VideoFrameRate == "25p") || (m.VideoFrameRate == "25i")) m.VideoFrameRate = "PAL"; else if ((m.VideoFrameRate == "30p") || (m.VideoFrameRate == "30i")) m.VideoFrameRate = "NTSC"; } m.VideoCodec = s.Codec; if (!string.IsNullOrEmpty(m.Duration) && !string.IsNullOrEmpty(s.Duration)) { if (int.Parse(s.Duration) > int.Parse(m.Duration)) m.Duration = p.Duration = s.Duration; } if (video_count == 1) { s.Default = null; s.Forced = null; } } if (m.Container != "mkv") { s.Index = iidx.ToString(CultureInfo.InvariantCulture); iidx++; } streams.Add(s); } } ex = 3; int totalsoundrate = 0; if (audio_count > 0) { for (int x = 0; x < audio_count; x++) { Stream s = TranslateAudioStream(mi, x); if ((s.Codec == "adpcm") && (p.Container == "flv")) s.Codec = "adpcm_swf"; if (x == 0) { m.AudioCodec = s.Codec; m.AudioChannels = s.Channels; if (!string.IsNullOrEmpty(m.Duration) && !string.IsNullOrEmpty(s.Duration)) { if (int.Parse(s.Duration) > int.Parse(m.Duration)) m.Duration = p.Duration = s.Duration; } if (audio_count == 1) { s.Default = null; s.Forced = null; } } if (!string.IsNullOrEmpty(s.Bitrate)) { totalsoundrate += int.Parse(s.Bitrate); } if (m.Container != "mkv") { s.Index = iidx.ToString(CultureInfo.InvariantCulture); iidx++; } streams.Add(s); } } if ((VideoStream!=null) && (string.IsNullOrEmpty(VideoStream.Bitrate) && (!string.IsNullOrEmpty(m.Bitrate)))) { VideoStream.Bitrate = (int.Parse(m.Bitrate) - totalsoundrate).ToString(CultureInfo.InvariantCulture); } ex = 4; if (text_count > 0) { for (int x = 0; x < audio_count; x++) { Stream s = TranslateTextStream(mi, x); streams.Add(s); if (text_count == 1) { s.Default = null; s.Forced = null; } if (m.Container != "mkv") { s.Index = iidx.ToString(CultureInfo.InvariantCulture); iidx++; } } } ex = 5; m.Parts = new List<Part>(); m.Parts.Add(p); bool over = false; if (m.Container == "mkv") { int val = int.MaxValue; foreach (Stream s in streams) { if (string.IsNullOrEmpty(s.Index)) { over = true; break; } s.idx = int.Parse(s.Index); if (s.idx < val) val = s.idx; } if ((val != 0) && (!over)) { foreach (Stream s in streams) { s.idx = s.idx - val; s.Index = s.idx.ToString(CultureInfo.InvariantCulture); } } else if (over) { int xx = 0; foreach (Stream s in streams) { s.idx = xx++; s.Index = s.idx.ToString(CultureInfo.InvariantCulture); } } streams = streams.OrderBy(a => a.idx).ToList(); } ex = 6; p.Streams = streams; if ((p.Container == "mp4") || (p.Container=="mov")) { p.Has64bitOffsets = "0"; p.OptimizedForStreaming = "0"; m.OptimizedForStreaming = "0"; byte[] buffer = new byte[8]; FileStream fs = File.OpenRead(filename); fs.Read(buffer, 0, 4); int siz = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]; fs.Seek(siz, SeekOrigin.Begin); fs.Read(buffer, 0, 8); if ((buffer[4] == 'f') && (buffer[5] == 'r') && (buffer[6] == 'e') && (buffer[7] == 'e')) { siz = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]-8; fs.Seek(siz, SeekOrigin.Current); fs.Read(buffer, 0, 8); } if ((buffer[4] == 'm') && (buffer[5] == 'o') && (buffer[6] == 'o') && (buffer[7] == 'v')) { p.OptimizedForStreaming = "1"; m.OptimizedForStreaming = "1"; siz = (buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]) - 8; buffer = new byte[siz]; fs.Read(buffer, 0, siz); int opos ; int oposmax ; if (FindInBuffer("trak", 0, siz, buffer, out opos, out oposmax)) { if (FindInBuffer("mdia", opos, oposmax, buffer, out opos, out oposmax)) { if (FindInBuffer("minf", opos, oposmax, buffer, out opos, out oposmax)) { if (FindInBuffer("stbl", opos, oposmax, buffer, out opos, out oposmax)) { if (FindInBuffer("co64", opos, oposmax, buffer, out opos, out oposmax)) { p.Has64bitOffsets = "1"; } } } } } } } ex = 7; return m; } catch (Exception e) { throw new Exception(ex+":"+e.Message,e); } finally { mi.Close(); GC.Collect(); } }
public static bool ReadMediaInfo(string fileNameFull, bool forceRefresh, ref MediaInfoResult info, bool useKodi) { try { if (!forceRefresh) { // if we have populated the full info, we have already read the data if (!string.IsNullOrEmpty(info.FullInfo)) { return(false); } } if (useKodi) { JMMContracts.KodiContracts.Media m = KodiMediaInfo.MediaConvert.Convert(fileNameFull); if (m != null) { string xml = XmlSerializeToString(m); if (!string.IsNullOrEmpty(m.Width) && !string.IsNullOrEmpty(m.Height)) { info.VideoResolution = m.Width + "x" + m.Height; } if (!string.IsNullOrEmpty(m.VideoCodec)) { info.VideoCodec = m.VideoCodec; } if (!string.IsNullOrEmpty(m.AudioCodec)) { info.AudioCodec = m.AudioCodec; } if (!string.IsNullOrEmpty(m.Duration)) { info.Duration = int.Parse(m.Duration); } List <JMMContracts.KodiContracts.Stream> vparts = m.Parts[0].Streams.Where(a => a.StreamType == "1").ToList(); if (vparts.Count > 0) { if (!string.IsNullOrEmpty(vparts[0].Bitrate)) { info.VideoBitrate = vparts[0].Bitrate; } if (!string.IsNullOrEmpty(vparts[0].BitDepth)) { info.VideoBitDepth = vparts[0].BitDepth; } if (!string.IsNullOrEmpty(vparts[0].FrameRate)) { info.VideoFrameRate = vparts[0].FrameRate; } } List <JMMContracts.KodiContracts.Stream> aparts = m.Parts[0].Streams.Where(a => a.StreamType == "2").ToList(); if (aparts.Count > 0) { if (!string.IsNullOrEmpty(aparts[0].Bitrate)) { info.AudioBitrate = aparts[0].Bitrate; } } info.FullInfo = xml; } else { logger.Error("ERROR getting kodi media info:: {0}", fileNameFull); } } else { JMMContracts.PlexContracts.Media m = PlexMediaInfo.MediaConvert.Convert(fileNameFull); if (m != null) { string xml = XmlSerializeToString(m); if (!string.IsNullOrEmpty(m.Width) && !string.IsNullOrEmpty(m.Height)) { info.VideoResolution = m.Width + "x" + m.Height; } if (!string.IsNullOrEmpty(m.VideoCodec)) { info.VideoCodec = m.VideoCodec; } if (!string.IsNullOrEmpty(m.AudioCodec)) { info.AudioCodec = m.AudioCodec; } if (!string.IsNullOrEmpty(m.Duration)) { info.Duration = int.Parse(m.Duration); } List <JMMContracts.PlexContracts.Stream> vparts = m.Parts[0].Streams.Where(a => a.StreamType == "1").ToList(); if (vparts.Count > 0) { if (!string.IsNullOrEmpty(vparts[0].Bitrate)) { info.VideoBitrate = vparts[0].Bitrate; } if (!string.IsNullOrEmpty(vparts[0].BitDepth)) { info.VideoBitDepth = vparts[0].BitDepth; } if (!string.IsNullOrEmpty(vparts[0].FrameRate)) { info.VideoFrameRate = vparts[0].FrameRate; } } List <JMMContracts.PlexContracts.Stream> aparts = m.Parts[0].Streams.Where(a => a.StreamType == "2").ToList(); if (aparts.Count > 0) { if (!string.IsNullOrEmpty(aparts[0].Bitrate)) { info.AudioBitrate = aparts[0].Bitrate; } } info.FullInfo = xml; } else { logger.Error("ERROR getting plex media info:: {0}", fileNameFull); } } } catch (Exception ex) { logger.Error("Error reading Media Info for: {0} --- {1}", fileNameFull, ex.ToString()); } return(true); }