Esempio n. 1
0
        private void Button_Convert_Click(object sender, RoutedEventArgs e)
        {
            ((Button)e.Source).IsEnabled = false;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var temp = FileList.Where(x => x.IsChecked).ToList();

            foreach (var _temp in temp)
            {
                Mouse.OverrideCursor = Cursors.Wait;
                stopwatch.Start();
                try
                {
                    var tfile = TagLib.File.Create(Path.Combine(_temp.Path, _temp.Name));
                    tfile.RemoveTags((Enable_ID3v1 ? TagLib.TagTypes.None : TagLib.TagTypes.Id3v1) | (Enable_ID3v2 ? TagLib.TagTypes.None : TagLib.TagTypes.Id3v2));
                    TagLib.Id3v1.Tag t  = (TagLib.Id3v1.Tag)tfile.GetTag(TagLib.TagTypes.Id3v1, Enable_ID3v1 ? true : false);
                    TagLib.Id3v2.Tag t2 = (TagLib.Id3v2.Tag)tfile.GetTag(TagLib.TagTypes.Id3v2, Enable_ID3v2 ? true : false);
                    SetID3v2Encoding(Encoding_Output_ID3v2);
                    if (t != null)
                    {
                        GetAllStringProperties(t).ForEach(x =>
                        {
                            x.Value         = StringToUnicode.TryToConvertLatin1ToUnicode(x.Value, encoding[0]);
                            x.Value_Preview = ConvertEncoding ? ConvertHelper.Convert(x.Value, encoding, ToChinese1) : ConvertHelper.Convert(x.Value, ToChinese1);
                            t.SetPropertiesValue(x.TagName, Encoding.GetEncoding("ISO-8859-1").GetString(encoding[1].GetBytes(x.Value_Preview)));
                        });
                    }
                    if (t2 != null)
                    {
                        GetAllStringProperties(t2).ForEach(x =>
                        {
                            if (tfile.TagTypesOnDisk.HasFlag(TagLib.TagTypes.Id3v2))
                            {
                                x.Value = StringToUnicode.TryToConvertLatin1ToUnicode(x.Value, encoding2[0]);
                            }
                            else
                            {
                                var _   = ID3v1_TagList.Where(y => y.TagName == x.TagName).FirstOrDefault();
                                x.Value = _ != null ? _.Value_Preview : "";
                            }
                            x.Value_Preview = ConvertHelper.Convert(x.Value, ToChinese2);
                            t2.SetPropertiesValue(x.TagName, x.Value_Preview);
                        });
                        t2.Version = (Combobox_ID3v2_Version.Text == "2.3") ? (byte)3 : (byte)4;
                    }
                    tfile.Save();
                }
                catch (TagLib.UnsupportedFormatException) { MessageBox.Show(string.Format("轉換{0}時出現錯誤,該檔案並非音訊檔", _temp.Name)); }
                catch { MessageBox.Show(string.Format("轉換{0}時出現未知錯誤", _temp.Name)); }
            }
            Mouse.OverrideCursor = null;
            stopwatch.Stop();
            if (App.Settings.Prompt)
            {
                new Toast(string.Format("轉換完成\r\n耗時:{0} ms", stopwatch.ElapsedMilliseconds)).Show();
            }
            ((Button)e.Source).IsEnabled = true;
            Listview_SelectionChanged(null, null);
        }
