private void Run(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; while (!mWorker.CancellationPending && mPendingVideos.TryDequeue(out mCurrentVideoDownload)) { var video = mCurrentVideoDownload.Info; var target = Path.Combine(mSettings.DownloadFolder, FixFilePath(video.Title + video.AudioExtension)); var audioDownloader = new AudioDownloader(video, target); ProgressEvent("Start downloading...", 0.0); ProgressEvent("Target file: " + target, 0.0); // set events const double downloadPart = 0.95; audioDownloader.DownloadProgressChanged += (s, args) => { if (worker.CancellationPending) { args.Cancel = true; } ProgressEvent(null, args.ProgressPercentage / 100.0 * downloadPart); }; audioDownloader.AudioExtractionProgressChanged += (s, args) => { if (worker.CancellationPending) { args.Cancel = true; } ProgressEvent(null, downloadPart + args.ProgressPercentage / 100.0 * (1 - downloadPart)); }; audioDownloader.DownloadFinished += (s, args) => { ProgressEvent("File downloaded!", downloadPart); ProgressEvent("Start extracting...", downloadPart); }; // Download and extract... try { audioDownloader.Execute(); }catch (Exception ex) { ProgressEvent("Error: " + ex.Message, 1.0); continue; } if (worker.CancellationPending) { mPendingVideos.Enqueue(mCurrentVideoDownload); } else { // Done ProgressEvent("Audio track extracted!", 1.0); if (mSettings.SetMediaTags) { var tags = TagLib.File.Create(target); tags.Tag.Title = mCurrentVideoDownload.Title; tags.Tag.Performers = new string[] { mCurrentVideoDownload.Artist }; tags.Save(); } if (mSettings.MoveDownloadedFilesToInbox) { ProgressEvent("Move audio file to inbox...", 1.0); mApi.Library_AddFileToLibrary(target, Plugin.LibraryCategory.Inbox); mApi.MB_RefreshPanels(); } ProgressEvent("Done.", 1.0); } mCurrentVideoDownload = null; } e.Cancel = worker.CancellationPending; }
public void AddVideo(DownloadVideo video) { mPendingVideos.Enqueue(video); Running = true; }