Search code examples
c#luayoutubevlc

Generating direct video youtube links in C# code (like VLC does)


I am trying to create a method, would get passed a standard youtube link (e.g. https://youtu.be/.......) and would return the direct video playback link, just like VLC does.

In VLC this is done with a luac or lua code (https://github.com/videolan/vlc/blob/master/share/lua/playlist/youtube.lua) also for some reason VLC only plays in 640x368 resolution??

I tried looking through the code itself but I know nothing of lua so I dont understand it at all, even with the comments. Is there a resource I could read on how this is done? As I understand it, the lua code runs some script from the website itself to generate this? Also there is a lot of descrambling??

Now I wouldnt be opposed to using LibVLC to generate the links, but as I mentioned above, that only works in 640x368, which I dont understand why. If it worked with the best quality available, then thats what I would use (possibly with ability to choose resolution??)

Also I dont really care about youtubes policy about this, so dont tell me "this is against youtubes TOS, dont do this" this is a personal project only. I know that youtube is very much against this, as even the rythm bot on discord had to stop operation....

So, in short, what I am looking for: A way to get the direct link to the video resource, be it by generating it from the website or scraping it from the HTML. I know that the link will be temporary, I only need it for a short while.


Solution

  • From https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/docs/how_do_I_do_X.md#how-do-i-play-a-youtube-video

    Core.Initialize();
    
    using(var libVLC = new LibVLC())
    {
        var media = new Media(libVLC, "https://www.youtube.com/watch?v=dQw4w9WgXcQ", FromType.FromLocation);
        await media.Parse(MediaParseOptions.ParseNetwork);
        using (var mp = new MediaPlayer(media.SubItems.First()))
        {
                var r = mp.Play();
                Console.ReadKey();
        }
    }
    

    media.SubItems.First() will contain what you need.

    As for the resolution, I don't think there is anything LibVLC can do.

    But if you don't need playback, just the video links, I'd have a look at https://github.com/Tyrrrz/YoutubeExplode