Search code examples
c#.netyoutubeyoutube-api

How to get video id using YouTube .net API


I'm trying to find a way to give my application a YouTube URL (as copied from the address bar in a browser) and extract the unique video ID from that URL. I want to stay away from regex or any other string manipulation as a solution since not all YouTube URLs are the same and may continue to change.

There has to be some way to use the YouTube API for .NET (specifically C# for my solution) to simply call a method/function, pass the method the URL for the video, and finally the method would return the video ID as a string.

I've been trying to find documentation on this and so far I can only find info on using methods to retrieve data about a video based on already having and providing the video ID - which I do not have at this point.

I recognize which part of the YouTube URLs identify the video, but the users of my application should not have to be concerned with that.

It would be greatly appreciated if anybody could help me find a solution here.


Solution

  • Unfortunately I do not think this will work. But then again, I don´t understand the problem you have with using the URL. The URL in itself is a kind of an ID and the v property of the URL specifies which video it is. It seems you already know this.

    Regarding your problems with using an URL:

    1. Not all YouTube URLs are the same

    R: No, but it doesn´t matter because using RegEx you would only read the v property (v=-l6P7VFKnW8), alternatively the short be variant

    1. YouTube URLs may continue to change

    R: Yes, they may. However it is unlikely that YouTube would change the identifier anytime soon because of the effects it would have on API's and other infrastructure. Besides, if you have a pure and modular generic solution you wouldn´t have to change more than one RegEx to comply with the new Id.

    Related: Youtube .NET Data API: Retrieve only videoID?

    Related: C# regex to get video id from youtube and vimeo by url