New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ie/Twitch] Fix video formats extraction #8960
Conversation
yt_dlp/extractor/twitch.py
Outdated
def _extract_twitch_m3u8_formats(self, type, id, token, signature): | ||
URLS_MAP = { | ||
'video': '%s/vod/%s.m3u8', | ||
'stream': '%s/api/channel/hls/%s.m3u8' | ||
} | ||
query = { | ||
'allow_source': 'true', | ||
'allow_audio_only': 'true', | ||
'allow_spectre': 'true', | ||
'p': random.randint(1000000, 10000000), | ||
'player': 'twitchweb', | ||
'playlist_include_framerate': 'true', | ||
'sig': signature, | ||
'token': token, | ||
} | ||
return self._extract_m3u8_formats( | ||
URLS_MAP[type] % (self._USHER_BASE, id), id, 'mp4', query=query) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be simpler to do it like this:
def _extract_twitch_m3u8_formats(self, type, id, token, signature): | |
URLS_MAP = { | |
'video': '%s/vod/%s.m3u8', | |
'stream': '%s/api/channel/hls/%s.m3u8' | |
} | |
query = { | |
'allow_source': 'true', | |
'allow_audio_only': 'true', | |
'allow_spectre': 'true', | |
'p': random.randint(1000000, 10000000), | |
'player': 'twitchweb', | |
'playlist_include_framerate': 'true', | |
'sig': signature, | |
'token': token, | |
} | |
return self._extract_m3u8_formats( | |
URLS_MAP[type] % (self._USHER_BASE, id), id, 'mp4', query=query) | |
def _extract_twitch_m3u8_formats(self, video_id, token, signature): | |
"""Subclasses must define _M3U8_PATH""" | |
return self._extract_m3u8_formats( | |
f'{self._USHER_BASE}/{self._M3U8_PATH}/{video_id}.m3u8', video_id, 'mp4', query={ | |
'allow_source': 'true', | |
'allow_audio_only': 'true', | |
'allow_spectre': 'true', | |
'p': random.randint(1000000, 10000000), | |
'player': 'twitchweb', | |
'playlist_include_framerate': 'true', | |
'sig': signature, | |
'token': token, | |
}) |
and then in the respective concrete IE classes:
_M3U8_PATH = 'vod'
_M3U8_PATH = 'api/channel/hls'
(and remove the 'video'
and 'stream'
args from the method calls)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just pass the m3u8 path itself as an argument? Each case is only used once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closes yt-dlp#8958 Authored by: DmitryScaletta
IMPORTANT: PRs without the template will be CLOSED
Description of your pull request and other information
For some users VOD downloads doesn't work right now (for me as well).
I think it's because of outdated query parameters
nauth
andnauthsig
in the m3u8 request.Probably they changed something because they added AV1 support yesterday:
https://blog.twitch.tv/en/2024/01/08/introducing-the-enhanced-broadcasting-beta/
https://blogs.nvidia.com/blog/twitch-multiencode-av1-livestreaming/
There are two separate places to get
m3u8_formats
For streams
yt-dlp/yt_dlp/extractor/twitch.py
Lines 1032 to 1046 in 5af1f19
For videos (outdated now)
yt-dlp/yt_dlp/extractor/twitch.py
Lines 535 to 547 in 5af1f19
Real query params for these requests currently look like this:
For streams
For videos
About
'segment_preference': '4',
.The only thing that this line changes is base url of playlist urls:
https://gist.github.com/DmitryScaletta/eab5d3e93d678c6f4cd89e678779f006/revisions
This line was added 9 years ago. And it's missing in real requests in the browser. I don't think twitch uses it now.
About
entry_protocol='m3u8_native'
.I checked both stream and video formats and the protocol there is
m3u8_native
by default so it can be removed.After this fix:
Formats for VODs
Formats for streams
$ ./yt-dlp.cmd https://www.twitch.tv/lirik -F [twitch:stream] Extracting URL: https://www.twitch.tv/lirik [twitch:stream] lirik: Downloading stream GraphQL [twitch:stream] lirik: Downloading stream access token GraphQL [twitch:stream] lirik: Downloading m3u8 information [info] Available formats for 43367080459: ID EXT RESOLUTION FPS │ TBR PROTO │ VCODEC ACODEC ABR ────────────────────────────────────────────────────────────────────────────── audio_only mp4 audio only │ 160k m3u8 │ audio only mp4a.40.2 160k 160p mp4 284x160 30 │ 230k m3u8 │ avc1.4D401F mp4a.40.2 360p mp4 640x360 30 │ 630k m3u8 │ avc1.4D401F mp4a.40.2 480p mp4 852x480 30 │ 1428k m3u8 │ avc1.4D401F mp4a.40.2 720p mp4 1280x720 30 │ 2373k m3u8 │ avc1.4D401F mp4a.40.2 720p60 mp4 1280x720 60 │ 3423k m3u8 │ avc1.4D401F mp4a.40.2 1080p60__source_ mp4 1920x1080 60 │ 9053k m3u8 │ avc1.42C02A mp4a.40.2
Fixes #8958
Template
Before submitting a pull request make sure you have:
In order to be accepted and merged into yt-dlp each piece of code must be in public domain or released under Unlicense. Check all of the following options that apply:
What is the purpose of your pull request?