private async Task ResampleAsync(MusicDb db) { db.ChangeTracker.AutoDetectChangesEnabled = false; taskItem = await db.TaskItems.FindAsync(taskId); var sources = new MusicSources(musicOptions, true); var vbrDestination = sources.FirstOrDefault(x => x.IsGenerated); if (vbrDestination != null) { var work = await db.Works.SingleAsync(x => x.UID.ToString() == taskItem.TaskString); foreach (var track in work.Tracks) { var mf = track.MusicFiles.First(f => f.Encoding == EncodingType.flac); var destinationFile = mf.File.Replace(mf.DiskRoot, vbrDestination.DiskRoot, StringComparison.CurrentCultureIgnoreCase); destinationFile = destinationFile.Replace(".flac", ".mp3", StringComparison.CurrentCultureIgnoreCase); var srcFi = new FileInfo(mf.File); var vbrFi = new FileInfo(destinationFile); if (!vbrFi.Exists || srcFi.LastWriteTimeUtc > vbrFi.LastWriteTimeUtc) { var existingVbr = track.MusicFiles.FirstOrDefault(f => f.IsGenerated); if (existingVbr != null) { track.MusicFiles.Remove(existingVbr); db.MusicFiles.Remove(existingVbr); } await Resample(srcFi.FullName, vbrFi.FullName); vbrFi.Refresh(); AddToTrack(vbrDestination, track, mf, vbrFi); } } } else { log.Warning($"{taskItem} Resampling failed as no source for generated files is available"); } taskItem.FinishedAt = DateTimeOffset.Now; taskItem.Status = Music.Core.TaskStatus.Finished; await db.SaveChangesAsync(); }
private async Task ResampleAsync(long taskId) { using (var db = new MusicDb(connectionString)) { TaskItem taskItem = null; db.ChangeTracker.AutoDetectChangesEnabled = false; taskItem = await db.TaskItems.FindAsync(taskId); var sources = new MusicSources(musicOptions, true); var vbrDestination = sources.FirstOrDefault(x => x.IsGenerated); if (vbrDestination != null) { var work = await db.Works.SingleOrDefaultAsync(x => x.UID.ToString() == taskItem.TaskString); if (work != null) { //log.Information($"Resampling {work.Artist.Name}, {work.Name}"); int resampledCount = 0; foreach (var track in work.Tracks) { var mf = track.MusicFiles.First(f => f.Encoding == EncodingType.flac); var destinationFile = mf.File.Replace(mf.DiskRoot, vbrDestination.DiskRoot, StringComparison.CurrentCultureIgnoreCase); destinationFile = destinationFile.Replace(".flac", ".mp3", StringComparison.CurrentCultureIgnoreCase); var srcFi = new FileInfo(mf.File); var vbrFi = new FileInfo(destinationFile); if (!vbrFi.Exists || srcFi.LastWriteTimeUtc > vbrFi.LastWriteTimeUtc) { await Resample(taskItem, srcFi.FullName, vbrFi.FullName); vbrFi.Refresh(); resampledCount++; } else { log.Debug($"{taskItem} {vbrFi.Name} is up to date"); } var existingVbr = track.MusicFiles.FirstOrDefault(f => f.IsGenerated); if (existingVbr != null) { track.MusicFiles.Remove(existingVbr); db.MusicFiles.Remove(existingVbr); } AddToTrack(vbrDestination, track, mf, vbrFi); await db.SaveChangesAsync(); } log.Information($"{taskItem} resampling completed {work.Artist.Name}, {work.Name}, {resampledCount}/{work.Tracks.Count()} files"); } else { log.Warning($"{taskItem} work with uid {taskItem.TaskString} not found"); taskItem.FinishedAt = DateTimeOffset.Now; taskItem.Status = Music.Core.TaskStatus.Failed; await db.SaveChangesAsync(); return; } } else { log.Warning($"{taskItem} Resampling failed as no source for generated files is available"); } taskItem.FinishedAt = DateTimeOffset.Now; taskItem.Status = Music.Core.TaskStatus.Finished; await db.SaveChangesAsync(); } }