public static void ImportMediaInfoforVideo
            (MediaInfoLib.MediaInfo mi, IMLItem item, string filesize, 
            string filecodec, int videoDuration)
        {

            if (!MeediFier.Settings.MediaInfoIsEnabled) 
                return;


          

            Debugger.LogMessageToFile("Setting item's Media Information tags...");

            try
            {
                    
                Debugger.LogMessageToFile("filesize: " + filesize);
                
                item.Tags["filesize"] = filesize;
                item.SaveTags();
                
                
                Debugger.LogMessageToFile("filecodec: " + filecodec);
                
                item.Tags["Format"] = filecodec;
                item.SaveTags();


                ImportVideoInfo(mi, item, videoDuration);

                ImportAudioInfo(mi, item);



                item.Tags["general_bitrate"] = mi.Get
                    (0, 0, "OverallBitRate");
                
                item.SaveTags();

            
            
            }
            catch (Exception e)
            {
                Debugger.LogMessageToFile("An unexpected error ocurred while importing media information. The error was:" + Environment.NewLine + e.ToString());
            }

            item.SaveTags();
        }
        private static Stream TranslateVideoStream(MediaInfoLib.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))
            {
                int www = 0;
                if (int.TryParse(s.Width, out www))
                {
                    float width = www;
                    width *= s.PA;
                    s.PixelAspectRatio = ((int) Math.Round(width)).ToString(CultureInfo.InvariantCulture) + ":" + s.Width;
                }
            }

            return s;
        }
        private static Stream TranslateTextStream(MediaInfoLib.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;
        }
        private static Stream TranslateAudioStream(MediaInfoLib.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;
        }
        private static void ImportVideoInfo
            (MediaInfoLib.MediaInfo mi, IMLItem item, int videoDuration)
        {

            Debugger.LogMessageToFile("VideoDuration: " + videoDuration);
            if (videoDuration > 0)
                item.Tags["VideoDuration"] = videoDuration + " min.";
            item.SaveTags();

            string videoCodec = mi.Get(StreamKind.Video, 0, "CodecID/Hint");
            Debugger.LogMessageToFile("video codec: " + videoCodec);
            item.Tags["video_codec"] = videoCodec;
            item.SaveTags();

            #region Video bitrate

            string videoBitrate = mi.Get(StreamKind.Video, 0, "BitRate");
            if (!String.IsNullOrEmpty(videoBitrate))
            {
                try
                {
                    int videoBitrateInt = Convert.ToInt32(videoBitrate);
                    int videoKbps = videoBitrateInt/1000;
                    item.Tags["video_bitrate"] = videoKbps + "kbps";
                }
                catch (Exception)
                {
                }
            }

            #endregion



                item.Tags["video_width"] = mi.Get
                    (StreamKind.Video, 0, "Width");
            
                item.Tags["video_height"] = mi.Get
                    (StreamKind.Video, 0, "Height");
                
                item.Tags["video_aspectratio"] = mi.Get
                    (StreamKind.Video, 0, "DisplayAspectRatio/String");
                
                item.Tags["video_framerate"] = mi.Get
                    (StreamKind.Video, 0, "FrameRate/String");

            item.Tags["video_aspectratio"] = mi.Get
                (StreamKind.Video, 0, "DisplayAspectRatio/String");

            item.Tags["video_framerate"] = mi.Get
                (StreamKind.Video, 0, "FrameRate/String");
        }
        public static void ImportMediaInfoforAudio(MediaInfoLib.MediaInfo MI, IMLItem Item, string filesize, string filecodec, int AudioDuration)
        {
            //MediaInfo MI = new MediaInfo();
            //int OpenResult = MI.Open(file.FullName);


            if (!MeediFier.Settings.MediaInfoIsEnabled) return;
            Debugger.LogMessageToFile("Setting item's Media Information tags...");

            try
            {
                Item.Tags["filesize"] = filesize;
                Item.Tags["Format"] = filecodec;

                if (AudioDuration > 0)
                    Item.Tags["AudioDuration"] = AudioDuration;

                Item.Tags["audio_codec"] = MI.Get(StreamKind.Audio, 0, "CodecID/Hint");
                Item.Tags["general_bitrate"] = MI.Get(0, 0, "OverallBitRate");
                //Item.Tags["audio_channels"] = MI.Get(StreamKind.Audio, 0, "Channel(s)");

                #region Audio bitrate
                string audioBitrate = MI.Get(StreamKind.Audio, 0, "BitRate");
                if (!String.IsNullOrEmpty(audioBitrate))
                {
                    try
                    {
                        int audioBitrateInt = Convert.ToInt32(audioBitrate);
                        int audioKbps = audioBitrateInt / 1000;
                        Debugger.LogMessageToFile("audio bitrate: " + audioKbps + "kbps");
                        Item.Tags["audio_bitrate"] = audioKbps + "kbps";
                    }
                    catch (Exception)
                    {
                    }

                }
                #endregion


                string audioLanguage = MI.Get(StreamKind.Audio, 0, "Language/String");
                Debugger.LogMessageToFile("audio language: " + audioLanguage);
                Item.Tags["audio_language"] = audioLanguage;
                Item.SaveTags();

                string audioSamplingRate = MI.Get(StreamKind.Audio, 0, "SamplingRate/String");
                Debugger.LogMessageToFile("audio sampling rate: " + audioSamplingRate);
                Item.Tags["audio_samplingrate"] = audioSamplingRate;
                Item.SaveTags();

                string audioResolution = MI.Get(StreamKind.Audio, 0, "BitDepth/String");
                Debugger.LogMessageToFile("audio resolution: " + audioResolution);
                Item.Tags["audio_resolution"] = audioResolution;
                Item.SaveTags();

            }
            catch (Exception e)
            {
                Debugger.LogMessageToFile("An unexpected error ocurred while importing media information. The error was:" + Environment.NewLine + e.ToString());
            }

            Item.SaveTags();
        }
        private static void ImportAudioInfo(MediaInfoLib.MediaInfo mi, IMLItem item)
        {

            string AudioCodec = mi.Get(StreamKind.Audio, 0, "CodecID/Hint");
            Debugger.LogMessageToFile("audio codec: " + AudioCodec);
            item.Tags["audio_codec"] = AudioCodec;
            item.SaveTags();

            string AudioChannels = mi.Get(StreamKind.Audio, 0, "Channel(s)");
            Debugger.LogMessageToFile("audio channels: " + AudioChannels);
            item.Tags["audio_channels"] = AudioChannels;
            item.SaveTags();

            string audioLanguage = mi.Get(StreamKind.Audio, 0, "Language/String");
            Debugger.LogMessageToFile("audio language: " + audioLanguage);
            item.Tags["audio_language"] = audioLanguage;
            item.SaveTags();

            string audioSamplingRate = mi.Get(StreamKind.Audio, 0, "SamplingRate/String");
            Debugger.LogMessageToFile("audio sampling rate: " + audioSamplingRate);
            item.Tags["audio_samplingrate"] = audioSamplingRate;
            item.SaveTags();

            string audioResolution = mi.Get(StreamKind.Audio, 0, "BitDepth/String");
            Debugger.LogMessageToFile("audio resolution: " + audioResolution);
            item.Tags["audio_resolution"] = audioResolution;
            item.SaveTags();


            string audioBitrate = mi.Get(StreamKind.Audio, 0, "BitRate");
            if (!String.IsNullOrEmpty(audioBitrate))
            {
                try
                {
                    int audioBitrateInt = Convert.ToInt32(audioBitrate);
                    int audioKbps = audioBitrateInt/1000;
                    item.Tags["audio_bitrate"] = audioKbps + "kbps";
                }
                catch (Exception)
                {
                }
            }
        }