// Write a media-file's metadata (tags) from a SongInfo struct... // Returns true if success. //--------------------------------------------------------------------------- public bool Write(SongInfo si, string file) { bool bRet = false; // Presume error if (FilterInList(Path.GetExtension(file).ToLower())) { try { // Attribute types: // WM_TYPE_DWORD = 0; // WM_TYPE_STRING = 1; // WM_TYPE_BINARY = 2; // WM_TYPE_BOOL = 3; // WM_TYPE_QWORD = 4; // WM_TYPE_WORD = 5; // WM_TYPE_GUID = 6; using (MediaDataManager Editor = new MediaDataManager()) // WMA Editor... { // See the end of this file for a list of windows media attributes... if (si.bAlbumTag) Editor.SetAttrib(file, 0, "WM/AlbumTitle", WmTypes.WM_TYPE_STRING, si.Album); if (si.bTitleTag) Editor.SetAttrib(file, 0, "Title", WmTypes.WM_TYPE_STRING, si.Title); if (si.bArtistTag) Editor.SetAttrib(file, 0, "WM/AlbumArtist", WmTypes.WM_TYPE_STRING, si.Artist); if (si.bPerformerTag) Editor.SetAttrib(file, 0, "Author", WmTypes.WM_TYPE_STRING, si.Performer); if (si.bCommentsTag) Editor.SetAttrib(file, 0, "WM/Text", WmTypes.WM_TYPE_STRING, si.Comments); if (si.bGenreTag) Editor.SetAttrib(file, 0, "WM/Genre", WmTypes.WM_TYPE_STRING, si.Genre); if (si.bPublisherTag) Editor.SetAttrib(file, 0, "WM/Publisher", WmTypes.WM_TYPE_STRING, si.Publisher); if (si.bComposerTag) Editor.SetAttrib(file, 0, "WM/Composer", WmTypes.WM_TYPE_STRING, si.Composer); if (si.bConductorTag) Editor.SetAttrib(file, 0, "WM/Conductor", WmTypes.WM_TYPE_STRING, si.Conductor); if (si.bYearTag) Editor.SetAttrib(file, 0, "WM/Year", WmTypes.WM_TYPE_STRING, si.Year); if (si.bLyricsTag) Editor.SetAttrib(file, 0, "WM/Lyrics", WmTypes.WM_TYPE_STRING, si.Lyrics); // This is at https://picard.musicbrainz.org/docs/mappings/ if (si.bAcoustIDTag) Editor.SetAttrib(file, 0, "Acoustid/Id", WmTypes.WM_TYPE_STRING, si.AcoustID); if (si.bMBIDTag) Editor.SetAttrib(file, 0, "MusicBrainz/Track Id", WmTypes.WM_TYPE_STRING, si.MBID); // Additional Info: if (si.bCopyrightTag) Editor.SetAttrib(file, 0, "Copyright", WmTypes.WM_TYPE_STRING, si.Copyright); if (si.bDescriptionTag) Editor.SetAttrib(file, 0, "Description", WmTypes.WM_TYPE_STRING, si.Description); try { if (si.bTrackTag) Editor.SetAttrib(file, 0, "WM/TrackNumber", WmTypes.WM_TYPE_DWORD, si.Track); if (si.bBitrateTag) Editor.SetAttrib(file, 0, "Bitrate", WmTypes.WM_TYPE_DWORD, si.BitRate.ToString()); if (si.bFilesizeTag) Editor.SetAttrib(file, 0, "FileSize", WmTypes.WM_TYPE_QWORD, si.FileSize.ToString()); } catch { } // Duration is read-only! // These don't exist for windows media files... // .TrackCount = (uint)si.TrackCount; // .Disc = (uint)si.Disc; // .DiscCount = (uint)si.DiscCount; // .AudioChannels = si.Channels; // .BitsPerSample = si.BitsPerSample; // .AudioSampleRate = si.SampleRate; bRet = true; } // end using } catch { } } else try { using (TagLib.File f = TagLib.File.Create(file)) // Use taglib-sharp... { // Get the tag-frame and create it if necessary... TagLib.Id3v2.Tag tag = (TagLib.Id3v2.Tag)f.GetTag(TagLib.TagTypes.Id3v2, true); if (tag == null) return false; if (si.bAlbumTag) tag.Album = si.Album; if (si.bTitleTag) tag.Title = si.Title; if (si.bCommentsTag) tag.Comment = si.Comments; if (si.bGenreTag) { // Get list of artists, if any, and re-write string[] Genres = tag.Genres; if (Genres.Length == 0) tag.Genres = new string[] { si.Genre }; else { Genres[0] = si.Genre; tag.Genres = Genres; } } if (si.bComposerTag) { // Get list of artists, if any, and re-write string[] Composers = tag.Composers; if (Composers.Length == 0) tag.Composers = new string[] { si.Composer }; else { Composers[0] = si.Composer; tag.Composers = Composers; } } if (si.bArtistTag) { // Get list of artists, if any, and re-write string[] Artists = tag.AlbumArtists; if (Artists.Length == 0) tag.AlbumArtists = new string[] { si.Artist }; else { Artists[0] = si.Artist; tag.AlbumArtists = Artists; } } if (si.bPerformerTag) { // Get list of performers, if any, and re-write string[] Performers = tag.Performers; if (Performers.Length == 0) tag.Performers = new string[] { si.Performer }; else { Performers[0] = si.Performer; tag.Performers = Performers; } } if (si.bPublisherTag) tag.Publisher = si.Publisher; if (si.bConductorTag) tag.Conductor = si.Conductor; if (si.bYearTag) { try { tag.Year = Convert.ToUInt32(si.Year); } catch { tag.Year = 0; } } if (si.bTrackTag) { try { tag.Track = Convert.ToUInt32(si.Track); } catch { tag.Track = 0; } } // if (si.bDurationTag) // DURATION IS READ-ONLY! // Additional Info: if (si.bLyricsTag) tag.Lyrics = si.Lyrics; if (si.bAcoustIDTag) tag.MusicIpId = si.AcoustID; if (si.bMBIDTag) tag.MusicBrainzTrackId = si.MBID; if (si.bCopyrightTag) tag.Copyright = si.Copyright; if (si.bTrackCountTag) tag.TrackCount = (uint)si.TrackCount; if (si.bDiscTag) tag.Disc = (uint)si.Disc; if (si.bDiscCountTag) tag.DiscCount = (uint)si.DiscCount; // CAN'T SET THESE in taglib-sharp because they are read-only! //f.Properties.Description = si.Description; //f.Properties.AudioBitrate = si.BitRate; //f.Properties.AudioChannels = si.Channels; //f.Properties.BitsPerSample = si.BitsPerSample; //f.Properties.AudioSampleRate = si.SampleRate; //f.Length = si.FileSize; tag.CopyTo(f.Tag, true); // Overwrite the song file's tag... f.Save(); // Save tag bRet = true; } // end using } catch { } return bRet; }
// Write a media-file's metadata (tags) from a SongInfo2 struct... // Returns true if success. //--------------------------------------------------------------------------- public bool Write2(SongInfo2 si, string file) { bool bRet = false; // Presume error // ext == ".wma" || ext == ".asf" || ext == ".wmv" || ext == ".wm" if (FilterInList(Path.GetExtension(file).ToLower())) { try { using (MediaDataManager Editor = new MediaDataManager()) // WMA Editor... { // See the end of this file for a list of windows media attributes... if (si.bAlbumTag) Editor.SetAttrib(file, 0, "WM/AlbumTitle", WmTypes.WM_TYPE_STRING, si.Album); if (si.bTitleTag) Editor.SetAttrib(file, 0, "Title", WmTypes.WM_TYPE_STRING, si.Title); if (si.bArtistTag) Editor.SetAttrib(file, 0, "WM/AlbumArtist", WmTypes.WM_TYPE_STRING, si.Artist); if (si.bPerformerTag) Editor.SetAttrib(file, 0, "Author", WmTypes.WM_TYPE_STRING, si.Performer); bRet = true; } // end using } catch { } } else try { using (TagLib.File f = TagLib.File.Create(file)) // Use taglib-sharp... { // Get the tag-frame and create it if necessary... TagLib.Id3v2.Tag tag = (TagLib.Id3v2.Tag)f.GetTag(TagLib.TagTypes.Id3v2, true); if (tag == null) return false; if (si.bAlbumTag) tag.Album = si.Album; if (si.bTitleTag) tag.Title = si.Title; if (si.bArtistTag) { // Get list of artists, if any, and re-write string[] Artists = tag.AlbumArtists; if (Artists.Length == 0) tag.AlbumArtists = new string[] { si.Artist }; else { Artists[0] = si.Artist; tag.AlbumArtists = Artists; } } if (si.bPerformerTag) { // Get list of performers, if any, and re-write string[] Performers = tag.Performers; if (Performers.Length == 0) tag.Performers = new string[] { si.Performer }; else { Performers[0] = si.Performer; tag.Performers = Performers; } } tag.CopyTo(f.Tag, true); // Overwrite the song file's tag... f.Save(); // Save tag bRet = true; } // end using } catch { } return bRet; }
// // attrName is "WM/AlbumArtist", or "Author", "Title", "WM/Genre" etc // // attrType: // WM_TYPE_DWORD = 0; // WM_TYPE_STRING = 1; // WM_TYPE_BINARY = 2; // WM_TYPE_BOOL = 3; // WM_TYPE_QWORD = 4; // WM_TYPE_WORD = 5; // WM_TYPE_GUID = 6; // // newValue is always passed as a string but is converted to the specified type later! //--------------------------------------------------------------------------- public void SetValueWMA(string filePath, string attrName, ushort attrType, string newValue) { try { MediaDataManager mdm = new MediaDataManager(); // fileName, stream # (0), attr name, attr type (0-6), attr new value mdm.SetAttrib(filePath, 0, attrName, attrType, newValue); } catch { } }