예제 #1
0
        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);
                return(null);
            }
            finally
            {
                mi.Close();
                GC.Collect();
            }
        }
예제 #2
0
        private static Stream TranslateAudioStream(MediaInfo m, int num)
        {
            Stream s = new Stream();

            s.Id      = m.Get(StreamKind.Audio, num, "UniqueID");
            s.CodecID = (m.Get(StreamKind.Audio, num, "CodecID"));
            s.Codec   = TranslateCodec(m.Get(StreamKind.Audio, num, "Codec"));
            string title = m.Get(StreamKind.Audio, num, "Title");

            if (!string.IsNullOrEmpty(title))
            {
                s.Title = title;
            }
            s.StreamType = "2";
            string lang = m.Get(StreamKind.Audio, num, "Language/String3");

            if (!string.IsNullOrEmpty(lang))
            {
                s.LanguageCode = PostTranslateCode3(lang);
            }
            ;
            string lan = PostTranslateLan(GetLanguageFromCode3(lang, m.Get(StreamKind.Audio, num, "Language/String1")));

            if (!string.IsNullOrEmpty(lan))
            {
                s.Language = lan;
            }
            string duration = m.Get(StreamKind.Audio, num, "Duration");

            if (!string.IsNullOrEmpty(duration))
            {
                s.Duration = duration;
            }
            int brate = BiggerFromList(m.Get(StreamKind.Audio, num, "BitRate"));

            if (brate != 0)
            {
                s.Bitrate = Math.Round(brate / 1000F).ToString(CultureInfo.InvariantCulture);
            }
            int bitdepth = m.GetInt(StreamKind.Audio, num, "BitDepth");

            if (bitdepth != 0)
            {
                s.BitDepth = bitdepth.ToString(CultureInfo.InvariantCulture);
            }
            string fprofile = m.Get(StreamKind.Audio, num, "Format_Profile");

            if (!string.IsNullOrEmpty(fprofile))
            {
                if ((fprofile.ToLower() != "layer 3") && (fprofile.ToLower() != "dolby digital") && (fprofile.ToLower() != "pro") && (fprofile.ToLower() != "layer 2"))
                {
                    s.Profile = fprofile.ToLower(CultureInfo.InvariantCulture);
                }
                if (fprofile.ToLower().StartsWith("ma"))
                {
                    s.Profile = "ma";
                }
            }
            string fset = m.Get(StreamKind.Audio, num, "Format_Settings");

            if ((!string.IsNullOrEmpty(fset)) && (fset == "Little / Signed") && (s.Codec == "pcm") && (bitdepth == 16))
            {
                s.Profile = "pcm_s16le";
            }
            else if ((!string.IsNullOrEmpty(fset)) && (fset == "Big / Signed") && (s.Codec == "pcm") && (bitdepth == 16))
            {
                s.Profile = "pcm_s16be";
            }
            else if ((!string.IsNullOrEmpty(fset)) && (fset == "Little / Unsigned") && (s.Codec == "pcm") &&
                     (bitdepth == 8))
            {
                s.Profile = "pcm_u8";
            }
            string id = m.Get(StreamKind.Audio, num, "ID");

            if (!string.IsNullOrEmpty(id))
            {
                int idx;
                if (int.TryParse(id, out idx))
                {
                    s.Index = idx.ToString(CultureInfo.InvariantCulture);
                }
            }
            int pa = BiggerFromList(m.Get(StreamKind.Audio, num, "SamplingRate"));

            if (pa != 0)
            {
                s.SamplingRate = pa.ToString(CultureInfo.InvariantCulture);
            }
            int channels = BiggerFromList(m.Get(StreamKind.Audio, num, "Channel(s)"));

            if (channels != 0)
            {
                s.Channels = channels.ToString(CultureInfo.InvariantCulture);
            }
            int channelso = BiggerFromList(m.Get(StreamKind.Audio, num, "Channel(s)_Original"));

            if ((channelso != 0))
            {
                s.Channels = channelso.ToString(CultureInfo.InvariantCulture);
            }

            string bitRateMode = m.Get(StreamKind.Audio, num, "BitRate_Mode");

            if (!string.IsNullOrEmpty(bitRateMode))
            {
                s.BitrateMode = bitRateMode.ToLower(CultureInfo.InvariantCulture);
            }
            string dialnorm = m.Get(StreamKind.Audio, num, "dialnorm");

            if (!string.IsNullOrEmpty(dialnorm))
            {
                s.DialogNorm = dialnorm;
            }
            dialnorm = m.Get(StreamKind.Audio, num, "dialnorm_Average");
            if (!string.IsNullOrEmpty(dialnorm))
            {
                s.DialogNorm = dialnorm;
            }

            string def = m.Get(StreamKind.Text, num, "Default");

            if (!string.IsNullOrEmpty(def))
            {
                if (def.ToLower(CultureInfo.InvariantCulture) == "yes")
                {
                    s.Default = "1";
                }
            }
            string forced = m.Get(StreamKind.Text, num, "Forced");

            if (!string.IsNullOrEmpty(forced))
            {
                if (forced.ToLower(CultureInfo.InvariantCulture) == "yes")
                {
                    s.Forced = "1";
                }
            }
            return(s);
        }
예제 #3
0
        private static Stream TranslateTextStream(MediaInfo m, int num)
        {
            Stream s = new Stream();


            s.Id      = m.Get(StreamKind.Text, num, "UniqueID");
            s.CodecID = (m.Get(StreamKind.Text, num, "CodecID"));

            s.StreamType = "3";
            string title = m.Get(StreamKind.Text, num, "Title");

            if (!string.IsNullOrEmpty(title))
            {
                s.Title = title;
            }

            string lang = m.Get(StreamKind.Text, num, "Language/String3");

            if (!string.IsNullOrEmpty(lang))
            {
                s.LanguageCode = PostTranslateCode3(lang);
            }
            string lan = PostTranslateLan(GetLanguageFromCode3(lang, m.Get(StreamKind.Text, num, "Language/String1")));

            if (!string.IsNullOrEmpty(lan))
            {
                s.Language = lan;
            }
            string id = m.Get(StreamKind.Text, num, "ID");

            if (!string.IsNullOrEmpty(id))
            {
                int idx;
                if (int.TryParse(id, out idx))
                {
                    s.Index = idx.ToString(CultureInfo.InvariantCulture);
                }
            }
            s.Format = s.Codec = GetFormat(m.Get(StreamKind.Text, num, "CodecID"), m.Get(StreamKind.Text, num, "Format"));

            string def = m.Get(StreamKind.Text, num, "Default");

            if (!string.IsNullOrEmpty(def))
            {
                if (def.ToLower(CultureInfo.InvariantCulture) == "yes")
                {
                    s.Default = "1";
                }
            }
            string forced = m.Get(StreamKind.Text, num, "Forced");

            if (!string.IsNullOrEmpty(forced))
            {
                if (forced.ToLower(CultureInfo.InvariantCulture) == "yes")
                {
                    s.Forced = "1";
                }
            }


            return(s);
        }
