/// <summary> /// Queue up a video for downloading /// </summary> /// <param name="video">The video you want to download</param> public void AddToPending(Download video) { video.Completed = false; video.DownloadFailed = false; video.SetDownloadProgress(0); video.SetConvertProgress(0); pendingDownloads.Add(video); }
public async void Download(Download video) { using (var cli = Client.For(new YouTube())) //use a libvideo client to get video metadata { IEnumerable <YouTubeVideo> downloadLinks = null; try { downloadLinks = cli.GetAllVideos(video.VideoData.Url).OrderBy(br => - br.AudioBitrate); //sort by highest audio quality } catch (ArgumentException) { video.DownloadFailed = true; downloadManager.RemoveActive(video); //invalid url gui.DisplayMessage("Invalid URL!"); threadHandler.RemoveActive(Thread.CurrentThread); Thread.CurrentThread.Abort(); } YouTubeVideo highestQuality = null; try { highestQuality = downloadLinks.First(); //grab best quality link } catch { video.DownloadFailed = true; downloadManager.RemoveActive(video); gui.DisplayMessage("Unable to download video"); gui.OnProgressChanged(); return; } //setup http web request to get video bytes var asyncRequest = await highestQuality.GetUriAsync(); var request = (HttpWebRequest)HttpWebRequest.Create(Convert.ToString(asyncRequest)); request.AllowAutoRedirect = true; request.Method = "GET"; request.Proxy = HttpWebRequest.DefaultWebProxy; request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; //execute request and save bytes to buffer using (var response = (HttpWebResponse)request.GetResponse()) { var len = response.ContentLength; var buffer = new byte[256]; using (var stream = response.GetResponseStream()) { stream.ReadTimeout = 5000; using (var tempbytes = new TempFile()) { FileStream bytes = new FileStream(tempbytes.Path, FileMode.Open); while (bytes.Length < len) { try { var read = stream.Read(buffer, 0, buffer.Length); if (read > 0) { bytes.Write(buffer, 0, read); double percentage = bytes.Length * 100 / len; if (video.DownloadProgress != percentage) { video.SetDownloadProgress(percentage); gui.OnProgressChanged(); } } else { break; } } catch { video.DownloadFailed = true; downloadManager.RemoveActive(video); //thread aborted return; } } //check integrity of byte array if (bytes.Length != len) { video.DownloadFailed = true; downloadManager.RemoveActive(video); gui.DisplayMessage("File content is corrupted!"); threadHandler.RemoveActive(Thread.CurrentThread); Thread.CurrentThread.Abort(); } else { if (video.Bitrate == 0) //video { video.SetConvertProgress(100); string videoPath = Path.Combine(downloadManager.DownloadsPath, highestQuality.FullName); File.Copy(tempbytes.Path, videoPath, true); gui.DisplayMessage("Successful!"); } else //mp3 { //create temp video file to convert to mp3 and dispose of when done TimeSpan duration = GetVideoDuration(tempbytes.Path); string mp3Name = highestQuality.FullName + ".mp3"; string audioPath = Path.Combine(downloadManager.DownloadsPath, mp3Name); ToMp3(tempbytes.Path, audioPath, duration, video, video.Bitrate); //convert to mp3 } } bytes.Dispose(); bytes.Close(); } } } } downloadManager.RemoveActive(video); }