private void btnTaiVe_Click(object sender, EventArgs e) { if (cmbLinkVideo.SelectedIndex == -1) { return; } LinkYoutube obj = (LinkYoutube)cmbLinkVideo.SelectedItem; SaveFileDialog f = new SaveFileDialog(); f.Filter = obj.DinhDang + "|*." + obj.getExt(); f.InitialDirectory = txtLuuTru.Text; f.Title = "Luu file video"; if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (var _client = new WebClient()) { _client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); _client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); lockForm(true); _client.DownloadFileAsync(new Uri(obj.URL), f.FileName); while (_client.IsBusy) { Application.DoEvents(); } } } }
/// <summary> /// Phân tích html và lấy link /// </summary> /// <param name="url"></param> public void KhoiTao(string url) { using (var _client = new WebClient()) { _client.Encoding = System.Text.Encoding.UTF8; Html = _client.DownloadString(url); /**************************************************** * LẤY THÔNG TIN URL VIDEO * **************************************************/ //Giờ mình cắt lấy chuỗi sau url_encoded_fmt_stream_map Regex regStreamMap = new Regex("url_encoded_fmt_stream_map(.*?)\","); Match m = regStreamMap.Match(Html); if (!m.Success) { throw new Exception("Video không tồn tại!"); } //Lấy giá trị đoạn script của youtube player string strStreamMap = m.Groups[1].Value; //decode url để tiện trích xuất dữ liệu cần strStreamMap = Regex.Unescape(strStreamMap); strStreamMap = HttpUtility.UrlDecode(strStreamMap); //Đoạn thông tin chứa độ phân giải của video string strFmt = new Regex("\"fmt_list\":\"(.*?)\"").Match(Html).Groups[1].Value; //Lọc lấy từng link video Regex regUrl = new Regex("url=(.*?)(;|,|\\z)"); MatchCollection arrM = regUrl.Matches(strStreamMap); ListUrlVideo = new List <LinkYoutube>(); foreach (Match item in arrM) { LinkYoutube obj = new LinkYoutube(); obj.URL = HttpUtility.UrlDecode(item.Groups[1].Value); obj.DinhDang = new Regex("mime=(.*?)(&|\\z)").Match(obj.URL).Groups[1].Value; obj.Itag = new Regex("itag=(\\d+)(&|\\z)").Match(obj.URL).Groups[1].Value; obj.ChatLuong = new Regex(obj.Itag + "\\\\\\/(\\d+x\\d+)").Match(strFmt).Groups[1].Value; ListUrlVideo.Add(obj); } /**************************************************** * LẤY TIÊU ĐỀ, HÌNH ẢNH VÀ MÔ TẢ VIDEO * **************************************************/ TieuDe = new Regex("<meta property=\"og:title\" content=\"(.*?)\"").Match(Html).Groups[1].Value; MoTa = new Regex("<meta property=\"og:description\" content=\"(.*?)\"").Match(Html).Groups[1].Value; HinhAnh = new Regex("<meta property=\"og:image\" content=\"(.*?)\"").Match(Html).Groups[1].Value; } }