Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

twitch-streamlink-extractor

PANCHO7532167MIT3.1.2

Extract m3u8's links from Livestreams/VODs in Twitch

livestream, twitch, twitch.tv, vod, links, link extractor, javascript, backend

readme

twitch-streamlink-extractor

Extract m3u8 from livestreams/VODs on Twitch

Visit the official npm page here: NPM Page

Installing

npm install twitch-streamlink-extractor --save

(or just download a zip from here and append it manually to your Node.JS project)

Functions/API/Usage

.extract(channel_name_or_vod_id, client_id, device_id, oauth_token, useragent)

Description: Retrieve and parse the master m3u8 file obtained from the Usher API on Twitch.

channel_name - [string or number]

The channel or VOD number that you want to extract.

client_id - [string]

Twitch Client ID that you will use for the request.

device_id - [string]

Some Device ID that will be used to identify your request to the server as it came from that device

oauth_token - [string - optional]

OAuth token of an real Twitch account, if not provided, it will be set to "undefined"

This can be used for retrieve restricted streams that normally with an public/no-account can't be watched.

useragent - [string - optional]

HTTP User Agent for the API requests, if not provided, it will use an default one.

.getToken(channel_name_or_vod_id, client_id, device_id, oauth_token, useragent)

Description: Retrieve the authorization token for the Usher API and a valid signature

channel_name - [string or number]

The channel or VOD number that you want to extract.

client_id - [string]

Twitch client id that you will use for the request.

device_id - [string]

Some Device ID that will be used to identify your request to the server as it came from that device

oauth_token - [string - optional]

OAuth token of an real Twitch account, if not provided, it will be set to "undefined"

This can be used for retrieve restricted streams that normally with an public/no-account can't be watched.

useragent - [string - optional]

HTTP User Agent for the API requests, if not provided, it will use an default one.

.getMaster(token, signature, channel_name, useragent)

Description: Retrieve the master m3u8 file from the Usher API on Twitch

token - [json string]

The JSON token string retrieved from Twitch API

signature - [string]

Hashed signature string retrieved from Twitch API

channel_name - [string or number]

The channel or VOD number that you want to extract.

useragent - [string - optional]

HTTP User Agent for the API requests, if not provided, it will use an default one.

Usage Examples

// Example of getting an m3u8's from an actual livestream using the example values.
const twitchStream = require('twitch-streamlink-extractor');
var returnedData = await twitchStream.extract("awesome_channel_name", "jknof83ly4odx7cthm5nv7xji6h2ek", "gw45n55ceowp2klfhpcjvp0cy95xed", "fv34m44bdnvo1jkegobiuo9bx84", "Mozilla/4.0; (UserAgent/1.0");
console.log(returnedData)

Returned data for the requested livestream should look like this:

[{"quality":"1080p","link":"https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/CqAENULH9QMi75PRzZb-VqJFT...z89g.m3u8"},{"quality":"720p60","link":"https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/Cp4EKo_punwHjm9MQcXm...wg.m3u8"},{"quality":"720p30","link":"https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/Cp4EakBjgnDikohPqD501YcaW0sQe8SiuULC0...GxA.m3u8"},{"quality":"480p30","link":"https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/Cp4ELeLn9jqhb1jgrUoa7xFfqQl...VKZ2tAZ1w.m3u8"},{"quality":"360p30","link":"https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/Cp4E_ldXeK0EeE0woAtn7...PlpNdWiQ.m3u8"},{"quality":"160p30","link":"https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/Cp4ELp4A-lcwFSCa0m...SPPyuA.m3u8"},{"quality":"audio_only","link":"https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/CroEgFSchiJalTMO...7YqCs.m3u8"}]
// Example of getting an m3u8's from an actual VOD using the example values.
const twitchStream = require('twitch-streamlink-extractor');
var returnedData = await twitchStream.extract("vod_number_id", "jknof83ly4odx7cthm5nv7xji6h2ek", "gw45n55ceowp2klfhpcjvp0cy95xed", "fv34m44bdnvo1jkegobiuo9bx84", "Mozilla/4.0; (UserAgent/1.0");
console.log(returnedData)

