C# download mp4 from url -
How to extract download URLs of any Youtube Video in C#
by admin ·

If you are wondering how most of the Youtube Video Downloader websites help you download any Youtube video, then wonder no more. The elusive download link for any Youtube video is hidden right in the HTML of the video page itself. You can grab download URLs for any Youtube video in multiple resolutions and use a download manager to download them, or just play them directly in the browser. Here’s how you can go about grabbing those download links along with the video quality information using c#.
First of all, simply open any Youtube video and go to its HTML source(Ctrl+U) and search for “url=”. The part after url= is the direct URL to the video file(for a certain resolution), ending at the string “tags=\\u0026”. You will find multiple “url=” matches, all of which point to a video file for the same video, at different resolutions. So, we are just going to grab the part after “url=” here and clean up some unnecessary bits so that we finally have our direct link to the video file. There are already some Nuget packages that allow you to grab these links but if you want a lightweight solution without using any external libraries, you can follow along. The idea is really simple – we just find the matches for url= and try to parse the actual URl using regex.
The Extractor Method takes Youtube video URL as the parameter and returns a List<string> with the video link as the first element and the quality as the second, you can also return it as an array for a slightly better performance.
usingSystem.Collections.Generic; usingSystem.Text.RegularExpressions; publicclassYoutube{ publicstaticvoidMain(string[]args) { varyt=newYoutubeUrlResolver(); varlinks=yt.Extractor("https://www.youtube.com/watch?v=LL3OYlMZq-A"); foreach(varlink inlinks) { Console.WriteLine(link.ElementAt(0)+"\n");// url of the video file at a particular resolution Console.WriteLine(link.ElementAt(1)+"\n\n");//quality of the video file } Console.ReadLine(); } } publicclassYoutubeUrlResolver { publicList<List<string>>Extractor(stringurl){ varhtml_content=""; using(varclient=newWebClient()) { client.Headers.Add("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36"); html_content+=client.DownloadString(url); } varRegex1=newRegex(@"url=(.*?tags=\\u0026)",RegexOptions.Multiline); varmatched=Regex1.Match(html_content); vardownload_infos=newList<List<string>>(); foreach(varmatched_group inmatched.Groups) { varurls=Regex.Split(WebUtility.UrlDecode(matched_group.ToString().Replace("\\u0026"," &")),",?url="); foreach(varvid_url inurls.Skip(1)) { vardownload_url=vid_url.Split(' ')[0].Split(',')[0].ToString(); Console.WriteLine(download_url); // for quality info of the video varRegex2=newRegex("(quality=|quality_label=)(.*?)(,|&| |\")"); varQualityInfo=Regex2.Match(vid_url); varquality=QualityInfo.Groups[2].ToString();//quality_info download_infos.Add((newList<string>{download_url,quality}));//contains url and resolution } } returndownload_infos; } } |
-