예제 #4
0
        private static Stream TranslateVideoStream(MediaInfo m, int num)
        {
            Stream s = new Stream();

            s.Id         = m.Get(StreamKind.Video, num, "UniqueID");
            s.Codec      = TranslateCodec(m.Get(StreamKind.Video, num, "Codec"));
            s.CodecID    = (m.Get(StreamKind.Video, num, "CodecID"));
            s.StreamType = "1";
            s.Width      = m.Get(StreamKind.Video, num, "Width");
            string title = m.Get(StreamKind.Video, num, "Title");

            if (!string.IsNullOrEmpty(title))
            {
                s.Title = title;
            }

            string lang = m.Get(StreamKind.Video, num, "Language/String3");

            if (!string.IsNullOrEmpty(lang))
            {
                s.LanguageCode = PostTranslateCode3(lang);
            }
            string lan = PostTranslateLan(GetLanguageFromCode3(lang, m.Get(StreamKind.Video, num, "Language/String1")));

            if (!string.IsNullOrEmpty(lan))
            {
                s.Language = lan;
            }
            string duration = m.Get(StreamKind.Video, num, "Duration");

            if (!string.IsNullOrEmpty(duration))
            {
                s.Duration = duration;
            }
            s.Height = m.Get(StreamKind.Video, num, "Height");
            int brate = BiggerFromList(m.Get(StreamKind.Video, num, "BitRate"));

            if (brate != 0)
            {
                s.Bitrate = Math.Round(brate / 1000F).ToString(CultureInfo.InvariantCulture);
            }
            string stype = m.Get(StreamKind.Video, num, "ScanType");

            if (!string.IsNullOrEmpty(stype))
            {
                s.ScanType = stype.ToLower();
            }
            string refframes = m.Get(StreamKind.Video, num, "Format_Settings_RefFrames");

            if (!string.IsNullOrEmpty(refframes))
            {
                s.RefFrames = refframes;
            }
            string fprofile = m.Get(StreamKind.Video, num, "Format_Profile");

            if (!string.IsNullOrEmpty(fprofile))
            {
                int a = fprofile.ToLower(CultureInfo.InvariantCulture).IndexOf("@", StringComparison.Ordinal);
                if (a > 0)
                {
                    s.Profile = TranslateProfile(s.Codec, fprofile.ToLower(CultureInfo.InvariantCulture).Substring(0, a));
                    s.Level   = TranslateLevel(fprofile.ToLower(CultureInfo.InvariantCulture).Substring(a + 1));
                }
                else
                {
                    s.Profile = TranslateProfile(s.Codec, fprofile.ToLower(CultureInfo.InvariantCulture));
                }
            }
            string rot = m.Get(StreamKind.Video, num, "Rotation");

            if (!string.IsNullOrEmpty(rot))
            {
                float val;
                if (float.TryParse(rot, out val))
                {
                    if (val == 90F)
                    {
                        s.Orientation = "9";
                    }
                    else if (val == 180F)
                    {
                        s.Orientation = "3";
                    }
                    else if (val == 270F)
                    {
                        s.Orientation = "6";
                    }
                }
                else
                {
                    s.Orientation = rot;
                }
            }
            string muxing = m.Get(StreamKind.Video, num, "MuxingMode");

            if (!string.IsNullOrEmpty(muxing))
            {
                if (muxing.ToLower(CultureInfo.InvariantCulture).Contains("strip"))
                {
                    s.HeaderStripping = "1";
                }
            }
            string cabac = m.Get(StreamKind.Video, num, "Format_Settings_CABAC");

            if (!string.IsNullOrEmpty(cabac))
            {
                s.Cabac = cabac.ToLower(CultureInfo.InvariantCulture) == "yes" ? "1" : "0";
            }
            if (s.Codec == "h264")
            {
                if (!string.IsNullOrEmpty(s.Level) && (s.Level == "31") && (s.Cabac == null || s.Cabac == "0"))
                {
                    s.HasScalingMatrix = "1";
                }
                else
                {
                    s.HasScalingMatrix = "0";
                }
            }
            string fratemode = m.Get(StreamKind.Video, num, "FrameRate_Mode");

            if (!string.IsNullOrEmpty(fratemode))
            {
                s.FrameRateMode = fratemode.ToLower(CultureInfo.InvariantCulture);
            }
            float frate = m.GetFloat(StreamKind.Video, num, "FrameRate");

            if (frate == 0.0F)
            {
                frate = m.GetFloat(StreamKind.Video, num, "FrameRate_Original");
            }
            if (frate != 0.0F)
            {
                s.FrameRate = frate.ToString("F3");
            }
            string colorspace = m.Get(StreamKind.Video, num, "ColorSpace");

            if (!string.IsNullOrEmpty(colorspace))
            {
                s.ColorSpace = colorspace.ToLower(CultureInfo.InvariantCulture);
            }
            string chromasubsampling = m.Get(StreamKind.Video, num, "ChromaSubsampling");

            if (!string.IsNullOrEmpty(chromasubsampling))
            {
                s.ChromaSubsampling = chromasubsampling.ToLower(CultureInfo.InvariantCulture);
            }


            int bitdepth = m.GetInt(StreamKind.Video, num, "BitDepth");

            if (bitdepth != 0)
            {
                s.BitDepth = bitdepth.ToString(CultureInfo.InvariantCulture);
            }
            string id = m.Get(StreamKind.Video, num, "ID");

            if (!string.IsNullOrEmpty(id))
            {
                int idx;
                if (int.TryParse(id, out idx))
                {
                    s.Index = idx.ToString(CultureInfo.InvariantCulture);
                }
            }
            string qpel = m.Get(StreamKind.Video, num, "Format_Settings_QPel");

            if (!string.IsNullOrEmpty(qpel))
            {
                s.QPel = qpel.ToLower(CultureInfo.InvariantCulture) == "yes" ? "1" : "0";
            }
            string gmc = m.Get(StreamKind.Video, num, "Format_Settings_GMC");

            if (!string.IsNullOrEmpty(gmc))
            {
                s.GMC = gmc;
            }
            string bvop = m.Get(StreamKind.Video, num, "Format_Settings_BVOP");

            if (!string.IsNullOrEmpty(bvop) && (s.Codec != "mpeg1video"))
            {
                if (bvop == "No")
                {
                    s.BVOP = "0";
                }
                else if ((bvop == "1") || (bvop == "Yes"))
                {
                    s.BVOP = "1";
                }
            }
            string def = m.Get(StreamKind.Text, num, "Default");

            if (!string.IsNullOrEmpty(def))
            {
                if (def.ToLower(CultureInfo.InvariantCulture) == "yes")
                {
                    s.Default = "1";
                }
            }
            string forced = m.Get(StreamKind.Text, num, "Forced");

            if (!string.IsNullOrEmpty(forced))
            {
                if (forced.ToLower(CultureInfo.InvariantCulture) == "yes")
                {
                    s.Forced = "1";
                }
            }
            s.PA = m.GetFloat(StreamKind.Video, num, "PixelAspectRatio");
            string sp2 = m.Get(StreamKind.Video, num, "PixelAspectRatio_Original");

            if (!string.IsNullOrEmpty(sp2))
            {
                s.PA = System.Convert.ToSingle(sp2);
            }
            if ((s.PA != 1.0) && (!string.IsNullOrEmpty(s.Width)))
            {
                float width = int.Parse(s.Width);
                width *= s.PA;
                s.PixelAspectRatio = ((int)Math.Round(width)).ToString(CultureInfo.InvariantCulture) + ":" + s.Width;
            }

            return(s);
        }
