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); }
/// <summary> /// Gets the value of a text frame. If the frame is a composite, the /// first frame is used; if no frames exist for the given FrameType, /// null is returned. /// </summary> /// <param name="frameType">The type of frame to get the value of.</param> /// <returns>The value of the indicated text frame, or null if there /// are no frames of the indicated type.</returns> private string GetFrameTextValue(FrameType frameType) { string textValue = null; TextInformationFrame textFrame = GetFirstFrame(frameType) as TextInformationFrame; if (textFrame != null) { textValue = textFrame.Text; } return(textValue); }
/// <summary> /// Sets the value of a text frame. If the frame is a composite, the /// first frame is used; if no frames exist for the given FrameType, /// a new frame is added; if null is passed as the value, any frames /// of the specified type are removed. /// </summary> /// <param name="frameType">The type of frame to set the value of.</param> /// <param name="textValue">The value to assign to the frame.</param> private void SetFrameTextValue(FrameType frameType, string textValue) { if (textValue == null) { this.RemoveFrame(frameType); } else { TextInformationFrame textFrame = GetFirstFrame(frameType) as TextInformationFrame; if (textFrame == null) { textFrame = (TextInformationFrame)FrameRegistry.GetNewFrame(frameType); this.AddFrame(textFrame); } textFrame.Text = textValue; } }
/// <summary> /// set text frame from id /// </summary> /// <param name="text"></param> public void SetText(string code, string text) { if (text != null) { if (code.StartsWith("T")) { TextInformationFrame frame = GetTextFrame(code, true); // add frame if (frame == null) { frame = new TextInformationFrame(code); tag.AddFrame(frame); } if (frame.Text.Length < 2) // one or zero { frame.Text = new string[1] { text }; } else // more than one { // add to front string[] strs = new string[frame.Text.Length]; strs[0] = text; Array.Copy(frame.Text, 1, strs, 1, frame.Text.Length - 1); } } else if (code.StartsWith("W")) { UnknownFrame frame = GetUnknownFrame(code); if (frame != null) { byte[] byts = UTF8Encoding.UTF8.GetBytes(text); frame.Data = new TagLib.ByteVector(byts); frame.Data.Add((byte)0); } } } }
/// <summary> /// get text from id /// </summary> /// <param name="code">the frame ifd</param> /// <returns></returns> public string GetText(string code) { if (code.StartsWith("T")) { TextInformationFrame frame = GetTextFrame(code); if (frame != null && frame.Text.Length > 0) { return(frame.Text[0]); } } else if (code.StartsWith("W")) { UnknownFrame frame = GetUnknownFrame(code); if (frame != null) { // todo //string[] strs = frame.Data.ToStrings( TagLib.StringType.UTF8 ); // gets the first frame return(frame.Data.ToString(TagLib.StringType.UTF8, 0, frame.Data.Count - 1)); } } return(string.Empty); }
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()); } }
public void DemoAdvancedUsage() { // ID3v2 Track Number frames allow you to specify not only the track number, // but the total number of tracks as well. Use a TRCKFrame to get to this // functionality. (The frame classes are named after their ID3v2.4 frame IDs, // e.g. TRCK for Track Number and PRIV for Private Use.) ID3v2Tag tag = new ID3v2Tag(); // This somewhat awkward method for creating frames does have some benefits // in other areas. TRCKFrame trackNumberFrame = (TRCKFrame)ID3v2Frame.GetNewFrame(FrameType.TrackNumber); // Add the track number data. trackNumberFrame.TrackNumber = 1; trackNumberFrame.TotalTracks = 10; // Add the frame to the tag. tag.AddFrame(trackNumberFrame); // Most of the usual frames are simple implemented as TextInformationFrame's. TextInformationFrame artistFrame = (TextInformationFrame)ID3v2Frame.GetNewFrame(FrameType.LeadArtist); artistFrame.Text = "My Artist"; tag.AddFrame(artistFrame); // Now that the artist frame is there, the Artist property will work Console.WriteLine("Artist: " + tag.Artist); // but the others still don't. if (tag.HasAlbum) { Console.WriteLine("Hmmmm....something didn't go right here."); } else { Console.WriteLine("Album (still null): " + tag.Album); } // While we're on the topic of the properties, TrackNumber works, but only returns // the track number, not the total tracks (can't fit both into one int, of course) Console.WriteLine("Track Number: " + tag.TrackNumber); // Use a Private frame (PRIVFrame) to hold some private data PRIVFrame privateFrame = (PRIVFrame)ID3v2Frame.GetNewFrame(FrameType.Private); privateFrame.OwnerIdentifier = "ID3Sharp demo"; privateFrame.PrivateData = new byte[] { 0x49, 0x44, 0x33, 0x53, 0x68, 0x61, 0x72, 0x70 }; tag.AddFrame(privateFrame); // Write the tag out to a file tag.WriteTag("advanced.tag", ID3Versions.V2_3); // and read it back. tag = ID3v2Tag.ReadTag("advanced.tag"); // Verify the contents privateFrame = (PRIVFrame)tag[FrameType.Private]; Console.Write("Private Data: "); foreach (byte dataByte in privateFrame.PrivateData) { Console.Write(dataByte); Console.Write(" (" + (char)dataByte + ") "); } Console.WriteLine(); // Remove the frames, with a few (for demonstration) different methods tag.RemoveFrame(privateFrame); tag.RemoveFrame(FrameType.LeadArtist); tag[FrameType.TrackNumber] = null; tag.WriteTag("empty.tag", ID3Versions.V2_3); // Questions? Problems? Suggestions? Post to a forum on the project site // or send me an email. }
/// <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)); }
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()); } }