Exemplo n.º 1
0
        private void SetTagMainKey()
        {
            if (trackNameModel.MainKey == null)
            {
                return;
            }
            var tagKeyFrame = TextInformationFrame.Get(tag, "TKEY", true);

            tagKeyFrame.Text = new[] { trackNameModel.MainKey.ToString(KeyNotation.Sharp_M) };
            tag.AddFrame(tagKeyFrame);
        }
Exemplo n.º 2
0
        public async static Task AddTags(IMusicInformation information, string path)
        {
            var filePath = new FileInfo(path);

            if (!filePath.Exists)
            {
                return;
            }
            try
            {
                var songResult = information.WebTrack;
                using (var file = File.Create(filePath.FullName))
                {
                    Tag.DefaultVersion      = 3;
                    Tag.ForceDefaultVersion = true;
                    Tag.DefaultEncoding     = StringType.UTF8;
                    if (file == null)
                    {
                        return;
                    }
                    if (filePath.FullName.Contains("ogg"))
                    {
                        var id3V1 = file.GetTag(TagTypes.Id3v1, true);
                        if (!string.IsNullOrEmpty(songResult.SongName))
                        {
                            id3V1.Title = songResult.SongName;
                        }
                        if (!string.IsNullOrEmpty(songResult.ArtistName))
                        {
                            id3V1.Performers = songResult.ArtistName.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumName))
                        {
                            id3V1.Album = songResult.AlbumName;
                        }
                        if (songResult.TrackNum != 0)
                        {
                            id3V1.Track = Convert.ToUInt32(songResult.TrackNum);
                        }
                        id3V1.Disc = Convert.ToUInt32(songResult.Disc);
                        if (!string.IsNullOrEmpty(songResult.Year))
                        {
                            id3V1.Year = Convert.ToUInt32(songResult.Year.Substring(0, 4));
                        }
                        id3V1.Comment = "雅音FM";
                    }
                    else
                    {
                        TagLib.Tag tags;
                        if (filePath.FullName.Contains("ape"))
                        {
                            tags = file.GetTag(TagTypes.Ape, true);
                        }
                        else if (filePath.FullName.Contains("flac"))
                        {
                            tags = file.GetTag(TagTypes.FlacMetadata, true);
                        }
                        else
                        {
                            tags = (Tag)file.GetTag(TagTypes.Id3v2, true);
                        }
                        var picBasePath = Path.Combine(Environment.CurrentDirectory, "Pic");
                        if (!Directory.Exists(picBasePath))
                        {
                            Directory.CreateDirectory(picBasePath);
                        }
                        var picPath = Path.Combine(picBasePath, songResult.SongId + ".jpg");
                        try
                        {
                            if (!string.IsNullOrEmpty(songResult.PicUrl))
                            {
                                var picLocation = CommonHelper.GetLocation(songResult.PicUrl);
                                if (!string.IsNullOrEmpty(picLocation))
                                {
                                    if (!System.IO.File.Exists(picPath))
                                    {
                                        await new WebClient().DownloadFileTaskAsync(picLocation, picPath);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            CommonHelper.AddLog(ex.ToString());
                        }
                        try
                        {
                            if (System.IO.File.Exists(picPath))
                            {
                                var picture = new Picture(picPath)
                                {
                                    Description = "itwusun.com",
                                    MimeType    = MediaTypeNames.Image.Jpeg,
                                    Type        = PictureType.FrontCover
                                };
                                tags.Pictures = new IPicture[] { picture };
                            }
                        }
                        catch (Exception ex)
                        {
                            CommonHelper.AddLog(ex.ToString());
                        }
                        if (!string.IsNullOrEmpty(songResult.SongName))
                        {
                            tags.Title = songResult.SongName;
                        }
                        if (!string.IsNullOrEmpty(songResult.ArtistName))
                        {
                            tags.Performers = songResult.ArtistName.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumName))
                        {
                            tags.Album = songResult.AlbumName;
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumArtist))
                        {
                            tags.AlbumArtists = songResult.AlbumArtist.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (songResult.TrackNum != 0)
                        {
                            tags.Track = Convert.ToUInt32(songResult.TrackNum);
                        }
                        tags.Disc      = Convert.ToUInt32(songResult.Disc);
                        tags.Copyright = "雅音FM";
                        if (!string.IsNullOrEmpty(songResult.Year))
                        {
                            tags.Year = Convert.ToUInt32(songResult.Year.Substring(0, 4));
                            if (tags.TagTypes == TagTypes.Id3v2)
                            {
                                var dat = TextInformationFrame.Get((Tag)tags, "TDAT", true);
                                dat.Text = new[] { songResult.Year };
                            }
                        }
                        if (tags.TagTypes == TagTypes.Id3v2)
                        {
                            if (!string.IsNullOrEmpty(songResult.Company))
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TPUB", true);
                                cmp.Text = new[] { songResult.Company };
                            }
                            if (!string.IsNullOrEmpty(songResult.Language))
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TLAN", true);
                                cmp.Text = new[] { songResult.Language };
                            }
                            if (songResult.Length != 0)
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TLEN", true);
                                cmp.Text = new[] { songResult.Length.ToString() };
                            }
                            if (!string.IsNullOrEmpty(songResult.SongSubName))
                            {
                                var title = TextInformationFrame.Get((Tag)tags, "TIT3", true);
                                title.Text = new[] { songResult.SongSubName };
                            }
                        }
                        if (!string.IsNullOrEmpty(songResult.LrcUrl))
                        {
                            try
                            {
                                var html = await new WebClient {
                                    Encoding = Encoding.UTF8
                                }.DownloadStringTaskAsync(new Uri(songResult.LrcUrl));
                                if (!string.IsNullOrEmpty(html))
                                {
                                    html        = HttpUtility.HtmlDecode(html);
                                    html        = HttpUtility.HtmlDecode(html);
                                    tags.Lyrics = html;
                                }
                            }
                            catch (Exception)
                            {
                                //
                            }
                        }
                        try
                        {
                            System.IO.File.Delete(picPath);
                        }
                        catch (Exception)
                        {
                            //
                        }
                        // ReSharper disable once AccessToDisposedClosure
                        await Task.Run(() => file.Save());
                    }
                }
            }
            catch (Exception ex)
            {
                CommonHelper.AddLog(ex.ToString());
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// get text frame from id
 /// </summary>
 /// <param name="code">the frame id</param>
 /// <param name="create">if true creates the frame</param>
 /// <returns></returns>
 public TextInformationFrame GetTextFrame(string code, bool create)
 {
     return(TextInformationFrame.Get(tag, code, create));
 }
Exemplo n.º 4
0
        public static async Task AddTags(IMusicInformation information, string path)
        {
            var filePath = new FileInfo(path);

            if (!filePath.Exists)
            {
                return;
            }
            try
            {
                var songResult = information.WebTrack;
                using (var file = File.Create(filePath.FullName))
                {
                    Tag.DefaultVersion      = 3;
                    Tag.ForceDefaultVersion = true;
                    Tag.DefaultEncoding     = StringType.UTF8;
                    if (file == null)
                    {
                        return;
                    }
                    if (filePath.FullName.Contains("ogg"))
                    {
                        var id3V1 = file.GetTag(TagTypes.Id3v1, true);
                        if (!string.IsNullOrEmpty(songResult.SongName))
                        {
                            id3V1.Title = songResult.SongName;
                        }
                        if (!string.IsNullOrEmpty(songResult.ArtistName))
                        {
                            id3V1.Performers = songResult.ArtistName.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumName))
                        {
                            id3V1.Album = songResult.AlbumName;
                        }
                        if (songResult.TrackNum != 0)
                        {
                            id3V1.Track = Convert.ToUInt32(songResult.TrackNum);
                        }
                        id3V1.Disc = Convert.ToUInt32(songResult.Disc);
                        if (!string.IsNullOrEmpty(songResult.Year))
                        {
                            id3V1.Year = Convert.ToUInt32(songResult.Year.Substring(0, 4));
                        }
                        id3V1.Comment = "雅音FM";
                    }
                    else
                    {
                        TagLib.Tag tags;
                        if (filePath.FullName.Contains("ape"))
                        {
                            tags = file.GetTag(TagTypes.Ape, true);
                        }
                        else if (filePath.FullName.Contains("flac"))
                        {
                            tags = file.GetTag(TagTypes.FlacMetadata, true);
                        }
                        else
                        {
                            tags = (Tag)file.GetTag(TagTypes.Id3v2, true);
                        }
                        if (!string.IsNullOrEmpty(songResult.SongName))
                        {
                            tags.Title = songResult.SongName;
                        }
                        if (!string.IsNullOrEmpty(songResult.ArtistName))
                        {
                            tags.Performers = songResult.ArtistName.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumName))
                        {
                            tags.Album = songResult.AlbumName;
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumArtist))
                        {
                            tags.AlbumArtists = songResult.AlbumArtist.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (songResult.TrackNum != 0)
                        {
                            tags.Track = Convert.ToUInt32(songResult.TrackNum);
                        }
                        tags.Disc      = Convert.ToUInt32(songResult.Disc);
                        tags.Copyright = "雅音FM";
                        if (!string.IsNullOrEmpty(songResult.Year))
                        {
                            tags.Year = Convert.ToUInt32(songResult.Year.Substring(0, 4));
                            if (tags.TagTypes == TagTypes.Id3v2)
                            {
                                var dat = TextInformationFrame.Get((Tag)tags, "TDAT", true);
                                dat.Text = new[] { songResult.Year };
                            }
                        }
                        if (tags.TagTypes == TagTypes.Id3v2)
                        {
                            if (!string.IsNullOrEmpty(songResult.Company))
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TPUB", true);
                                cmp.Text = new[] { songResult.Company };
                            }
                            if (!string.IsNullOrEmpty(songResult.Language))
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TLAN", true);
                                cmp.Text = new[] { songResult.Language };
                            }
                            if (songResult.Length != 0)
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TLEN", true);
                                cmp.Text = new[] { songResult.Length.ToString() };
                            }
                            if (!string.IsNullOrEmpty(songResult.SongSubName))
                            {
                                var title = TextInformationFrame.Get((Tag)tags, "TIT3", true);
                                title.Text = new[] { songResult.SongSubName };
                            }
                        }
                        if (!string.IsNullOrEmpty(songResult.LrcUrl))
                        {
                            try
                            {
                                var html = await new WebClient {
                                    Encoding = Encoding.UTF8
                                }.DownloadStringTaskAsync(songResult.LrcUrl);
                                if (!string.IsNullOrEmpty(html))
                                {
                                    html        = HttpUtility.HtmlDecode(html);
                                    html        = HttpUtility.HtmlDecode(html);
                                    tags.Lyrics = html;
                                    if (AnyListenSettings.Instance.Config.DownLrc)
                                    {
                                        // ReSharper disable once StringLastIndexOfIsCultureSpecific.1
                                        var lrcPath = filePath.FullName.Substring(0, filePath.FullName.LastIndexOf(".")) +
                                                      ".lrc";
                                        System.IO.File.WriteAllText(lrcPath, html);
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                //
                            }
                        }

                        try
                        {
                            if (!string.IsNullOrEmpty(songResult.PicUrl))
                            {
                                var datas   = new WebClient().DownloadData(songResult.PicUrl);
                                var picture = new Picture
                                {
                                    Description = "yyfm",
                                    MimeType    = MediaTypeNames.Image.Jpeg,
                                    Type        = PictureType.FrontCover,
                                    Data        = new ByteVector(datas, datas.Length)
                                };
                                tags.Pictures = new IPicture[] { picture };
                            }
                        }
                        catch (Exception ex)
                        {
                            CommonHelper.AddLog(ex.ToString());
                        }
                        // ReSharper disable once AccessToDisposedClosure
                        await Task.Run(() => file.Save());
                    }
                }
            }
            catch (Exception ex)
            {
                CommonHelper.AddLog(ex.ToString());
            }
        }