public void CancelDownload(Download video) { if (!video.Completed && activeDownloads.Contains(video)) { int threadID = video.DownloadThread != null ? video.DownloadThread.ManagedThreadId : -1; while (video.DownloadThread != null && video.DownloadThread.IsAlive) { Debug.WriteLine("Attempting download termination"); if (video.DownloadThread != null) { video.DownloadThread.Abort(); } } threadHandler.RemoveActive(threadID); activeDownloads.Remove(video); Debug.WriteLine("Download terminated"); Thread listener = new Thread(WaitForDownload); listener.Name = String.Format("Re-listener: {0}", video.VideoData.Title); threadHandler.AddActive(listener); listener.Start(); } }
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); }