예제 #5
0
파일: Convert.cs 프로젝트: dizzydezz/jmm
        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);
                return null;
                
            }
            finally
            {
                mi.Close();
                GC.Collect();
            }
        }
예제 #6
0
파일: Convert.cs 프로젝트: dizzydezz/jmm
        private static Stream TranslateTextStream(MediaInfo m, int num)
        {
            Stream s = new Stream();


            s.Id = m.Get(StreamKind.Text, num, "UniqueID");
            s.CodecID = (m.Get(StreamKind.Text, num, "CodecID"));

            s.StreamType = "3";
            string title = m.Get(StreamKind.Text, num, "Title");
            if (!string.IsNullOrEmpty(title))
                s.Title = title;

            string lang = m.Get(StreamKind.Text, num, "Language/String3");
            if (!string.IsNullOrEmpty(lang))
                s.LanguageCode = PostTranslateCode3(lang);
            string lan = PostTranslateLan(GetLanguageFromCode3(lang, m.Get(StreamKind.Text, num, "Language/String1")));
            if (!string.IsNullOrEmpty(lan))
                s.Language = lan;
            string id = m.Get(StreamKind.Text, num, "ID");
            if (!string.IsNullOrEmpty(id))
            {
                int idx ;
                if (int.TryParse(id, out idx))
                {
                    s.Index = idx.ToString(CultureInfo.InvariantCulture);
                }
            }
            s.Format = s.Codec=GetFormat(m.Get(StreamKind.Text, num, "CodecID"), m.Get(StreamKind.Text, num, "Format"));

            string def = m.Get(StreamKind.Text, num, "Default");
            if (!string.IsNullOrEmpty(def))
            {
                if (def.ToLower(CultureInfo.InvariantCulture) == "yes")
                    s.Default = "1";
            }
            string forced = m.Get(StreamKind.Text, num, "Forced");
            if (!string.IsNullOrEmpty(forced))
            {
                if (forced.ToLower(CultureInfo.InvariantCulture) == "yes")
                    s.Forced = "1";
            }


            return s;
        }
