Search code examples
c#jsonunity-game-enginetwitch

Get Twitch VOD source video URL


I'm building an application in Unity that can play Twitch VODs via the AVPro video player. Using Twitch's V5 API I can retrieve up to 100 of the most recent VODs of a given channel (source).

I have successfully performed that; however, the JSON objects contain URLs with links to the VOD - the same one you'd see if you went to a channel and clicked on an archived broadcast - but not a link to the video file itself. I need a video file to link to AVPro for it to play the VOD, and I have searched all over the internet and through Twitch's API and cannot find a way to get a URL to a video file corresponding to a VOD. I do not want to download the VOD with a 3rd party service, as that will take up too much of the user's memory. To note, I am getting this data in C# using Unity's WWW class.

Is there something I'm missing, or is this simply not possible? All help is greatly appreciated!


Solution

  • By going through the source code to Twitch Leecher, I found the following two API calls:

    1. https://api.twitch.tv/api/vods/{0}/access_token
    2. https://usher.ttvnw.net/vod/{0}?nauthsig={1}&nauth={2}&allow_source=true&player=twitchweb&allow_spectre=true&allow_audio_only=true
    

    The first API takes in the video ID of the VOD for {0} and returns a token and signature. For the second API, use the video ID for {0} again, the signature for {1}, and the entire token for {2}.

    After that is done, I had a string with various source URLs for the VOD corresponding to different qualities. Then it was just a simple matter of parsing the result and taking the links I wanted!