public override bool Execute(ref TrackData track, int rowIndex) { Util.SendProgress(string.Format("Search coverart for {0}", track.FileName)); Log.Debug("CoverArt: Retrieving coverart for: {0} - {1}", track.Artist, track.Album); // Should we take an existing folder.jpg instead of searching the web if (Options.MainSettings.EmbedFolderThumb && !Options.MainSettings.OnlySaveFolderThumb) { if (_folderThumb == null || Path.GetDirectoryName(track.FullFileName) != _savedFolder) { _savedFolder = Path.GetDirectoryName(track.FullFileName); _folderThumb = Util.GetFolderThumb(_savedFolder); } if (_folderThumb != null) { // Only write a picture if we don't have a picture OR Overwrite Pictures is set if (track.Pictures.Count == 0 || Options.MainSettings.OverwriteExistingCovers) { if (Options.MainSettings.ChangeCoverSize && Core.Common.Picture.ImageFromData(_folderThumb.Data).Width > Options.MainSettings.MaxCoverWidth) { _folderThumb.Resize(Options.MainSettings.MaxCoverWidth); } Log.Debug("CoverArt: Using existing folder.jpg"); // First Clear all the existingPictures track.Pictures.Clear(); track.Pictures.Add(_folderThumb); TracksGrid.MainForm.SetGalleryItem(); } return true; } } // If we don't have an Album don't do any query if (track.Album == "") return true; string coverSearchString = track.Artist + track.Album; if (_isMultipleArtistAlbum) { coverSearchString = track.Album; } bool foundInCash = _savedCoverCash.ContainsKey(coverSearchString); if (foundInCash) { _album = _savedCoverCash[coverSearchString]; } else { _album = null; } // Only retrieve the Cover Art, if we don't have it yet) if (!foundInCash || _album == null) { CoverSearch dlgAlbumResults = new CoverSearch(); dlgAlbumResults.Artist = _isMultipleArtistAlbum ? "" : track.Artist; dlgAlbumResults.Album = track.Album; dlgAlbumResults.FileDetails = track.FullFileName; dlgAlbumResults.Owner = TracksGrid.MainForm; dlgAlbumResults.StartPosition = FormStartPosition.CenterParent; _album = null; DialogResult dlgResult = dlgAlbumResults.ShowDialog(); if (dlgResult == DialogResult.OK) { if (dlgAlbumResults.SelectedAlbum != null) { _album = dlgAlbumResults.SelectedAlbum; } } else if (dlgResult == DialogResult.Abort) { Log.Debug("CoverArt: Search for all albums cancelled"); ProgressCancelled = true; return false; } else { Log.Debug("CoverArt: Album Selection cancelled"); return false; } dlgAlbumResults.Dispose(); } // Now update the Cover Art if (_album != null) { if (!_savedCoverCash.ContainsKey(coverSearchString)) { _savedCoverCash.Add(coverSearchString, _album); } // Only write a picture if we don't have a picture OR Overwrite Pictures is set); if ((track.Pictures.Count == 0 || Options.MainSettings.OverwriteExistingCovers) && !Options.MainSettings.OnlySaveFolderThumb) { track.Pictures.Clear(); ByteVector vector = _album.AlbumImage; if (vector != null) { var pic = new Core.Common.Picture(); pic.MimeType = "image/jpg"; pic.Description = "Front Cover"; pic.Type = PictureType.FrontCover; pic.Data = vector.Data; if (Options.MainSettings.ChangeCoverSize && Core.Common.Picture.ImageFromData(pic.Data).Width > Options.MainSettings.MaxCoverWidth) { pic.Resize(Options.MainSettings.MaxCoverWidth); } track.Pictures.Add(pic); } // And also set the Year from the Release Date delivered by Amazon // only if not present in Track if (_album.Year != null) { string strYear = _album.Year; if (strYear.Length > 4) strYear = strYear.Substring(0, 4); int year = 0; try { year = Convert.ToInt32(strYear); } catch (Exception) { // ignored } if (year > 0 && track.Year == 0) track.Year = year; } TracksGrid.MainForm.SetGalleryItem(); } } // If the user has selected to store only the folder thumb, without touching the file if (_album != null && Options.MainSettings.OnlySaveFolderThumb) { ByteVector vector = _album.AlbumImage; if (vector != null) { string fileName = Path.Combine(Path.GetDirectoryName(track.FullFileName), "folder.jpg"); try { Core.Common.Picture pic = new Core.Common.Picture(); if (Options.MainSettings.ChangeCoverSize && Core.Common.Picture.ImageFromData(pic.Data).Width > Options.MainSettings.MaxCoverWidth) { pic.Resize(Options.MainSettings.MaxCoverWidth); } Image img = Core.Common.Picture.ImageFromData(vector.Data); // Need to make a copy, otherwise we have a GDI+ Error Bitmap bmp = new Bitmap(img); bmp.Save(fileName, ImageFormat.Jpeg); FileInfo fi = new FileInfo(fileName); TracksGrid.NonMusicFiles.RemoveAll(f => f.Name == fi.Name); TracksGrid.NonMusicFiles.Add(fi); TracksGrid.MainForm.MiscInfoPanel.AddNonMusicFiles(TracksGrid.NonMusicFiles); } catch (Exception ex) { Log.Error("Exception Saving picture: {0} {1}", fileName, ex.Message); } return false; } } return true; }
/// <summary> /// Return the folder.jpg as a Taglib.Picture /// </summary> /// <param name = "folder"></param> /// <returns></returns> public Core.Common.Picture GetFolderThumb(string folder) { string thumb = Path.Combine(folder, "folder.jpg"); if (!System.IO.File.Exists(thumb)) { return null; } try { Core.Common.Picture pic = new Core.Common.Picture(thumb); pic.Description = "Front Cover"; pic.Type = PictureType.FrontCover; return pic; } catch (Exception ex) { log.Error("Exception loading thumb file: {0} {1}", thumb, ex.Message); return null; } }
/// <summary> /// Lookup the file in Music Brainz with the Fingerprint /// </summary> /// <param name="track"></param> /// <param name="rowIndex"></param> /// <returns></returns> public override bool Execute(ref TrackData track, int rowIndex) { using (MusicBrainzTrackInfo trackinfo = new MusicBrainzTrackInfo()) { Util.SendProgress(string.Format("Identifying file {0}", track.FileName)); Log.Debug("Identify: Processing file: {0}", track.FullFileName); List<MusicBrainzTrack> musicBrainzTracks = trackinfo.GetMusicBrainzTrack(track.FullFileName); if (musicBrainzTracks == null) { Log.Debug("Identify: Couldn't identify file"); return false; } if (musicBrainzTracks.Count > 0) { MusicBrainzTrack musicBrainzTrack = null; if (musicBrainzTracks.Count == 1 && musicBrainzTracks[0].Releases.Count == 1) { musicBrainzTrack = musicBrainzTracks[0]; // Have we got already this album if (musicBrainzTrack.AlbumId == null || musicBrainzTrack.AlbumId != _musicBrainzAlbum.Id) { using (var albumInfo = new MusicBrainzAlbumInfo()) { _musicBrainzAlbum = albumInfo.GetMusicBrainzAlbumById(musicBrainzTrack.Releases[0].AlbumId); } } musicBrainzTrack.AlbumId = _musicBrainzAlbum.Id; } else { // Skip the Album selection, if the album been selected already for a previous track bool albumFound = false; foreach (var mbtrack in musicBrainzTracks) { foreach (var mbRelease in mbtrack.Releases) { if (mbRelease.AlbumId == _musicBrainzAlbum.Id) { albumFound = true; musicBrainzTrack = mbtrack; musicBrainzTrack.AlbumId = mbRelease.AlbumId; break; } } } if (!albumFound) { var dlgAlbumResults = new MusicBrainzAlbumResults(musicBrainzTracks); dlgAlbumResults.Owner = TracksGrid.MainForm; dlgAlbumResults.StartPosition = FormStartPosition.CenterParent; if (dlgAlbumResults.ShowDialog() == DialogResult.OK) { var itemTag = dlgAlbumResults.SelectedListItem as Dictionary<string, MusicBrainzTrack>; if (itemTag != null) foreach (var albumId in itemTag.Keys) { itemTag.TryGetValue(albumId, out musicBrainzTrack); if (musicBrainzTrack != null) musicBrainzTrack.AlbumId = albumId; } } dlgAlbumResults.Dispose(); } } // We didn't get a track if (musicBrainzTrack == null) { Log.Debug("Identify: No information returned from Musicbrainz"); return false; } // Are we still at the same album? // if not, get the album, so that we have the release date if (_musicBrainzAlbum.Id != musicBrainzTrack.AlbumId) { using (var albumInfo = new MusicBrainzAlbumInfo()) { Application.DoEvents(); if (ProgressCancelled) { return false; } _musicBrainzAlbum = albumInfo.GetMusicBrainzAlbumById(musicBrainzTrack.AlbumId); } } track.Title = musicBrainzTrack.Title; track.Artist = musicBrainzTrack.Artist; track.Album = _musicBrainzAlbum.Title; track.AlbumArtist = _musicBrainzAlbum.Artist; // Get the Disic and Track# from the Album foreach (var mbTrack in _musicBrainzAlbum.Tracks) { if (mbTrack.Id == musicBrainzTrack.Id) { track.TrackNumber = Convert.ToUInt32(mbTrack.Number); track.TrackCount = Convert.ToUInt32(mbTrack.TrackCount); track.DiscNumber = Convert.ToUInt32(mbTrack.DiscId); track.DiscCount = Convert.ToUInt32(_musicBrainzAlbum.DiscCount); break; } } if (_musicBrainzAlbum.Year != null && _musicBrainzAlbum.Year.Length >= 4) track.Year = Convert.ToInt32(_musicBrainzAlbum.Year.Substring(0, 4)); // Do we have a valid Amazon Album? if (_musicBrainzAlbum.Amazon != null) { // Only write a picture if we don't have a picture OR Overwrite Pictures is set if (track.Pictures.Count == 0 || Options.MainSettings.OverwriteExistingCovers) { var vector = _musicBrainzAlbum.Amazon.AlbumImage; if (vector != null) { var pic = new Core.Common.Picture(); pic.MimeType = "image/jpg"; pic.Description = ""; pic.Type = PictureType.FrontCover; pic.Data = vector.Data; track.Pictures.Add(pic); } } } return true; } } return false; }