Esempio n. 2
0
        private async void Button_Convert_Click(object sender, RoutedEventArgs e)
        {
            ((Button)e.Source).IsEnabled = false;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var    temp         = FileList.Where(x => x.IsChecked).ToList();
            string ErrorMessage = null;

            foreach (var _temp in temp)
            {
                Mouse.OverrideCursor = Cursors.Wait;
                stopwatch.Start();
                try
                {
                    var tfile = TagLib.File.Create(Path.Combine(_temp.Path, _temp.Name));
                    tfile.RemoveTags((Enable_ID3v1 ? TagLib.TagTypes.None : TagLib.TagTypes.Id3v1) | (Enable_ID3v2 ? TagLib.TagTypes.None : TagLib.TagTypes.Id3v2));
                    TagLib.Id3v1.Tag t  = (TagLib.Id3v1.Tag)tfile.GetTag(TagLib.TagTypes.Id3v1, Enable_ID3v1 ? true : false);
                    TagLib.Id3v2.Tag t2 = (TagLib.Id3v2.Tag)tfile.GetTag(TagLib.TagTypes.Id3v2, Enable_ID3v2 ? true : false);
                    SetID3v2Encoding(Encoding_Output_ID3v2);
                    if (t != null)
                    {
                        var TagList = GetAllStringProperties(t);
                        var Dic     = TagList.ToDictionary(x => x.TagName, x => StringToUnicode.TryToConvertLatin1ToUnicode(x.Value, encoding[0]));
                        var resoult = ConvertEncoding ? await ConvertHelper.ConvertDictionary(Dic, encoding, ToChinese1) : await ConvertHelper.ConvertDictionary(Dic, ToChinese1);

                        resoult.ToList().ForEach(x => t.SetPropertiesValue(x.Key, Encoding.GetEncoding("ISO-8859-1").GetString(encoding[1].GetBytes(x.Value))));
                    }
                    if (t2 != null)
                    {
                        var TagList = GetAllStringProperties(t2);
                        var Dic     = TagList.ToDictionary(x => x.TagName, x =>
                        {
                            if (tfile.TagTypesOnDisk.HasFlag(TagLib.TagTypes.Id3v2))
                            {
                                return(StringToUnicode.TryToConvertLatin1ToUnicode(x.Value, encoding2[0]));
                            }
                            else
                            {
                                var _ = ID3v1_TagList.Where(y => y.TagName == x.TagName).FirstOrDefault();
                                return(_ != null ? _.Value_Preview : "");
                            }
                        });
                        var resoult = await ConvertHelper.ConvertDictionary(Dic, ToChinese2);

                        resoult.ToList().ForEach(x => t.SetPropertiesValue(x.Key, x.Value));
                        t2.Version = (Combobox_ID3v2_Version.Text == "2.3") ? (byte)3 : (byte)4;
                    }
                    tfile.Save();
                }
                catch (TagLib.UnsupportedFormatException) { ErrorMessage = string.Format("轉換{0}時出現錯誤,該檔案並非音訊檔", _temp.Name); }
                catch (FanhuajiException val)
                {
                    ErrorMessage = ((Exception)val).Message;
                    break;
                }
                catch { ErrorMessage = string.Format("轉換{0}時出現未知錯誤", _temp.Name); }
            }
            Mouse.OverrideCursor = null;
            stopwatch.Stop();
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                Window_MessageBoxEx.ShowDialog(ErrorMessage, "轉換過程中出現錯誤", "我知道了");
            }
            else if (App.Settings.Prompt)
            {
                new Toast(string.Format("轉換完成\r\n耗時:{0} ms", stopwatch.ElapsedMilliseconds)).Show();
            }
            ((Button)e.Source).IsEnabled = true;
            Listview_SelectionChanged(null, null);
        }
Esempio n. 3
0
        private void Preview(string path)
        {
            if (!File.Exists(path))
            {
                return;
            }
            try
            {
                var tfile           = TagLib.File.Create(path, TagLib.ReadStyle.None);
                TagLib.Id3v1.Tag t  = (TagLib.Id3v1.Tag)tfile.GetTag(TagLib.TagTypes.Id3v1);
                TagLib.Id3v2.Tag t2 = (TagLib.Id3v2.Tag)tfile.GetTag(TagLib.TagTypes.Id3v2);

                ID3v1_TagList.Clear();
                ID3v2_TagList.Clear();

                GetAllStringProperties(t).ForEach(x =>
                {
                    x.Value         = StringToUnicode.TryToConvertLatin1ToUnicode(x.Value, encoding[0]);
                    x.Value_Preview = ConvertEncoding ? ConvertHelper.Convert(x.Value, encoding, ToChinese1) : ConvertHelper.Convert(x.Value, ToChinese1);
                    ID3v1_TagList.Add(x);
                });

                GetAllStringProperties(t2).ForEach(x =>
                {
                    if (tfile.TagTypesOnDisk.HasFlag(TagLib.TagTypes.Id3v2))
                    {
                        x.Value = StringToUnicode.TryToConvertLatin1ToUnicode(x.Value, encoding2[0]);
                    }
                    else
                    {
                        var _   = ID3v1_TagList.Where(y => y.TagName == x.TagName).FirstOrDefault();
                        x.Value = _ != null ? _.Value_Preview : "";
                    }
                    x.Value_Preview = ConvertHelper.Convert(x.Value, ToChinese2);
                    ID3v2_TagList.Add(x);
                });
            }
            catch (TagLib.UnsupportedFormatException)
            {
                ID3v1_TagList.Clear();
                ID3v2_TagList.Clear();
                ID3v1_TagList.Add(new TagList_Line()
                {
                    TagName = "Error", Value = "非音訊檔"
                });
                ID3v2_TagList.Add(new TagList_Line()
                {
                    TagName = "Error", Value = "非音訊檔"
                });
            }
            catch (System.Exception)
            {
                ID3v1_TagList.Clear();
                ID3v2_TagList.Clear();
                ID3v1_TagList.Add(new TagList_Line()
                {
                    TagName = "Error", Value = "未知"
                });
                ID3v2_TagList.Add(new TagList_Line()
                {
                    TagName = "Error", Value = "未知"
                });
            }
        }
