private void ClearMetadata() { TagLib.File file = GetTagLibFile(); file.Tag.Clear(); file.RemoveTags(file.TagTypes & ~file.TagTypesOnDisk); file.RemoveTags(TagLib.TagTypes.AllTags); file.Save(); file.Dispose(); }
public static void AddMp3Tags(Uri filePath, Track track) { TagLib.Id3v2.Tag.DefaultVersion = 3; TagLib.Id3v2.Tag.ForceDefaultVersion = true; TagLib.File file = TagLib.File.Create(filePath.LocalPath); SetAlbumArt(track.CoverLink, file); file.Tag.Title = track.SongTitle; file.Tag.Performers = track.Artists.Split(','); file.Tag.Album = track.Album; file.Tag.Track = (uint)track.TrackNumber; file.Tag.Year = (uint)Convert.ToInt32(Regex.Match(track.Year, @"(\d)(\d)(\d)(\d)").Value); file.RemoveTags(file.TagTypes & ~file.TagTypesOnDisk); file.Save(); }
// Method to convert wav to mp3 and tag the mp3 and also delete original wav file void convertTag(string path, string filename, currentlyplaying cp) { // This converts the wav to mp3 var proc = new Process { StartInfo = new ProcessStartInfo { // C:\ffmpeg.exe -i 0A5gdlrpAuQqZ2iFgnqBFW.wav 0A5gdlrpAuQqZ2iFgnqBFW.mp3 FileName = @"C:\Windows\SysWOW64\cmd.exe", Arguments = @"/c C:\ffmpeg.exe -i " + path + "\\" + filename + ".wav " + path + "\\" + filename + ".mp3", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true, WorkingDirectory = path } }; proc.Start(); proc.WaitForExit(); proc.Close(); // Delete the original wav file File.Delete(path + "\\" + filename + ".wav"); // Tag the mp3 TagLib.Id3v2.Tag.DefaultVersion = 3; TagLib.Id3v2.Tag.ForceDefaultVersion = true; TagLib.File file = TagLib.File.Create(path + "\\" + filename + ".mp3"); SetAlbumArt(cp.item.album.images[0].url, file, path, filename); file.Tag.Title = cp.item.name; // Get artists string[] artists = new string[cp.item.artists.Count]; int i = 0; foreach (currentlyplaying.Artist2 artist in cp.item.artists) { artists[i] = artist.name; i++; } file.Tag.Performers = artists; file.Tag.Album = cp.item.album.name; file.Tag.Track = (uint)cp.item.track_number; file.Tag.Year = (uint)Convert.ToInt32(Regex.Match(cp.item.album.release_date, @"(\d)(\d)(\d)(\d)").Value); // MessageBox.Show(cp.item.album.release_date.Split('-')[0]); file.RemoveTags(file.TagTypes & ~file.TagTypesOnDisk); file.Save(); if (useLocalDB) { DB_Handler.insertSong(cp, path + "\\" + filename + ".mp3", true, DateTime.Now); } }
public void SetMp3Tags(string mp3Path, string artist, string title) { if (!File.Exists(mp3Path)) { throw new FileNotFoundException($"Mp# was not found at the specified path {mp3Path}. Setting of MP3 tags failed."); } using (TagLib.File file = TagLib.File.Create(mp3Path)) { file.RemoveTags(TagLib.TagTypes.AllTags); file.Save(); } using (TagLib.File file = TagLib.File.Create(mp3Path)) { file.Tag.Title = title; file.Tag.Performers = new[] { artist }; file.Tag.AlbumArtists = new[] { artist }; file.Save(); } }
private void ExecuteTagEdits() { if (!((SetDiscAndSetNumber && ID3V2Tag.DiscCount == 0 && (ID3V2Tag.Disc == 0 || ID3V2Tag.Disc == 1)) || (FramesToRemove != null && FramesToRemove.Any(f => !string.IsNullOrWhiteSpace(f)) && ID3V2Tag.Frames.Any(f => FramesToRemove.Contains(f.FrameId.ToString()))))) { return; } string editFile = MediaFile.FullName; if (EditAlternateFile) { if (string.IsNullOrWhiteSpace(EditFilePath)) { Logger.Info($"AlternatePath not specified in App.config file."); } editFile = editFile.Replace(ScanFilePath, EditFilePath); } TagLib.Id3v2.Tag.UseNumericGenres = false; if (File.Exists(editFile)) { if (SetDiscAndSetNumber && ID3V2Tag.DiscCount == 0 && (ID3V2Tag.Disc == 0 || ID3V2Tag.Disc == 1)) { Logger.Info($"Adding disc number and disc total."); using (TagLib.File tagFile = TagLib.File.Create(editFile)) { tagFile.Tag.Disc = 1; tagFile.Tag.DiscCount = 1; tagFile.Save(); } using (TagLib.File tagFile3 = TagLib.File.Create(editFile)) { TagLib.Id3v2.Tag v2Tag = (TagLib.Id3v2.Tag)tagFile3.GetTag(TagLib.TagTypes.Id3v2); ID3V2Tag = new TagLib.Id3v2.Tag(); v2Tag.CopyTo(ID3V2Tag, true); } } if (FramesToRemove != null && FramesToRemove.Any(f => !string.IsNullOrWhiteSpace(f)) && ID3V2Tag.Frames.Any(f => FramesToRemove.Contains(f.FrameId.ToString()))) { Logger.Info($"Removing extra frames from '{editFile}'."); DateTime editStart = DateTime.Now; try { TagLib.Tag tempTag = null; tempTag = new TagLib.Id3v2.Tag(); using (TagLib.File tagFile = TagLib.File.Create(editFile)) { TagLib.Id3v2.Tag v2Tag = (TagLib.Id3v2.Tag)tagFile.GetTag(TagLib.TagTypes.Id3v2); v2Tag.CopyTo(tempTag, true); tagFile.RemoveTags(TagLib.TagTypes.Id3v2); tagFile.Save(); } foreach (var frame in FramesToRemove.Where(f => !string.IsNullOrWhiteSpace(f))) { ((TagLib.Id3v2.Tag)tempTag).RemoveFrames(TagLib.ByteVector.FromString(frame, TagLib.StringType.UTF8)); } using (TagLib.File tagFile2 = TagLib.File.Create(editFile)) { tempTag.CopyTo(tagFile2.Tag, true); tagFile2.Save(); } using (TagLib.File tagFile3 = TagLib.File.Create(editFile)) { TagLib.Id3v2.Tag v2Tag = (TagLib.Id3v2.Tag)tagFile3.GetTag(TagLib.TagTypes.Id3v2); ID3V2Tag = new TagLib.Id3v2.Tag(); v2Tag.CopyTo(ID3V2Tag, true); } tempTag = null; } catch (Exception ex) { Logger.Error("", ex); } DateTime editEnd = DateTime.Now; if (Globals.VerboseLogging) { Logger.Info($"'{editFile}' ID3V2 tag edited in {(editEnd - editStart).TotalMilliseconds} milliseconds."); } } } else { Logger.Info($"Alternate file '{editFile}' does not exist."); } }
public void RemoveTag(FileInfo fi, TagLib.TagTypes type) { TagLib.File tag_file = TagLib.File.Create(fi.FullName); tag_file.RemoveTags(type); tag_file.Save(); }
private static void ReadTagsAndCreateCsv(ref int albNo, string[] files, ref StringBuilder csv, Dictionary <int, string> albums, Dictionary <int, int> albumMusicCount, Dictionary <string, int> albumTrackNames, bool isAlbum) { var cou = files.Length; for (int x = 0; x < cou; x++) { currentLoad += 1; if (System.IO.File.Exists(files[x]) && files[x].IndexOf(".mp3") > 0) { if (albums.Count > 0) { albNo = albums.Keys.Max() + 1; } var track = albNo * multiple; try { var file = TagLib.File.Create(files[x]); var album = file.Tag.Album; var title = file.Tag.Title; if (title != null) { title = FixString(title); } else { title = Path.GetFileName(files[x]); title = title.ToUpper(); title = FixString(title); } if (album != null) { album = FixString(album); if (albums.ContainsValue(album)) { albNo = albums.Where(j => j.Value == album).First().Key; if (albumTrackNames.ContainsKey(title)) { Form1 fo = new Form1(); fo.EntryOutput("File already exists, moving to next"); continue; } if (albumMusicCount.ContainsKey(albNo)) { track = albNo * multiple + (albumMusicCount[albNo] + 1); albumMusicCount[albNo] += 1; } else { track = albNo * multiple; albumMusicCount.Add(albNo, 0); } albumTrackNames.Add(title, albNo); } else { if (albumTrackNames.ContainsKey(title)) { Form1 fo = new Form1(); fo.EntryOutput("File already exists, moving to next"); continue; } albums.Add(albNo, album); track = albNo * multiple; albumMusicCount.Add(albNo, 0); albumTrackNames.Add(title, albNo); } } else { album = title; if (title != null) { if (albumTrackNames.ContainsKey(title)) { Form1 fo = new Form1(); fo.EntryOutput("File already exists, moving to next"); continue; } else { albumTrackNames.Add(title, albNo); } } albums.Add(albNo, album); track = albNo * multiple; albumMusicCount.Add(albNo, 0); } var genre = file.Tag.Genres.Length > 0 ? file.Tag.Genres[0] : "0"; if (genre != null) { genre = FixString(genre); } var artist = file.Tag.Artists.Length > 0 ? file.Tag.Artists[0] : "0"; if (artist != null) { artist = FixString(artist); } var art = 0; if (file.Tag.Pictures.Length >= 1 && isAlbum) { var bin = (byte[])(file.Tag.Pictures[0].Data.Data); art = albNo * multiple; } csv = WriteToCSV(csv, track, album, title, genre, artist, art); if (!isAlbum) { if (!System.IO.Directory.Exists(outputTrackFolder)) { System.IO.Directory.CreateDirectory(outputTrackFolder); } var newFileCopied = outputTrackFolder + track + ".mp3"; System.IO.File.Copy(files[x], newFileCopied); using (TagLib.File f = TagLib.File.Create(newFileCopied)) { f.RemoveTags(TagLib.TagTypes.Id3v1); f.RemoveTags(TagLib.TagTypes.Id3v2); f.Save(); } } } catch (Exception ex) { Form1 fo = new Form1(); fo.EntryOutput("Problem with file " + files[x] + " , moving to next"); continue; } } else { // MessageBox.Show("Invalid location!"); } } }
private static void ReadTagsAndCreateCsvForAlbum(ref int albNo, string[] files, ref StringBuilder csv, Dictionary <int, string> albums, Dictionary <int, int> albumMusicCount, Dictionary <string, int> albumTrackNames, bool isAlbum) { List <string> ImageExtensions = new List <string> { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" }; int numOfTracks = files.Where(x => x.Contains(".mp3")).Count(); files = files.OrderByDescending(x => x).ToArray(); //if (isAlbum) //{ // files = new string[1] { files[0] }; //} var cou = files.Length; if (albums.Count > 0) { albNo = albums.Keys.Max() + 1; } string album = ""; bool albumFirstEntry = true; for (int x = 0; x < cou; x++) { currentLoad += 1; if (System.IO.File.Exists(files[x]) && files[x].IndexOf(".mp3") > 0) { var track = albNo * multiple; var file = TagLib.File.Create(files[x]); if (album == "") { album = file.Tag.Album; if (cou > 2) { var firstName = ""; var secondName = ""; if (files[0].IndexOf(".mp3") > 0) { var nexF = TagLib.File.Create(files[0]); firstName = nexF.Tag.Album; } if (files[1].IndexOf(".mp3") > 0) { var nexF = TagLib.File.Create(files[1]); if (firstName == "") { firstName = nexF.Tag.Album; } else { secondName = nexF.Tag.Album; } } if (files[2].IndexOf(".mp3") > 0) { var nexF = TagLib.File.Create(files[2]); secondName = nexF.Tag.Album; } if (firstName == secondName && firstName != null && firstName != "") { album = file.Tag.Album; } else { System.IO.FileInfo fInfo = new System.IO.FileInfo(files[x]); album = fInfo.Directory.Name; } } else if (album == "" || album == null) { System.IO.FileInfo fInfo = new System.IO.FileInfo(files[x]); album = fInfo.Directory.Name; } } var title = file.Tag.Title; if (title != null) { title = FixString(title); } else { title = Path.GetFileName(files[x]); title = FixString(title); } if (album != null) { album = FixString(album); if (albums.ContainsValue(album)) { albNo = albums.Where(j => j.Value == album).First().Key; if (albumTrackNames.ContainsKey(title)) { continue; Form1 fo = new Form1(); fo.EntryOutput("File already exists, moving to next"); } if (albumMusicCount.ContainsKey(albNo)) { track = albNo * multiple + (albumMusicCount[albNo] + 1); albumMusicCount[albNo] += 1; } else { track = albNo * multiple; albumMusicCount.Add(albNo, 0); } albumTrackNames.Add(title, albNo); } else { albums.Add(albNo, album); if (albumTrackNames.ContainsKey(title)) { continue; Form1 fo = new Form1(); fo.EntryOutput("File already exists, moving to next"); } else { albumTrackNames.Add(title, albNo); track = albNo * multiple; albumMusicCount.Add(albNo, 0); } } } var genre = file.Tag.Genres.Length > 0 ? file.Tag.Genres[0] : "0"; if (genre != null) { genre = FixString(genre); } var artist = file.Tag.Artists.Length > 0 ? file.Tag.Artists[0] : "0"; if (artist != null) { artist = FixString(artist); } var art = albNo * multiple; if (albumFirstEntry) { csv = WriteToCSV(csv, track, album, title, genre, artist, art, numOfTracks, false); csv = WriteToCSV(csv, track, album, title, genre, artist, art, numOfTracks, true); albumFirstEntry = false; } else { csv = WriteToCSV(csv, track, album, title, genre, artist, art, numOfTracks, true); } if (isAlbum) { if (!System.IO.Directory.Exists(outputTrackFolder)) { System.IO.Directory.CreateDirectory(outputTrackFolder); } var newFileCopied = outputTrackFolder + track + ".mp3"; if (!File.Exists(newFileCopied)) { System.IO.File.Copy(files[x], newFileCopied); } using (TagLib.File f = TagLib.File.Create(newFileCopied)) { f.RemoveTags(TagLib.TagTypes.Id3v1); f.RemoveTags(TagLib.TagTypes.Id3v2); f.Save(); } } } else if (ImageExtensions.Contains(System.IO.Path.GetExtension(files[x]).ToUpperInvariant())) { if (!System.IO.Directory.Exists(outputArtFolder)) { System.IO.Directory.CreateDirectory(outputArtFolder); } var art = albNo * multiple; var artFileName = art + System.IO.Path.GetExtension(files[x]); var artLoc = outputArtFolder + artFileName; if (System.IO.File.Exists(artLoc)) { } else { System.IO.File.Copy(files[x], artLoc); } } } }