/// <summary> /// Construct a DownloadManager which can add pending downloads, and create threads to start downloading them /// </summary> /// <param name="threadHandler">The main ThreadHandler of the application</param> /// /// <param name="applicationInterface">The frontend interface of the application</param> public DownloadManager(ThreadHandler threadHandler, iGui applicationInterface) { this.threadHandler = threadHandler; this.downloader = new Downloader(threadHandler, applicationInterface, this); this.pendingDownloads = new List <Download>(); this.activeDownloads = new List <Download>(); for (int i = 0; i < 4; i++) { Thread listener = new Thread(WaitForDownload); listener.Name = String.Format("{0} listener created by constructor", i); threadHandler.AddActive(listener); listener.Start(); } //check if downloads folder is there and try to create it if not try { VerifyDownloadDirectory(); } catch { applicationInterface.DisplayMessage("Unable to create downloads directory!"); } }
/// <summary> /// Grab the video information from a particular playlist or search query (* search query to be implemented) /// </summary> /// <param name="query">A valid YouTube video URL, playlist URL or generic search query</param> /// <returns>Returns a list of VideoData references that can then be passed to the Download method of the MainController</returns> public List <VideoData> GetVideos(string query) { List <VideoData> retrieved = videoRetriever.GetVideosByPlaylist(query); if (retrieved == null) { creator.DisplayMessage("Unable to retrieve videos - invalid search query."); } return(retrieved); }
/// <summary> /// Verifies that there is a directory set up for downloads /// </summary> /// <param name="threadHandler">The main program's thread manager</param> public Downloader(ThreadHandler threadHandler, iGui callingForm, DownloadManager downloadManager) { this.threadHandler = threadHandler; this.gui = callingForm; this.downloadManager = downloadManager; //check ffmpeg in right place if (!File.Exists(ffmpegPath)) { gui.DisplayMessage("ffmpeg.exe not found in lib folder!"); } }
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); }