How to get youtube video title from url programatically

How to get Youtube Video Title from url programatically

Some people have asked me for a code snippet to get Youtube Video Title from Url. Here is how you can fetch youtube video title from url programatically using C#:

public static string GetTitle(string url)
{
    var api = $"http://youtube.com/get_video_info?video_id={GetArgs(url, "v", '?')}";
    return GetArgs(new WebClient().DownloadString(api), "title", '&');
}

private static string GetArgs(string args, string key, char query)
{
    var iqs = args.IndexOf(query);
    return iqs == -1
        ? string.Empty
        : HttpUtility.ParseQueryString(iqs < args.Length - 1
            ? args.Substring(iqs + 1) : string.Empty)[key];
}

From these two functions you would get video information and title information for a youtube url. Once you get these information, you would have to do this:

public static void Main()
{
    var url = "https://www.youtube.com/watch?v=nPXfsewcPO8";
    string name = GetTitle(url);
    Console.WriteLine(name);
}

 

Thanks for dropping by!!! Feel free to comment to this post or you can also drop me an email at naik899@gmail.com.

Leave a Reply

Your email address will not be published. Required fields are marked *