Twitch¶
Authentication¶
In order to get the personal OAuth token from Twitch's website which identifies your account, open Twitch.tv in your web
browser and after a successful login, open the developer tools by pressing F12 or CTRL+SHIFT+I. Then navigate to
the "Console" tab or its equivalent of your web browser and execute the following JavaScript snippet, which reads the value of
the auth-token
cookie, if it exists:
document.cookie.split("; ").find(item=>item.startsWith("auth-token="))?.split("=")[1]
Copy the resulting string consisting of 30 alphanumerical characters without any quotations.
The final Authorization
header which will identify your account while requesting a streaming access token can then be set
via Streamlink's --twitch-api-header
or --http-header
CLI arguments.
The value of the Authorization
header must be in the format of OAuth YOUR_TOKEN
. Notice the space character in the
argument value, which requires quotation on command line shells:
$ streamlink "--twitch-api-header=Authorization=OAuth abcdefghijklmnopqrstuvwxyz0123" twitch.tv/CHANNEL best
The entire argument can optionally be added to Streamlink's (Twitch plugin specific) config file, which doesn't require quotes:
twitch-api-header=Authorization=OAuth abcdefghijklmnopqrstuvwxyz0123
Embedded ads¶
Streamlink will automatically filter out preroll and midroll ads on Twitch. A message with the expected advertisement time will be logged.
Filtering out ads means that the stream output will be paused during that time, and the player will keep trying to read data until the real stream becomes available again. This also means that there will always be a stream discontinuity when the output resumes, aka. a gap of the cohesively encoded stream data. This is expected and can't be circumvented. Most players should be able to recover from this kind of discontinuity though, unlike when concatenating data of the real stream and ads.
Completely preventing ads may be possible by authenticating (Twitch Turbo)
or via special Twitch API request headers and/or parameters that modify the access token acquirement, if the community is aware
of such loop-holes. See --twitch-api-header
and --twitch-access-token-param
.
Client-integrity token¶
Sometimes when acquiring a streaming access token from Twitch for watching streams, a client-integrity token might be required. CI tokens are supposed to prove the legitimacy of the user and thus filter out bots. They are calculated using sophisticated JavaScript code in the user's web browser that's infeasible to reverse engineer or translate to Python.
When such a CI token is required, or if the user has set the --twitch-force-client-integrity
argument,
the Twitch plugin will use Streamlink's streamlink.webbrowser API, which requires
a Chromium-based web browser to be installed on the user's system, so that the CI token can be calculated.
See the --webbrowser
and related CLI arguments for more details.
If supported by the Chromium-based web browser and the environment Streamlink is run in, --webbrowser-headless
allows hiding the web browser's window.
CI tokens will be cached for as long as they are valid, to prevent having to launch the local web browser every time.
--twitch-purge-client-integrity
allows clearing the cached token.
Low latency streaming¶
Low latency streaming on Twitch can be enabled by setting the --twitch-low-latency
argument and (optionally)
configuring the player via --player-args
and reducing its own buffer to a bare minimum.
Setting --twitch-low-latency
will make Streamlink prefetch future HLS segments that are included in the HLS playlist
and which can be requested ahead of time. As soon as content becomes available, Streamlink can download it without having to
waste time on waiting for another HLS playlist refresh that might include new segments.
In addition to that, --twitch-low-latency
also reduces --hls-live-edge
to a value of at most 2
, and it
also sets the --hls-segment-stream-data
argument.
--hls-live-edge
defines how many HLS segments Streamlink should stay behind the stream's live edge, so that it can
refresh playlists and download segments in time without causing buffering. Setting the value to 1
is not advised due to how
prefetching works.
--hls-segment-stream-data
lets Streamlink write the content of in-progress segment downloads to the output buffer
instead waiting for the entire segment to complete first before data gets written. Since HLS segments on Twitch have a playback
duration of 2 seconds for most streams, this further reduces output delay.
Note
Low latency streams have to be enabled by the broadcasters on Twitch themselves. Regular streams can cause buffering issues
with this option enabled due to the reduced --hls-live-edge
value.
Unfortunately, there is no way to check whether a channel is streaming in low-latency mode before accessing the stream.
Player buffer tweaks¶
Since players do have their own input buffer, depending on how much data the player wants to keep in its buffer before it starts
playing the stream, this can cause an unnecessary delay while trying to watch low latency streams. Player buffer sizes should
therefore be tweaked via the --player-args
CLI argument or via the player's configuration options.
The delay introduced by the player depends on the stream's bitrate and how much data is necessary to allow for a smooth playback without causing any stuttering, e.g. when running out out available data.
Please refer to the player's own documentation for the available options.