Esempio n. 4
0
        private async void Preview(string path)
        {
            if (!File.Exists(path))
            {
                return;
            }
            try
            {
                var tfile           = TagLib.File.Create(path, TagLib.ReadStyle.None);
                TagLib.Id3v1.Tag t  = (TagLib.Id3v1.Tag)tfile.GetTag(TagLib.TagTypes.Id3v1);
                TagLib.Id3v2.Tag t2 = (TagLib.Id3v2.Tag)tfile.GetTag(TagLib.TagTypes.Id3v2);

                ID3v1_TagList.Clear();
                ID3v2_TagList.Clear();

                var TagList = GetAllStringProperties(t);
                TagList.ForEach(x => x.Value = StringToUnicode.TryToConvertLatin1ToUnicode(x.Value, encoding[0]));
                var Dic     = TagList.ToDictionary(x => x.TagName, x => x.Value);
                var resoult = ConvertEncoding ? await ConvertHelper.ConvertDictionary(Dic, encoding, ToChinese1) : await ConvertHelper.ConvertDictionary(Dic, ToChinese1);

                TagList.ForEach(x =>
                {
                    x.Value_Preview = resoult[x.TagName];
                    ID3v1_TagList.Add(x);
                });


                TagList = GetAllStringProperties(t2);
                Dic     = TagList.ToDictionary(x => x.TagName, x =>
                {
                    if (tfile.TagTypesOnDisk.HasFlag(TagLib.TagTypes.Id3v2))
                    {
                        return(StringToUnicode.TryToConvertLatin1ToUnicode(x.Value, encoding2[0]));
                    }
                    else
                    {
                        var _ = ID3v1_TagList.Where(y => y.TagName == x.TagName).FirstOrDefault();
                        return(_ != null ? _.Value_Preview : "");
                    }
                });
                resoult = await ConvertHelper.ConvertDictionary(Dic, ToChinese2);

                TagList.ForEach(x =>
                {
                    x.Value_Preview = resoult[x.TagName];
                    ID3v2_TagList.Add(x);
                });
            }
            catch (TagLib.UnsupportedFormatException)
            {
                ID3v1_TagList.Clear();
                ID3v2_TagList.Clear();
                ID3v1_TagList.Add(new TagList_Line()
                {
                    TagName = "Error", Value = "非音訊檔"
                });
                ID3v2_TagList.Add(new TagList_Line()
                {
                    TagName = "Error", Value = "非音訊檔"
                });
            }
            catch (FanhuajiException val)
            {
                ID3v1_TagList.Clear();
                ID3v2_TagList.Clear();
                ID3v1_TagList.Add(new TagList_Line
                {
                    TagName = "Error",
                    Value   = val.Message
                });
                ID3v2_TagList.Add(new TagList_Line
                {
                    TagName = "Error",
                    Value   = val.Message
                });
            }
            catch (System.Exception)
            {
                ID3v1_TagList.Clear();
                ID3v2_TagList.Clear();
                ID3v1_TagList.Add(new TagList_Line()
                {
                    TagName = "Error", Value = "未知"
                });
                ID3v2_TagList.Add(new TagList_Line()
                {
                    TagName = "Error", Value = "未知"
                });
            }
        }