Returned data for the requested VOD should look like this:

[{"quality":"1080p","link":"https://d2nvs31859zcd8.cloudfront.net/c2c985c.../chunked/index-dvr.m3u8"},{"quality":"720p60","link":"https://d2nvs31859zcd8.cloudfront.net/c2c985c.../720p60/index-dvr.m3u8"},{"quality":"720p30","link":"https://d2nvs31859zcd8.cloudfront.net/c2c985c.../720p30/index-dvr.m3u8"},{"quality":"480p30","link":"https://d2nvs31859zcd8.cloudfront.net/c2c985c.../480p30/index-dvr.m3u8"},{"quality":"360p30","link":"https://d2nvs31859zcd8.cloudfront.net/c2c985c.../360p30/index-dvr.m3u8"},{"quality":"160p30","link":"https://d2nvs31859zcd8.cloudfront.net/c2c985c.../160p30/index-dvr.m3u8"},{"quality":"audio_only","link":"https://d2nvs31859zcd8.cloudfront.net/c2c985c.../audio-only/index-dvr.m3u8"}]

How to obtain an Client-ID and Device-ID

There are many ways on obtaining it, the main one is by opening the developer tools on a twitch stream and capturing it from the headers of gql.twitch.tv requests, also you can try on registering an app on https://dev.twitch.tv, keep on mind this last option might give you a valid Client-ID, but you 100% have to extract the Device-ID from GQL headers.

An example of how a valid client ID for Twitch should look like: jknof83ly4odx7cthm5nv7xji6h2ek

An example of how a valid device ID for Twitch should look like: gw45n55ceowp2klfhpcjvp0cy95xed

(Please note these aren't valid IDs and you shouldn't use them)

How to obtain my OAuth token

As you may have guessed, you can obtain it too by opening developer tools on a twitch stream and capturing it from the Authorization header of gql.twitch.tv requests, it should look like "Authorization: OAuth fv34m44bdnvo1jkegobiuo9bx84" where "fv34m44bdnvo1jkegobiuo9bx84" is the token you should use on OAuth parameters of this script.

An example of an OAuth token is: fv34m44bdnvo1jkegobiuo9bx84

(Please note this is not a valid token and you shouldn't use it)

Future of this project - Long term releases - Disclaimer

This project may or may not be patched by me if Twitch makes changes on how it retrieves the stream/VOD information, however feel free to make a pull request with updates/bugfixes.

No copyright intended, you use this tool as you want, i'm not responsible of anything.

changelog

v3.1.2

  • Fixed typos on README.md

v3.1.1

  • Updated examples on README.md

v3.1.0

  • Proper return value from getMaster()/getToken() - This is a breaking API change for these functions.
  • New parameter: device_id on getToken/extract() - This is a breaking API change for these functions.
  • Parameters useragent and oauth_token are now swapped in getToken()/extract() - This is a breaking API change for these functions.
  • Slightly better error handling
  • A little bit of code cleanup

v3.0.1

  • Fix GQL OAuth request

v3.0.0

  • Drop sync-request dependency in favor of an custom async/await based library
  • Removed errorCode from errors
  • Re-vamped code for modern standards (hopefully).

v2.0.1

  • Fixed some typos and updated info on README.md

v2.0.0

  • Fixed token requests with the new Twitch GQL endpoints
  • Swapped parameters in getToken() and extract()
  • Functions now returns a Javascript object instead of a JSON string
  • Bug fixes

v1.0.2

  • Swapped the useragent and oauth parameters so it's more comfortable to use
  • Changed/fixed "chunked" entries with the actual video quality of that link
  • Fixed minor typos on README

v1.0.1

  • Now you can retrieve audio only m3u8 streams and audio only m3u8 VODs
  • Fixed some typos and error messages
  • Added more tags (?)

v1.0.0

  • Now you can retrieve VODs
  • You can now provide an oauth_token for restricted streams
  • Changed to MIT license
  • More detailed README
  • Added changelog
  • Code refactored, so everything should work now.

v0.0.2

  • Fixed code so it may work now

v0.0.1

  • Initial Release