Skip to content

Commit

Permalink
Improve handling of progressive streams
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed Aug 3, 2024
1 parent cad487d commit d6192e4
Showing 1 changed file with 73 additions and 75 deletions.
148 changes: 73 additions & 75 deletions resources/lib/youtube_plugin/youtube/helper/stream_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,13 +997,13 @@ def _normalize_url(url):

def _update_from_hls(self,
stream_list,
url,
urls,
is_live=False,
meta_info=None,
headers=None,
meta_info=None,
playback_stats=None):
if not url:
return []
if not urls:
return

if not headers and self._selected_client:
client_name = self._selected_client['_name']
Expand All @@ -1016,21 +1016,6 @@ def _update_from_hls(self,
headers = self.build_client(client_name, client_data)['headers']
curl_headers = self._make_curl_headers(headers, cookies=None)

result = self.request(
url,
headers=headers,
response_hook=self._response_hook_text,
error_title='Failed to get HLS manifest',
error_hook=self._error_hook,
error_hook_kwargs={
'video_id': self.video_id,
'client': client_name,
'auth': False,
},
)
if not result:
return ()

if meta_info is None:
meta_info = {'video': {},
'channel': {},
Expand All @@ -1040,75 +1025,84 @@ def _update_from_hls(self,
if playback_stats is None:
playback_stats = {}

if is_live:
for yt_format in ('9995', '9996'):
stream_list[yt_format] = self._get_stream_format(
itag=yt_format,
title='',
url=url,
meta=meta_info,
headers=curl_headers,
playback_stats=playback_stats,
)
else:
stream_list['9994'] = self._get_stream_format(
itag='9994',
title='',
url=url,
meta=meta_info,
headers=curl_headers,
playback_stats=playback_stats,
)

settings = self._context.get_settings()
if self._use_mpd:
qualities = settings.mpd_video_qualities()
selected_height = qualities[0]['nom_height']
else:
selected_height = settings.fixed_video_quality()

# The playlist might include a #EXT-X-MEDIA entry, but it's usually for
# a small default stream with itag 133 (240p) and can be ignored.
# Capture the URL of a .m3u8 playlist and the itag value from that URL.
re_playlist_data = re.compile(
r'#EXT-X-STREAM-INF[^#]+'
r'(?P<url>http\S+/itag/(?P<itag>\d+)\S+)'
)
for match in re_playlist_data.finditer(result):
itag = match.group('itag')
if itag in stream_list:
for url in urls:
result = self.request(
url,
headers=headers,
response_hook=self._response_hook_text,
error_title='Failed to get HLS manifest',
error_hook=self._error_hook,
error_hook_kwargs={
'video_id': self.video_id,
'client': client_name,
'auth': False,
},
)
if not result:
continue

yt_format = self._get_stream_format(
itag=itag,
max_height=selected_height,
title='',
url=match.group('url'),
meta=meta_info,
headers=curl_headers,
playback_stats=playback_stats,
for itag in ('9995', '9996') if is_live else ('9994', ):
if itag in stream_list:
continue

stream_list[itag] = self._get_stream_format(
itag=itag,
title='',
url=url,
headers=curl_headers,
meta=meta_info,
playback_stats=playback_stats,
)

# The playlist might include a #EXT-X-MEDIA entry, but it's usually
# for a default stream with itag 133 (240p) and can be ignored.
# Capture the URL of a .m3u8 playlist and the itag from that URL.
re_playlist_data = re.compile(
r'#EXT-X-STREAM-INF[^#]+'
r'(?P<url>http\S+/itag/(?P<itag>\d+)\S+)'
)
if yt_format is None:
self._context.log_debug('Unknown itag: {itag}\n{stream}'.format(
itag=itag, stream=redact_ip(match[0]),
))
if (not yt_format
or (yt_format.get('hls/video')
and not yt_format.get('hls/audio'))):
continue
for match in re_playlist_data.finditer(result):
itag = match.group('itag')
if itag in stream_list:
continue

if is_live:
yt_format['live'] = True
yt_format['title'] = 'Live ' + yt_format['title']
yt_format = self._get_stream_format(
itag=itag,
max_height=selected_height,
title='',
url=match.group('url'),
meta=meta_info,
headers=curl_headers,
playback_stats=playback_stats,
)
if yt_format is None:
self._context.log_debug('Unknown itag: {itag}\n{stream}'
.format(itag=itag,
stream=redact_ip(match[0])))
if (not yt_format
or (yt_format.get('hls/video')
and not yt_format.get('hls/audio'))):
continue

stream_list[itag] = yt_format
if is_live:
yt_format['live'] = True
yt_format['title'] = 'Live ' + yt_format['title']

stream_list[itag] = yt_format

def _update_from_streams(self,
stream_list,
streams,
is_live=False,
meta_info=None,
headers=None,
meta_info=None,
playback_stats=None):
if not headers and self._selected_client:
headers = self._selected_client['headers'].copy()
Expand All @@ -1125,6 +1119,7 @@ def _update_from_streams(self,
'channel': {},
'thumbnails': {},
'subtitles': []}

if playback_stats is None:
playback_stats = {}

Expand Down Expand Up @@ -1358,6 +1353,7 @@ def load_stream_info(self, video_id):
streaming_data = {}
adaptive_fmts = []
progressive_fmts = []
hls_playlists = []

video_info_url = 'https://www.youtube.com/youtubei/v1/player'

Expand Down Expand Up @@ -1389,7 +1385,7 @@ def load_stream_info(self, video_id):
continue
if name == 'mpd' and not use_mpd:
continue
if name == 'ask' and not ask_for_quality and self._selected_client:
if name == 'ask' and use_mpd and not ask_for_quality:
continue

status = None
Expand Down Expand Up @@ -1489,6 +1485,8 @@ def load_stream_info(self, video_id):
adaptive_fmts.extend(
_streaming_data.get('adaptiveFormats', [])
)
if 'hlsManifestUrl' in _streaming_data:
hls_playlists.append(_streaming_data['hlsManifestUrl'])
streaming_data.update(_streaming_data)

if not self._selected_client:
Expand Down Expand Up @@ -1628,18 +1626,18 @@ def load_stream_info(self, video_id):
license_info=license_info,
playback_stats=playback_stats,
)
if 'hlsManifestUrl' in streaming_data and (
if hls_playlists and (
is_live
or live_dvr
or ask_for_quality
or not use_mpd
):
self._update_from_hls(
stream_list,
streaming_data['hlsManifestUrl'],
hls_playlists,
is_live,
meta_info,
client['headers'],
meta_info,
playback_stats,
)

Expand Down Expand Up @@ -1743,8 +1741,8 @@ def load_stream_info(self, video_id):
stream_list,
progressive_fmts,
is_live,
meta_info,
client['headers'],
meta_info,
playback_stats,
)

Expand Down

0 comments on commit d6192e4

Please sign in to comment.