[youtube] Sanity check `chapters` (and refactor related code)

Closes #520
pull/310/head
pukkandan 3 years ago
parent 3f698246b2
commit 7c365c2109
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698

@ -48,6 +48,7 @@ from ..utils import (
smuggle_url, smuggle_url,
str_or_none, str_or_none,
str_to_int, str_to_int,
traverse_obj,
try_get, try_get,
unescapeHTML, unescapeHTML,
unified_strdate, unified_strdate,
@ -56,7 +57,7 @@ from ..utils import (
url_or_none, url_or_none,
urlencode_postdata, urlencode_postdata,
urljoin, urljoin,
variadic variadic,
) )
@ -1930,44 +1931,56 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
video_id = mobj.group(2) video_id = mobj.group(2)
return video_id return video_id
def _extract_chapters_from_json(self, data, video_id, duration): def _extract_chapters_from_json(self, data, duration):
chapters_list = try_get( chapter_list = traverse_obj(
data, (
'playerOverlays', 'playerOverlayRenderer', 'decoratedPlayerBarRenderer',
'decoratedPlayerBarRenderer', 'playerBar', 'chapteredPlayerBarRenderer', 'chapters'
), expected_type=list)
return self._extract_chapters(
chapter_list,
chapter_time=lambda chapter: float_or_none(
traverse_obj(chapter, ('chapterRenderer', 'timeRangeStartMillis')), scale=1000),
chapter_title=lambda chapter: traverse_obj(
chapter, ('chapterRenderer', 'title', 'simpleText'), expected_type=str),
duration=duration)
def _extract_chapters_from_engagement_panel(self, data, duration):
content_list = traverse_obj(
data, data,
lambda x: x['playerOverlays'] ('engagementPanels', ..., 'engagementPanelSectionListRenderer', 'content', 'macroMarkersListRenderer', 'contents'),
['playerOverlayRenderer'] expected_type=list)
['decoratedPlayerBarRenderer'] chapter_time = lambda chapter: parse_duration(self._get_text(chapter.get('timeDescription')))
['decoratedPlayerBarRenderer'] chapter_title = lambda chapter: self._get_text(chapter.get('title'))
['playerBar']
['chapteredPlayerBarRenderer'] return next((
['chapters'], filter(None, (
list) self._extract_chapters(
if not chapters_list: traverse_obj(contents, (..., 'macroMarkersListItemRenderer')),
return chapter_time, chapter_title, duration)
for contents in content_list
def chapter_time(chapter): ))), [])
return float_or_none(
try_get( def _extract_chapters(self, chapter_list, chapter_time, chapter_title, duration):
chapter,
lambda x: x['chapterRenderer']['timeRangeStartMillis'],
int),
scale=1000)
chapters = [] chapters = []
for next_num, chapter in enumerate(chapters_list, start=1): last_chapter = {'start_time': 0}
for idx, chapter in enumerate(chapter_list or []):
title = chapter_title(chapter)
start_time = chapter_time(chapter) start_time = chapter_time(chapter)
if start_time is None: if start_time is None:
continue continue
end_time = (chapter_time(chapters_list[next_num]) last_chapter['end_time'] = start_time
if next_num < len(chapters_list) else duration) if start_time < last_chapter['start_time']:
if end_time is None: if idx == 1:
chapters.pop()
self.report_warning('Invalid start time for chapter "%s"' % last_chapter['title'])
else:
self.report_warning(f'Invalid start time for chapter "{title}"')
continue continue
title = try_get( last_chapter = {'start_time': start_time, 'title': title}
chapter, lambda x: x['chapterRenderer']['title']['simpleText'], chapters.append(last_chapter)
compat_str) last_chapter['end_time'] = duration
chapters.append({
'start_time': start_time,
'end_time': end_time,
'title': title,
})
return chapters return chapters
def _extract_yt_initial_variable(self, webpage, regex, video_id, name): def _extract_yt_initial_variable(self, webpage, regex, video_id, name):
@ -2830,38 +2843,10 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
pass pass
if initial_data: if initial_data:
chapters = self._extract_chapters_from_json( info['chapters'] = (
initial_data, video_id, duration) self._extract_chapters_from_json(initial_data, duration)
if not chapters: or self._extract_chapters_from_engagement_panel(initial_data, duration)
for engagment_pannel in (initial_data.get('engagementPanels') or []): or None)
contents = try_get(
engagment_pannel, lambda x: x['engagementPanelSectionListRenderer']['content']['macroMarkersListRenderer']['contents'],
list)
if not contents:
continue
def chapter_time(mmlir):
return parse_duration(
self._get_text(mmlir.get('timeDescription')))
chapters = []
for next_num, content in enumerate(contents, start=1):
mmlir = content.get('macroMarkersListItemRenderer') or {}
start_time = chapter_time(mmlir)
end_time = chapter_time(try_get(
contents, lambda x: x[next_num]['macroMarkersListItemRenderer'])) \
if next_num < len(contents) else duration
if start_time is None or end_time is None:
continue
chapters.append({
'start_time': start_time,
'end_time': end_time,
'title': self._get_text(mmlir.get('title')),
})
if chapters:
break
if chapters:
info['chapters'] = chapters
contents = try_get( contents = try_get(
initial_data, initial_data,

Loading…
Cancel
Save