Monday, 14 January 2013

YouTube Downloader Using C#

How to create a YouTube downloader using C#


Before we start, it is important for our users to know What is YouTube Downloader? YouTube Downloader is software that allows user to download videos from YouTube, Facebook, Google Video, Yahoo Video, and many others and convert them to other video formats. The program is easy to use, just specify the URL for the video that the user wants to download and click the Ok button.
It also allows the user to convert downloaded videos for Ipod, Iphone, PSP, Cell Phone, Windows Media, XVid and MP3. For using this application, there are certain requirements that the user needs to have in their computers in which they will be using this YouTube Downloader application. These requirements are as follows:
  • Intel Pentium 233 Mhz (or equivalent processor, such as AMD) or better
  • Windows XP/Vista/7/8
  • Internet Explorer 6.0 or higher
  • 64 MB of RAM
  • Adobe Flash Player 9+
We are using C# in this article for developing YouTube downloader application. The code used for developing this application is very simple and easy to understand for our users. The code is written in such a way so that the user can easily integrate them to their ongoing solutions or projects. Here, in this article, the third party library is not used for doing the task. The only thing that needs to be done is to take two .cs files and then finally integrate it to the project.
Shows the snapshot of the YouTube downloader
Figure 1: Shows the snapshot of the YouTube downloader
Now in order to develop the YouTube downloader application, we need to two main classes. These two classes with their functions are as follows:
  • YouTubeVideoQuality Class: This class is used for describing the video.
  • YouTubeDownloader Class: This class is used for downloading the video.
Listing 1: Shows the code for developing the YouTubeVideoQualtiy class

public class YouTubeVideoQuality
{
    /// <summary>
    /// Gets or Sets the file name
    /// </summary>
    public string VideoTitle { get; set; }
    /// <summary>
    /// Gets or Sets the file extention
    /// </summary>
    public string Extention { get; set; }
    /// <summary>
    /// Gets or Sets the file url
    /// </summary>
    public string DownloadUrl { get; set; }
    /// <summary>
    /// Gets or Sets the youtube video url
    /// </summary>
    public string VideoUrl { get; set; }
    /// <summary>
    /// Gets or Sets the file size
    /// </summary>
    public Size Dimension { get; set; }
 
    public override string ToString()
    {
        return Extention + " File " + Dimension.Width +
                           "x" + Dimension.Height;
    }
 
    public void SetQuality(string Extention, Size Dimension)
    {
        this.Extention = Extention;
        this.Dimension = Dimension;
    }
}
Listing 2: Shows the code for creating the YouTubeDownloader class
public class YouTubeDownloader
{
    public static List<YouTubeVideoQuality> GetYouTubeVideoUrls(params string[] VideoUrls)
    {
        List<YouTubeVideoQuality> urls = new List<YouTubeVideoQuality>();
        foreach (var VideoUrl in VideoUrls)
        {
            string html = Helper.DownloadWebPage(VideoUrl);
            string title = GetTitle(html);
            foreach (var videoLink in ExtractUrls(html))
            {
                YouTubeVideoQuality q = new YouTubeVideoQuality();
                q.VideoUrl = VideoUrl;
                q.VideoTitle = title;
                q.DownloadUrl = videoLink + "&title=" + title;
                if (getQuality(q))
                    urls.Add(q);
            }
        }
        return urls;
    }
 
    private static string GetTitle(string RssDoc)
    {
        string str14 = Helper.GetTxtBtwn(RssDoc, "'VIDEO_TITLE': '", "'", 0);
        if (str14 == "") str14 = Helper.GetTxtBtwn(RssDoc, "\"title\" content=\"", "\"", 0);
        if (str14 == "") str14 = Helper.GetTxtBtwn(RssDoc, "&title=", "&", 0);
        str14 = str14.Replace(@"\", "").Replace("'", "'").Replace(
                "\"", """).Replace("<", "<").Replace(
                ">", ">").Replace("+", " ");
        return str14;
    }
 
 
    private static List<string> ExtractUrls(string html)
    {
        html = Uri.UnescapeDataString(Regex.Match(html, "url_encoded_fmt_stream_map=(.+?)&",
                                      RegexOptions.Singleline).Groups[1].ToString());
        MatchCollection matchs = Regex.Matches(html,
          "url=(.+?)&quality=(.+?)&fallback_host=(.+?)&type=(.+?)&itag=(.+?),",
          RegexOptions.Singleline);
        bool firstTry = matchs.Count > 0;
        if (!firstTry)
            matchs = Regex.Matches(html,
                     "itag=(.+?)&url=(.+?)&type=(.+?)&fallback_host=(.+?)&sig=(.+?)&quality=(.+?),{0,1}",
                     RegexOptions.Singleline);
        List<string> urls = new List<string>();
        foreach (Match match in matchs)
        {
            if (firstTry)
                urls.Add(Uri.UnescapeDataString(match.Groups[1] + ""));
            else urls.Add(Uri.UnescapeDataString(match.Groups[2] + "") + "&signature=" + match.Groups[5]);
        }
        return urls;
    }
 
    private static bool getQuality(YouTubeVideoQuality q)
    {
        if (q.DownloadUrl.Contains("itag=5"))
            q.SetQuality("flv", new Size(320, 240));
        else if (q.DownloadUrl.Contains("itag=34"))
            q.SetQuality("flv", new Size(400, 226));
        else if (q.DownloadUrl.Contains("itag=6"))
            q.SetQuality("flv", new Size(480, 360));
        else if (q.DownloadUrl.Contains("itag=35"))
            q.SetQuality("flv", new Size(640, 380));
        else if (q.DownloadUrl.Contains("itag=18"))
            q.SetQuality("mp4", new Size(480, 360));
        else if (q.DownloadUrl.Contains("itag=22"))
            q.SetQuality("mp4", new Size(1280, 720));
        else if (q.DownloadUrl.Contains("itag=37"))
            q.SetQuality("mp4", new Size(1920, 1280));
        else if (q.DownloadUrl.Contains("itag=38"))
            q.SetQuality("mp4", new Size(4096, 72304));
        else return false;
        return true;
    }
}
YouTube Downloader application also provides facility to download quality videos. Users can download these videos depending on the speed of the internet connection. If the speed of the internet connection is slow, then the user can download a low quality video and vice-versa.

Conclusion

In this article, we have learned about YouTube downloader and the requirements that are needed to use this application. Here, we have used C# for developing this application.

1 comment: