/// <summary> /// Button click event to kick off downloading the video /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DownloadStreamDownloadButton_Click(object sender, EventArgs e) { VideoQuality videoQuality = _videoQualityFormats.VideoQualityList.Find(x => x.format == VideoQualityComboBox.Text); DownloadVOD(); }
//private void DownloadStreamForm_Load(object sender, EventArgs e) //{ //} private void testTwitchDownload() { if (_VODObject != null) { if (comboBox2.Text != string.Empty) { VideoQuality downloadQuality = _videoQualityFormats.VideoQualityList.Find(x => x.format == comboBox2.Text); NYoutubeDL.Helpers.FileSizeRate help = new NYoutubeDL.Helpers.FileSizeRate(1.0, NYoutubeDL.Helpers.Enums.ByteUnit.M); var youtubeDL = new YoutubeDL(); youtubeDL.VideoUrl = downloadQuality.url; youtubeDL.Options.DownloadOptions.LimitRate = help; string fileType = downloadQuality.extension; youtubeDL.Options.PostProcessingOptions.FfmpegLocation = "C:\\ffmpeg.exe"; youtubeDL.Options.FilesystemOptions.Output = String.Format(@"C:\Users\rgsch\Downloads\{0}_{1}.{2}", _VODObject.title, downloadQuality.format, fileType); Console.WriteLine(downloadQuality.size); int totalSeconds = 0; try { youtubeDL.StandardOutputEvent += (sender, output) => { Console.WriteLine(output); //sw.WriteLine(output); }; youtubeDL.StandardErrorEvent += (sender, errorOutput) => { Console.WriteLine(errorOutput); int timeIndex = errorOutput.IndexOf("time="); TimeSpan downloadedDuration = TimeSpan.Zero; if (timeIndex != -1) { TimeSpan.TryParse(errorOutput.Substring(timeIndex + 5, 8), out downloadedDuration); } if (timeIndex > 0) { Console.WriteLine(errorOutput.Substring(timeIndex + 5, 8)); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine((double)downloadedDuration.TotalSeconds / (double)totalSeconds); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); } }; totalSeconds = VODDuration(_VODObject.duration); youtubeDL.Download(); MessageBox.Show("Done"); } catch (Exception e) { Console.WriteLine(e.ToString()); } } } }
/// <summary> /// Downloads the VOD at the selected video quality using FFmpeg /// </summary> private void DownloadVOD() { string fileName = ""; var updateProgressBar = new Progress <int>(value => { progressBar1.Value = (value > 100) ? 100 : value; if (value >= 100) { Process.Start(System.IO.Directory.GetParent(fileName).FullName); } }); var updateLoadingMessage = new Progress <bool>(isPreparingToDownload => { if (isPreparingToDownload) { LoadingPictureBox.Image = Properties.Resources.loadingIcon; } else { LoadingPictureBox.Image = Properties.Resources.GreenCheck; } }); var updateGUIThreadDownload = updateProgressBar as IProgress <int>; var UpdateGUIThreadLoading = updateLoadingMessage as IProgress <bool>; if (_selectedVOD != null) { if (VideoQualityComboBox.Text != string.Empty) { PickVideoQualityLabel.Visible = false; //get selected video quailty VideoQuality downloadQuality = _videoQualityFormats.VideoQualityList.Find(x => x.format == VideoQualityComboBox.Text); fileName = (_selectedVOD.title + downloadQuality.format + "." + downloadQuality.extension); //replace all invalid filename chars with _ System.IO.Path.GetInvalidFileNameChars().ToList().ForEach(c => fileName = fileName.Replace(c, '_')); //get filename fileName = getSavedFileName(fileName); YoutubeDL youtubeDL = new YoutubeDL() { VideoUrl = downloadQuality.url, YoutubeDlPath = Properties.Settings.Default.YoutubeDLLocation }; youtubeDL.Options.PostProcessingOptions.FfmpegLocation = Properties.Settings.Default.MpegLocation; if (fileName != "") { youtubeDL.Options.FilesystemOptions.Output = fileName; } else { return; } try { int totalSeconds = 0; //download the VOD Task.Run(() => { youtubeDL.StandardOutputEvent += (sender, output) => { Console.WriteLine(output); }; youtubeDL.StandardErrorEvent += (sender, errorOutput) => { Console.WriteLine(errorOutput); int timeIndex = 0; TimeSpan downloadedDuration = TimeSpan.Zero; if (errorOutput.Contains("time=")) { timeIndex = errorOutput.IndexOf("time=") + 5; TimeSpan.TryParse(errorOutput.Substring(timeIndex, 8), out downloadedDuration); } if (timeIndex > 0) { updateGUIThreadDownload.Report((int)(((double)downloadedDuration.TotalSeconds / (double)totalSeconds) * 100)); } }; totalSeconds = VODDuration(_selectedVOD.duration); UpdateGUIThreadLoading.Report(true); youtubeDL.Download(); Thread.Sleep(1000); updateGUIThreadDownload?.Report(100); UpdateGUIThreadLoading.Report(false); }); } catch (Exception e) { Console.WriteLine(e.ToString()); } } else { PickVideoQualityLabel.Visible = true; } } }