CursedMagic
CursedMagic

Reputation: 51

Retrieve Video Stream from Youtube Link

I am currently working on a project to retrieve mp3 files from the audio of YouTube videos.

I started my project with some basic parsing of youtube links and came up with a regex to extract the id.

    private static string GetYoutubeID(string url)
    {
        var match = youtubeRegex.Match(url);

        if(match.Success)
        {
            return match.Groups[0].Value ?? throw new ArgumentException("url");
        }
        else
        {
            throw new ArgumentException(url);
        }
    }

Then i am using the id in a request like so:

    public static string GetResponse(string id)
    {
        using (WebClient client = new WebClient())
        {
            return client.DownloadString($"http://youtube.com/get_video_info?video_id={id}");
        }
    }

So now this call returns a string with tags like "status" and "errorcode". There is also one particular tag named "url_encoded_fmt_stream_map". In it I can find the urls to download the videos in various qualities.
For Exampe:

https%3A%2F%2Fr2---sn-h0jeened.googlevideo.com%2Fvideoplayback...

But my query seemed to fail and the result delivered me an empty "url_encoded_fmt_stream_map"-tag with "status=fail" and the "errorcode=150" which is "video unavailable".

From my understanding the libraries out there to help downloading youtube videos have some similar problems.

So my question is, is my method reliable? If so, what am I doing wrong?

And if it is not reliable, how can i reliably retrieve a link to a video stream?

Upvotes: 1

Views: 3201

Answers (1)

CursedMagic
CursedMagic

Reputation: 51

I was able to fix my problem. After some more trying and searching I was able to change the request url to achieve a result.

For anyone interested:

https://www.youtube.com/get_video_info?video_id={id}

Can be changed to:

https://www.youtube.com/get_video_info?video_id={id}&el=detailpage

This returns a full answer for me and the error 150 does not occur anymore for me.

Upvotes: 4

Related Questions