예제 #7
0
파일: Convert.cs 프로젝트: dizzydezz/jmm
        private static Stream TranslateAudioStream(MediaInfo m, int num)
        {
            Stream s = new Stream();
            s.Id = m.Get(StreamKind.Audio, num, "UniqueID");
            s.CodecID = (m.Get(StreamKind.Audio, num, "CodecID"));
            s.Codec = TranslateCodec(m.Get(StreamKind.Audio, num, "Codec"));
            string title = m.Get(StreamKind.Audio, num, "Title");
            if (!string.IsNullOrEmpty(title))
                s.Title = title;
            s.StreamType = "2";
            string lang = m.Get(StreamKind.Audio, num, "Language/String3");
            if (!string.IsNullOrEmpty(lang))
                s.LanguageCode = PostTranslateCode3(lang); ;
            string lan = PostTranslateLan(GetLanguageFromCode3(lang, m.Get(StreamKind.Audio, num, "Language/String1")));
            if (!string.IsNullOrEmpty(lan))
                s.Language = lan;
            string duration = m.Get(StreamKind.Audio, num, "Duration");
            if (!string.IsNullOrEmpty(duration))
                s.Duration = duration;
            int brate = BiggerFromList(m.Get(StreamKind.Audio, num, "BitRate"));
            if (brate != 0)
                s.Bitrate = Math.Round(brate / 1000F).ToString(CultureInfo.InvariantCulture);
            int bitdepth = m.GetInt(StreamKind.Audio, num, "BitDepth");
            if (bitdepth != 0)
                s.BitDepth = bitdepth.ToString(CultureInfo.InvariantCulture);
            string fprofile = m.Get(StreamKind.Audio, num, "Format_Profile");
            if (!string.IsNullOrEmpty(fprofile))
            {
                if ((fprofile.ToLower() != "layer 3") && (fprofile.ToLower() != "dolby digital") && (fprofile.ToLower() != "pro") && (fprofile.ToLower() != "layer 2"))
                    s.Profile = fprofile.ToLower(CultureInfo.InvariantCulture);
                if (fprofile.ToLower().StartsWith("ma"))
                    s.Profile = "ma";
            }
            string fset = m.Get(StreamKind.Audio, num, "Format_Settings");
            if ((!string.IsNullOrEmpty(fset)) && (fset == "Little / Signed") && (s.Codec == "pcm") && (bitdepth==16))
            {
                s.Profile = "pcm_s16le";
            }
            else if ((!string.IsNullOrEmpty(fset)) && (fset == "Big / Signed") && (s.Codec == "pcm") && (bitdepth == 16))
            {
                 s.Profile = "pcm_s16be";
            }
            else if ((!string.IsNullOrEmpty(fset)) && (fset == "Little / Unsigned") && (s.Codec == "pcm") &&
                     (bitdepth == 8))
            {
                s.Profile = "pcm_u8";
            }
            string id = m.Get(StreamKind.Audio, num, "ID");
            if (!string.IsNullOrEmpty(id))
            {
                int idx;
                if (int.TryParse(id, out idx))
                {
                    s.Index = idx.ToString(CultureInfo.InvariantCulture);
                }
            }
            int pa = BiggerFromList(m.Get(StreamKind.Audio, num, "SamplingRate"));
            if (pa!=0)
                s.SamplingRate=pa.ToString(CultureInfo.InvariantCulture);
            int channels = BiggerFromList(m.Get(StreamKind.Audio, num, "Channel(s)"));
            if (channels != 0)
                s.Channels=channels.ToString(CultureInfo.InvariantCulture);
            int channelso = BiggerFromList(m.Get(StreamKind.Audio, num, "Channel(s)_Original"));
            if ((channelso!=0))
                s.Channels = channelso.ToString(CultureInfo.InvariantCulture);
            
            string bitRateMode = m.Get(StreamKind.Audio, num, "BitRate_Mode");
            if (!string.IsNullOrEmpty(bitRateMode))
                s.BitrateMode = bitRateMode.ToLower(CultureInfo.InvariantCulture);
            string dialnorm = m.Get(StreamKind.Audio, num, "dialnorm");
            if (!string.IsNullOrEmpty(dialnorm))
                s.DialogNorm=dialnorm;
            dialnorm = m.Get(StreamKind.Audio, num, "dialnorm_Average");
            if (!string.IsNullOrEmpty(dialnorm))
                s.DialogNorm = dialnorm;

            string def = m.Get(StreamKind.Text, num, "Default");
            if (!string.IsNullOrEmpty(def))
            {
                if (def.ToLower(CultureInfo.InvariantCulture) == "yes")
                    s.Default = "1";
            }
            string forced = m.Get(StreamKind.Text, num, "Forced");
            if (!string.IsNullOrEmpty(forced))
            {
                if (forced.ToLower(CultureInfo.InvariantCulture) == "yes")
                    s.Forced = "1";
            }
            return s;
        }
