public void Download() { try { OnDownloadStarted(new EventArgs()); using (var progressStream = new ProgressStream(TargetStream)) { var streamLength = (long)_video.StreamLength(); progressStream.BytesMoved += (s, a) => { CurrentProgress = a.StreamLength * 100 / streamLength; OnProgressChanged(new ProgressEventArgs(CurrentProgress)); }; _video.Stream().CopyTo(progressStream); } } catch (Exception e) { OnDownloadError(new ErrorEventArgs(e)); throw e; } OnDownloadFinished(new EventArgs()); }
private void Download_Vid() { finished = false; using (var service = Client.For(YouTube.Default)) { using (var video = service.GetVideo(videoURL)) { using (var outFile = File.OpenWrite(downloadPath + "/" + video.FullName)) { this.fileName = downloadPath + "/" + video.FullName; using (var ps = new ProgressStream(outFile)) { long streamLength = (long)video.StreamLength(); Stopwatch stopWatch = Stopwatch.StartNew(); ps.BytesMoved += (sender1, args) => { var percentage = args.StreamLength * 100 / streamLength; TimeSpan remaining = new TimeSpan(); this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { //update progress bar this.downloadProgressBar.Value = percentage; //update the overlayed percentage value this.downPercent.Content = percentage + "%"; //update the elapsed time this.timeElapsed.Content = stopWatch.Elapsed; //update the time remaining. double timeRemaining = (long)(stopWatch.Elapsed.TotalMilliseconds / percentage) * (100 - percentage); remaining = TimeSpan.FromMilliseconds(timeRemaining); this.timeRemaining.Content = remaining; })); }; video.Stream().CopyTo(ps); } } } } this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { this.cancelButton.IsEnabled = false; })); this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { this.doneButton.IsEnabled = true; this.audioButton.IsEnabled = true; })); }
private void SaveVideoToDisk() { Task.Factory.StartNew(() => { var CurrentFile = new FileHelper(); var Mp3Model = new Mp3Model(); using (var service = Client.For(YouTube.Default)) { if (IsWholeListChecked) { var youtubePlaylist = new YoutubePlaylist(); var playlist = youtubePlaylist.GetVideosFromPlaylist(YoutubeLinkUrl); if (playlist != null) { foreach (var audio in playlist) { using (var video = service.GetVideo(audio)) { CurrentFile.DefaultTrackName = video.FullName; CurrentFile.DefaultTrackPath = CurrentFile.Path + "\\" + CurrentFile.DefaultTrackName; CurrentFile.DefaultTrackHiddenPath = CurrentFile.HiddenPath + "\\" + CurrentFile.DefaultTrackName; CurrentFile.TmpTrackPath = CurrentFile.PreparePathForFFmpeg(CurrentFile.DefaultTrackHiddenPath); Mp3Model = new Mp3Model() { Name = CurrentFile.CheckVideoFormat(video.FullName, FormatModel.Format), IsProgressDownloadVisible = Visibility.Visible, IsPercentLabelVisible = Visibility.Visible, IsConvertingLabelVisible = Visibility.Hidden, IsOperationDoneLabelVisible = Visibility.Hidden, ConvertingLabelText = Consts.ConvertingPleaseWait, CurrentProgress = 0, }; Application.Current.Dispatcher.BeginInvoke(new Action(() => { this._mp3List.Add(Mp3Model); })); using (var outFile = File.OpenWrite(CurrentFile.TmpTrackPath)) { using (var progressStream = new ProgressStream(outFile)) { var streamLength = (long)video.StreamLength(); progressStream.BytesMoved += (sender, args) => { Mp3Model.CurrentProgress = args.StreamLength * 100 / streamLength; Debug.WriteLine($"{Mp3Model.CurrentProgress}% of video downloaded"); }; video.Stream().CopyTo(progressStream); } } BeforeConversion(Mp3Model); ExtractAudioFromVideo(CurrentFile); AfterConversion(Mp3Model, CurrentFile); } } } } else { using (var video = service.GetVideo(YoutubeLinkUrl)) { CurrentFile.DefaultTrackName = video.FullName; CurrentFile.DefaultTrackPath = CurrentFile.Path + "\\" + CurrentFile.DefaultTrackName; CurrentFile.DefaultTrackHiddenPath = CurrentFile.HiddenPath + "\\" + CurrentFile.DefaultTrackName; CurrentFile.TmpTrackPath = CurrentFile.PreparePathForFFmpeg(CurrentFile.DefaultTrackHiddenPath); Mp3Model = new Mp3Model() { Name = CurrentFile.CheckVideoFormat(video.FullName, FormatModel.Format), IsProgressDownloadVisible = Visibility.Visible, IsPercentLabelVisible = Visibility.Visible, IsConvertingLabelVisible = Visibility.Hidden, IsOperationDoneLabelVisible = Visibility.Hidden, ConvertingLabelText = Consts.ConvertingPleaseWait, CurrentProgress = 0, }; Application.Current.Dispatcher.BeginInvoke(new Action(() => { this._mp3List.Add(Mp3Model); })); using (var outFile = File.OpenWrite(CurrentFile.TmpTrackPath)) { using (var progressStream = new ProgressStream(outFile)) { var streamLength = (long)video.StreamLength(); progressStream.BytesMoved += (sender, args) => { Mp3Model.CurrentProgress = args.StreamLength * 100 / streamLength; Debug.WriteLine($"{Mp3Model.CurrentProgress}% of video downloaded"); }; video.Stream().CopyTo(progressStream); } } BeforeConversion(Mp3Model); ExtractAudioFromVideo(CurrentFile); AfterConversion(Mp3Model, CurrentFile); } } } }); }