private void VideoData(string link, bool playlist = false) { var video = youTube.GetAllVideos(link); // gets a Video object with info about the video var resolutions = video.Where(j => j.AdaptiveKind == AdaptiveKind.Video && j.Format == VideoFormat.Mp4).Select(j => j.Resolution); var bitRates = video.Where(j => j.AdaptiveKind == AdaptiveKind.Audio).Select(j => j.AudioBitrate); foreach (var val in resolutions) { Quality.Invoke((MethodInvoker)(() => Quality.Items.Add(val))); } foreach (var val in bitRates) { AudioQuality.Invoke((MethodInvoker)(() => AudioQuality.Items.Add(val))); } if (Quality.Items.Count > 0) { Quality.Invoke((MethodInvoker)(() => Quality.Sorted = true)); Quality.Invoke((MethodInvoker)(() => Quality.SelectedIndex = 0)); AudioQuality.Invoke((MethodInvoker)(() => AudioQuality.Sorted = true)); AudioQuality.Invoke((MethodInvoker)(() => AudioQuality.SelectedIndex = 0)); Vid.Invoke((MethodInvoker)(() => Vid.Enabled = true)); VidTitle.Invoke((MethodInvoker)(() => VidTitle.Text = video.ToList()[0].Title)); } if (playlist) { Status.Text = listVidsIds.Count + " Videos"; } else { Status.Text = "Single Video"; } }
async void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) { totalbytes = 0; collectedbytes = 0; string audiomp4 = "Audio.mp4"; string Audiomp3 = "Audio.mp3"; string VideoFile = "Video.mp4"; Status.Text = "Checking tools"; async Task SourceDownloader(string name, YouTubeVideo vid) { var client = new HttpClient(); long?totalByte = 0; using (Stream output = File.OpenWrite(name)) { using (var request = new HttpRequestMessage(HttpMethod.Head, vid.Uri)) { totalByte = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result.Content.Headers.ContentLength; } totalbytes += (long)totalByte; using (var input = await client.GetStreamAsync(vid.Uri)) { byte[] buffer = new byte[16 * 1024]; int read; int totalRead = 0; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); totalRead += read; collectedbytes += read; long x = collectedbytes * 100 / totalbytes; Dataprogress.Text = bytetobigger(collectedbytes) + "/" + bytetobigger(totalbytes); progressBar1.Invoke((MethodInvoker)(() => progressBar1.Value = (int)x)); } } } client.Dispose(); } await FFmpegDownloader.GetLatestVersion(FFmpegVersion.Full); async Task DownloadWork(string link, int playlist = -1) { if (playlist < 0) { Status.Text = "Downloading .."; } else { Status.Text = (playlist + 1).ToString() + "/" + listVidsIds.Count.ToString(); } var video = youTube.GetAllVideos(link); var Targetaudio = video.Where(j => j.AdaptiveKind == AdaptiveKind.Audio && j.AudioBitrate.ToString() == selectedAudioQuality).Select(j => j); var TargetVideo = video.Where(j => j.AdaptiveKind == AdaptiveKind.Video && j.Format == VideoFormat.Mp4 && j.Resolution.ToString() == selectedVideoQality).Select(j => j); VidTitle.Invoke((MethodInvoker)(() => VidTitle.Text = video.ToList()[0].Title)); Task au = SourceDownloader(audiomp4, Targetaudio.ToList()[0]); if (SoundOnly.Checked != true) { Task vide = SourceDownloader(VideoFile, TargetVideo.ToList()[0]); await au; FFMpeg.ExtractAudio(audiomp4, Audiomp3); fileDelete(audiomp4); await vide; FFMpeg.ReplaceAudio(VideoFile, Audiomp3, FilePath.Text + TargetVideo.ToList()[0].FullName); fileDelete(VideoFile); fileDelete(Audiomp3); } else { await au; FFMpeg.ExtractAudio(audiomp4, FilePath.Text + TargetVideo.ToList()[0].Title + ".mp3"); fileDelete(audiomp4); } if (playlist < 0) { Status.Text = "Done. "; } else { Status.Text = "Done (" + (playlist + 1).ToString() + "/" + listVidsIds.Count.ToString() + ")"; } Dataprogress.Text = ""; } if (listVidsIds.Count > 0) { for (int i = 0; i < listVidsIds.Count; i++) { await DownloadWork(watchLink + listVidsIds.ElementAt(i), i); } } else { await DownloadWork(videoLink.Text); } }