private void downloader_DoWork(object sender, DoWorkEventArgs e) { YouTube youtube = new YouTube(DWCrypt.base64Decode(IDsQueue[0])); string CurrentURL = youtube.GetDownloadURL(); string CurrentQuality = DWWeb.GetQuality(IDsQueue[0]); if (!DWWeb.isQualityHigh(IDsQueue[0], CurrentQuality)) CurrentQuality = IDsQueue[0] + ";720p, 152Kb/s"; int iLastIndex = CurrentURL.LastIndexOf('/'); string sDownloadFileName = DWCrypt.RandomString(16); string sFilePathToWriteFileTo = "Active/" + sDownloadFileName + ".mp4"; try { //Vars Int64 iSize = Convert.ToInt64(GetSize(IDsQueue[0], "", false)); Int64 iRunningByteTotal = 0; fStatus = 1; //Download Video using (System.Net.WebClient client = new System.Net.WebClient()) { using (System.IO.Stream streamRemote = client.OpenRead(new Uri(CurrentURL))) { using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None)) { int iByteSize = 0; byte[] byteBuffer = new byte[8192]; while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0) { //Check for cancellation if ((downloader.CancellationPending == true)) { fStatus = 4; e.Cancel = true; break; } //Write Bytes streamLocal.Write(byteBuffer, 0, iByteSize); iRunningByteTotal += iByteSize; //Update Progress double dIndex = (double)(iRunningByteTotal); double dTotal = (double)iSize; double dProgressPercentage = (dIndex / dTotal); int iProgressPercentage = (int)(dProgressPercentage * 100); downloader.ReportProgress(iProgressPercentage); } streamLocal.Close(); } streamRemote.Close(); } } //Convert Video to Audio if (fileSize("Active/" + sDownloadFileName + ".mp4") > 20000) { if (downloader.CancellationPending) { fStatus = 4; if (File.Exists("Active/" + sDownloadFileName + ".mp4")) File.Delete("Active/" + sDownloadFileName + ".mp4"); if (File.Exists("Downloads/" + sDownloadFileName + ".mp3")) File.Delete("Downloads/" + sDownloadFileName + ".mp3"); } else { ConvertVideo(sDownloadFileName, CurrentQuality, ConvertTypeQueue[0]); fStatus = 3; cleanUpFiles(sDownloadFileName, ConvertTypeQueue[0]); fStatus = 6; DWWeb.AddToLibrary(Log.ID, IDsQueue[0], safeFilename(fName), ViewsQueue[0], LengthQueue[0]); } } else { if (File.Exists("Active/" + sDownloadFileName + ".mp4")) File.Delete("Active/" + sDownloadFileName + ".mp4"); } } catch { fStatus = 5; Thread.Sleep(3000); if (File.Exists("Active/" + sDownloadFileName + ".mp4")) File.Delete("Active/" + sDownloadFileName + ".mp4"); } }
public string GetSize(string b64url, string suffix, bool stopOnChange) { string url = ""; for (int i = 0; i < 25; i++) { if (b64url != CurrentVideoB64URL && b64url != CurrentRecentB64URL && b64url != CurrentPlaylistVideoB64URL && stopOnChange) break; YouTube youtube = new YouTube(DWCrypt.base64Decode(b64url)); url = youtube.TryGetDownloadURL(); if (url != "") break; } if (url != "") { try { Uri u = new Uri(url); System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(u); request.Timeout = 10000; System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); response.Close(); long size = response.ContentLength; if (suffix == "KB") size = (int)(size / (1024)); if (suffix == "MB") size = (int)(size / (1024 * 1024)); if (suffix == "GB") size = (int)(size / (1024 * 1024 * 1024)); return size.ToString() + suffix; } catch { } } return "Failed"; }