예제 #8
0
파일: Convert.cs 프로젝트: dizzydezz/jmm
        private static Stream TranslateVideoStream(MediaInfo m, int num)
        {
            Stream s=new Stream();
            s.Id = m.Get(StreamKind.Video,num,"UniqueID");
            s.Codec = TranslateCodec(m.Get(StreamKind.Video, num, "Codec"));
            s.CodecID = (m.Get(StreamKind.Video, num, "CodecID"));
            s.StreamType = "1";
            s.Width = m.Get(StreamKind.Video, num, "Width");
            string title = m.Get(StreamKind.Video, num, "Title");
            if (!string.IsNullOrEmpty(title))
                s.Title = title;

            string lang = m.Get(StreamKind.Video, num, "Language/String3");
            if (!string.IsNullOrEmpty(lang))
                s.LanguageCode = PostTranslateCode3(lang);
            string lan = PostTranslateLan(GetLanguageFromCode3(lang, m.Get(StreamKind.Video, num, "Language/String1")));
            if (!string.IsNullOrEmpty(lan))
                s.Language = lan;
            string duration = m.Get(StreamKind.Video, num, "Duration");
            if (!string.IsNullOrEmpty(duration))
                s.Duration = duration;
            s.Height = m.Get(StreamKind.Video, num, "Height");
            int brate = BiggerFromList(m.Get(StreamKind.Video, num, "BitRate"));
            if (brate!=0)
                s.Bitrate = Math.Round(brate / 1000F).ToString(CultureInfo.InvariantCulture);
            string stype = m.Get(StreamKind.Video, num, "ScanType");
            if (!string.IsNullOrEmpty(stype))
                s.ScanType=stype.ToLower();
            string refframes = m.Get(StreamKind.Video, num, "Format_Settings_RefFrames");
            if (!string.IsNullOrEmpty(refframes))
                s.RefFrames = refframes;
            string fprofile = m.Get(StreamKind.Video, num, "Format_Profile");
            if (!string.IsNullOrEmpty(fprofile))
            {
                int a = fprofile.ToLower(CultureInfo.InvariantCulture).IndexOf("@", StringComparison.Ordinal);
                if (a > 0)
                {
                    s.Profile = TranslateProfile(s.Codec,fprofile.ToLower(CultureInfo.InvariantCulture).Substring(0, a));
                    s.Level = TranslateLevel(fprofile.ToLower(CultureInfo.InvariantCulture).Substring(a + 1));
                }
                else
                    s.Profile = TranslateProfile(s.Codec, fprofile.ToLower(CultureInfo.InvariantCulture));
            }
            string rot = m.Get(StreamKind.Video, num, "Rotation");
            
            if (!string.IsNullOrEmpty(rot))
            {
                float val;
                if (float.TryParse(rot, out val))
                {
                    if (val == 90F)
                        s.Orientation = "9";
                    else if (val == 180F)
                        s.Orientation = "3";
                    else if (val == 270F)
                        s.Orientation = "6";
                }
                else
                    s.Orientation = rot;
            }
            string muxing = m.Get(StreamKind.Video, num, "MuxingMode");
            if (!string.IsNullOrEmpty(muxing))
            {
                if (muxing.ToLower(CultureInfo.InvariantCulture).Contains("strip"))
                    s.HeaderStripping = "1";
            }
             string cabac = m.Get(StreamKind.Video, num, "Format_Settings_CABAC");
            if (!string.IsNullOrEmpty(cabac))
            {
                s.Cabac = cabac.ToLower(CultureInfo.InvariantCulture) == "yes" ? "1" : "0";
            }
            if (s.Codec=="h264")
            {
                if (!string.IsNullOrEmpty(s.Level) && (s.Level=="31") && (s.Cabac==null || s.Cabac=="0"))
                    s.HasScalingMatrix = "1";
                else
                    s.HasScalingMatrix = "0";
            }
            string fratemode = m.Get(StreamKind.Video, num, "FrameRate_Mode");
            if (!string.IsNullOrEmpty(fratemode))
                s.FrameRateMode = fratemode.ToLower(CultureInfo.InvariantCulture);
            float frate = m.GetFloat(StreamKind.Video, num, "FrameRate");
            if (frate==0.0F)
                frate = m.GetFloat(StreamKind.Video, num, "FrameRate_Original");
            if (frate != 0.0F)
                s.FrameRate=frate.ToString("F3");
            string colorspace = m.Get(StreamKind.Video, num, "ColorSpace");
            if (!string.IsNullOrEmpty(colorspace))
                s.ColorSpace=colorspace.ToLower(CultureInfo.InvariantCulture);
            string chromasubsampling= m.Get(StreamKind.Video, num, "ChromaSubsampling");
            if (!string.IsNullOrEmpty(chromasubsampling))
                s.ChromaSubsampling=chromasubsampling.ToLower(CultureInfo.InvariantCulture);


            int bitdepth = m.GetInt(StreamKind.Video, num, "BitDepth");
            if (bitdepth != 0)
                s.BitDepth = bitdepth.ToString(CultureInfo.InvariantCulture);
            string id = m.Get(StreamKind.Video, num, "ID");
            if (!string.IsNullOrEmpty(id))
            {
                int idx;
                if (int.TryParse(id, out idx))
                {
                    s.Index = idx.ToString(CultureInfo.InvariantCulture);
                }
            }
            string qpel = m.Get(StreamKind.Video, num, "Format_Settings_QPel");
            if (!string.IsNullOrEmpty(qpel))
            {
                s.QPel = qpel.ToLower(CultureInfo.InvariantCulture) == "yes" ? "1" : "0";
            }
            string gmc = m.Get(StreamKind.Video, num, "Format_Settings_GMC");
            if (!string.IsNullOrEmpty(gmc))
            {
                s.GMC = gmc;
            }
            string bvop = m.Get(StreamKind.Video, num, "Format_Settings_BVOP");
            if (!string.IsNullOrEmpty(bvop) && (s.Codec!="mpeg1video"))
            {
                if (bvop == "No")
                    s.BVOP = "0";
                else if ((bvop == "1") || (bvop=="Yes"))
                    s.BVOP = "1";
            }
            string def = m.Get(StreamKind.Text, num, "Default");
            if (!string.IsNullOrEmpty(def))
            {
                if (def.ToLower(CultureInfo.InvariantCulture) == "yes")
                    s.Default = "1";
            }
            string forced = m.Get(StreamKind.Text, num, "Forced");
            if (!string.IsNullOrEmpty(forced))
            {
                if (forced.ToLower(CultureInfo.InvariantCulture) == "yes")
                    s.Forced = "1";
            }
            s.PA = m.GetFloat(StreamKind.Video, num, "PixelAspectRatio");
            string sp2 = m.Get(StreamKind.Video, num, "PixelAspectRatio_Original");
            if (!string.IsNullOrEmpty(sp2))
                s.PA = System.Convert.ToSingle(sp2);
            if ((s.PA != 1.0) && (!string.IsNullOrEmpty(s.Width)))
            {
                float width = int.Parse(s.Width);
                width *= s.PA;
                s.PixelAspectRatio=((int)Math.Round(width)).ToString(CultureInfo.InvariantCulture)+":"+s.Width;
            }

            return s;
        }