Compare commits

...

12 Commits

Author SHA1 Message Date
N/Ame
cca310eb62
Merge 8631ff86d9 into 1d253b0a27 2024-11-16 22:24:54 +01:00
grqx_wsl
8631ff86d9 fix: _parse_json needs video_id 2024-10-23 22:50:34 +13:00
grqx_wsl
eb03632cc7 do not extract storyboard in extract_formats 2024-10-23 21:10:05 +13:00
grqx_wsl
1cf870df6a Merge remote-tracking branch 'upstream/master' into ie/bilibili/storyboards 2024-10-23 21:08:24 +13:00
grqx_wsl
6f5a908dff fix heatmap extraction 2024-10-23 20:45:03 +13:00
grqx_wsl
43c6c434ae make storyboard extraction non-fatal 2024-10-23 20:40:14 +13:00
grqx_wsl
a032d2b0d5 add heatmap, update tests 2024-10-23 20:26:32 +13:00
grqx_wsl
65a28bd514 add test for storyboard 2024-10-11 06:19:04 +13:00
grqx_wsl
7679b5241e update tests 2024-10-11 05:57:41 +13:00
grqx_wsl
22e5e37c82 extract episode_info in advance 2024-10-11 01:30:07 +13:00
grqx_wsl
47254db76d misc 2024-10-10 21:18:41 +13:00
grqx_wsl
995fc27931 support storyboards 2024-10-10 20:01:20 +13:00

View File

@ -33,6 +33,7 @@ from ..utils import (
parse_qs,
parse_resolution,
qualities,
sanitize_url,
smuggle_url,
srt_subtitles_timecode,
str_or_none,
@ -41,6 +42,7 @@ from ..utils import (
unsmuggle_url,
url_or_none,
urlencode_postdata,
value,
variadic,
)
@ -66,6 +68,84 @@ class BilibiliBaseIE(InfoExtractor):
f'Format(s) {missing_formats} are missing; you have to login or '
f'become a premium member to download them. {self._login_hint()}')
def _extract_heatmap(self, cid):
heatmap_json = self._download_json(
'https://bvc.bilivideo.com/pbp/data', cid,
note='Downloading heatmap', errnote='Failed to download heatmap', fatal=False,
query={'cid': cid})
if not isinstance(heatmap_json, dict):
return
duration = self._parse_json(heatmap_json['debug'], cid).get('max_time')
step_sec = traverse_obj(heatmap_json, ('step_sec', {int}))
heatmap_data = traverse_obj(heatmap_json, ('events', 'default', {list}))
if not step_sec or not heatmap_data:
return
peak = max(heatmap_data)
if not peak:
return
for idx, heatmap_entry in enumerate(heatmap_data):
start_time = idx * step_sec
end_time = start_time + step_sec
if duration and end_time >= duration:
yield {
'start_time': start_time,
'end_time': duration,
'value': heatmap_entry / peak,
}
break
yield {
'start_time': start_time,
'end_time': end_time,
'value': heatmap_entry / peak,
}
def _extract_storyboard(self, duration, aid=None, bvid=None, cid=None):
if not (video_id := aid or bvid) or not duration:
return
if storyboard_info := traverse_obj(self._download_json(
'https://api.bilibili.com/x/player/videoshot', video_id,
note='Downloading storyboard info', errnote='Failed to download storyboard info',
query=filter_dict({
'index': 1,
'aid': aid,
'bvid': bvid,
'cid': cid,
})), ('data', {lambda v: v if v.get('image') and v.get('index') else None})):
rows, cols = storyboard_info.get('img_x_len'), storyboard_info.get('img_y_len')
fragments = []
last_duration = 0.0
for i, url in enumerate(storyboard_info['image'], start=1):
if not rows or not cols:
fragments.append({'url': sanitize_url(url)})
continue
elif (duration_index := i * rows * cols - 1) < len(storyboard_info['index']) - 1:
current_duration = traverse_obj(storyboard_info, ('index', duration_index))
else:
current_duration = duration
if not current_duration or current_duration <= last_duration or current_duration > duration:
break
fragments.append({
'url': sanitize_url(url),
'duration': current_duration - last_duration if current_duration is not None else None,
})
if fragments:
return {
'format_id': 'sb',
'format_note': 'storyboard',
'ext': 'mhtml',
'protocol': 'mhtml',
'acodec': 'none',
'vcodec': 'none',
'url': 'about:invalid',
'width': storyboard_info.get('img_x_size'),
'height': storyboard_info.get('img_y_size'),
'fps': len(storyboard_info['image']) * rows * cols / duration if rows and cols else None,
'rows': rows,
'columns': cols,
'fragments': fragments,
}
def extract_formats(self, play_info):
format_names = {
r['quality']: traverse_obj(r, 'new_description', 'display_desc')
@ -287,14 +367,21 @@ class BilibiliBaseIE(InfoExtractor):
cid_edges = self._get_divisions(video_id, graph_version, {1: {'cid': cid}}, 1)
for cid, edges in cid_edges.items():
play_info = self._download_playinfo(video_id, cid, headers=headers)
formats = self.extract_formats(play_info)
duration = float_or_none(play_info.get('timelength'), scale=1000)
if storyboard_format := self._extract_storyboard(
duration=duration,
bvid=video_id, cid=cid):
formats.append(storyboard_format)
yield {
**metainfo,
'id': f'{video_id}_{cid}',
'title': f'{metainfo.get("title")} - {next(iter(edges.values())).get("title")}',
'formats': self.extract_formats(play_info),
'formats': formats,
'description': f'{json.dumps(edges, ensure_ascii=False)}\n{metainfo.get("description", "")}',
'duration': float_or_none(play_info.get('timelength'), scale=1000),
'duration': duration,
'subtitles': self.extract_subtitles(video_id, cid),
'heatmap': list(self._extract_heatmap(cid)),
}
@ -310,7 +397,7 @@ class BiliBiliIE(BilibiliBaseIE):
'description': '滴妹今天唱Closer給你聽! 有史以来,被推最多次也是最久的歌曲,其实歌词跟我原本想像差蛮多的,不过还是好听! 微博@阿滴英文',
'uploader_id': '65880958',
'uploader': '阿滴英文',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'duration': 554.117,
'tags': list,
'comment_count': int,
@ -319,6 +406,7 @@ class BiliBiliIE(BilibiliBaseIE):
'like_count': int,
'view_count': int,
'_old_archive_ids': ['bilibili 8903802_part1'],
'heatmap': [],
},
}, {
'note': 'old av URL version',
@ -337,8 +425,9 @@ class BiliBiliIE(BilibiliBaseIE):
'comment_count': int,
'view_count': int,
'tags': list,
'thumbnail': r're:^https?://.*\.(jpg|jpeg)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 1074402_part1'],
'heatmap': [],
},
'params': {'skip_download': True},
}, {
@ -356,7 +445,7 @@ class BiliBiliIE(BilibiliBaseIE):
'title': '物语中的人物是如何吐槽自己的OP的 p01 Staple Stable/战场原+羽川',
'tags': 'count:10',
'timestamp': 1589601697,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'uploader': '打牌还是打桩',
'uploader_id': '150259984',
'like_count': int,
@ -366,6 +455,7 @@ class BiliBiliIE(BilibiliBaseIE):
'description': 'md5:e3c401cf7bc363118d1783dd74068a68',
'duration': 90.314,
'_old_archive_ids': ['bilibili 498159642_part1'],
'heatmap': list,
},
}],
}, {
@ -377,7 +467,7 @@ class BiliBiliIE(BilibiliBaseIE):
'title': '物语中的人物是如何吐槽自己的OP的 p01 Staple Stable/战场原+羽川',
'tags': 'count:10',
'timestamp': 1589601697,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'uploader': '打牌还是打桩',
'uploader_id': '150259984',
'like_count': int,
@ -387,6 +477,7 @@ class BiliBiliIE(BilibiliBaseIE):
'description': 'md5:e3c401cf7bc363118d1783dd74068a68',
'duration': 90.314,
'_old_archive_ids': ['bilibili 498159642_part1'],
'heatmap': list,
},
}, {
'url': 'https://www.bilibili.com/video/av8903802/',
@ -399,13 +490,14 @@ class BiliBiliIE(BilibiliBaseIE):
'timestamp': 1488353834,
'uploader_id': '65880958',
'uploader': '阿滴英文',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'duration': 554.117,
'tags': list,
'comment_count': int,
'view_count': int,
'like_count': int,
'_old_archive_ids': ['bilibili 8903802_part1'],
'heatmap': [],
},
'params': {
'skip_download': True,
@ -428,8 +520,9 @@ class BiliBiliIE(BilibiliBaseIE):
'comment_count': int,
'view_count': int,
'like_count': int,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 463665680_part1'],
'heatmap': list,
},
'params': {'skip_download': True},
}, {
@ -447,8 +540,9 @@ class BiliBiliIE(BilibiliBaseIE):
'uploader_id': '528182630',
'view_count': int,
'like_count': int,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 893839363_part1'],
'heatmap': [],
},
}, {
'note': 'newer festival video',
@ -465,8 +559,9 @@ class BiliBiliIE(BilibiliBaseIE):
'uploader_id': '8469526',
'view_count': int,
'like_count': int,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 778246196_part1'],
'heatmap': list,
},
}, {
'note': 'legacy flv/mp4 video',
@ -484,8 +579,9 @@ class BiliBiliIE(BilibiliBaseIE):
'comment_count': int,
'like_count': int,
'tags': list,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 4120229_part4'],
'heatmap': [],
},
'params': {'extractor_args': {'bilibili': {'prefer_multi_flv': ['32']}}},
'playlist_count': 19,
@ -514,8 +610,9 @@ class BiliBiliIE(BilibiliBaseIE):
'view_count': int,
'like_count': int,
'tags': list,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 15700301_part1'],
'heatmap': [],
},
}, {
'note': 'interactive/split-path video',
@ -533,7 +630,7 @@ class BiliBiliIE(BilibiliBaseIE):
'comment_count': int,
'view_count': int,
'like_count': int,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 292734508_part1'],
},
'playlist_count': 33,
@ -552,10 +649,33 @@ class BiliBiliIE(BilibiliBaseIE):
'comment_count': int,
'view_count': int,
'like_count': int,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 292734508_part1'],
'heatmap': [],
},
}],
}, {
'note': 'storyboard',
'url': 'https://www.bilibili.com/video/av170001/',
'info_dict': {
'id': 'BV17x411w7KC_p1',
'title': '【MV】保加利亚妖王AZIS视频合辑 p01 Хоп',
'ext': 'mhtml',
'upload_date': '20111109',
'uploader_id': '122541',
'view_count': int,
'_old_archive_ids': ['bilibili 170001_part1'],
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'uploader': '冰封.虾子',
'timestamp': 1320850533,
'comment_count': int,
'tags': ['Hop', '保加利亚妖王', '保加利亚', 'Азис', 'azis', 'mv'],
'description': 'md5:acfd7360b96547f031f7ebead9e66d9e',
'like_count': int,
'duration': 199.4,
'heatmap': list,
},
'params': {'format': 'sb', 'playlist_items': '1'},
}, {
'note': '301 redirect to bangumi link',
'url': 'https://www.bilibili.com/video/BV1TE411f7f1',
@ -574,7 +694,8 @@ class BiliBiliIE(BilibiliBaseIE):
'duration': 1183.957,
'timestamp': 1571648124,
'upload_date': '20191021',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'heatmap': [],
},
}, {
'note': 'video has subtitles, which requires login',
@ -593,7 +714,7 @@ class BiliBiliIE(BilibiliBaseIE):
'comment_count': int,
'view_count': int,
'like_count': int,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'subtitles': 'count:2', # login required for CC subtitle
'_old_archive_ids': ['bilibili 898179753_part1'],
},
@ -729,6 +850,9 @@ class BiliBiliIE(BilibiliBaseIE):
__post_extractor=self.extract_comments(aid))
else:
formats = self.extract_formats(play_info)
formats.append(self._extract_storyboard(
duration=float_or_none(play_info.get('timelength'), scale=1000),
bvid=video_id, cid=cid))
if not traverse_obj(play_info, ('dash')):
# we only have legacy formats and need additional work
@ -773,6 +897,7 @@ class BiliBiliIE(BilibiliBaseIE):
'__post_extractor': self.extract_comments(aid) if idx == 0 else None,
} for idx, fragment in enumerate(formats[0]['fragments'])],
'duration': float_or_none(play_info.get('timelength'), scale=1000),
'heatmap': list(self._extract_heatmap(cid)),
}
else:
return {
@ -782,6 +907,7 @@ class BiliBiliIE(BilibiliBaseIE):
'chapters': self._get_chapters(aid, cid),
'subtitles': self.extract_subtitles(video_id, cid),
'__post_extractor': self.extract_comments(aid),
'heatmap': list(self._extract_heatmap(cid)),
}
@ -805,7 +931,8 @@ class BiliBiliBangumiIE(BilibiliBaseIE):
'duration': 1420.791,
'timestamp': 1320412200,
'upload_date': '20111104',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'heatmap': list,
},
}, {
'url': 'https://www.bilibili.com/bangumi/play/ep267851',
@ -824,7 +951,7 @@ class BiliBiliBangumiIE(BilibiliBaseIE):
'duration': 1425.256,
'timestamp': 1554566400,
'upload_date': '20190406',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
},
'skip': 'Geo-restricted',
}, {
@ -845,7 +972,8 @@ class BiliBiliBangumiIE(BilibiliBaseIE):
'duration': 1922.129,
'timestamp': 1602853860,
'upload_date': '20201016',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'heatmap': list,
},
}]
@ -865,6 +993,8 @@ class BiliBiliBangumiIE(BilibiliBaseIE):
'Extracting episode', query={'fnval': '4048', 'ep_id': episode_id},
headers=headers)
premium_only = play_info.get('code') == -10403
episode_info = traverse_obj(play_info, ('result', 'play_view_business_info', 'episode_info'))
aid, cid = episode_info.get('aid'), episode_info.get('cid')
play_info = traverse_obj(play_info, ('result', 'video_info', {dict})) or {}
formats = self.extract_formats(play_info)
@ -878,7 +1008,7 @@ class BiliBiliBangumiIE(BilibiliBaseIE):
episode_number, episode_info = next((
(idx, ep) for idx, ep in enumerate(traverse_obj(
bangumi_info, (('episodes', ('section', ..., 'episodes')), ..., {dict})), 1)
if str_or_none(ep.get('id')) == episode_id), (1, {}))
if str_or_none(ep.get('id')) == episode_id), (1, episode_info))
season_id = bangumi_info.get('season_id')
season_number, season_title = season_id and next((
@ -887,8 +1017,10 @@ class BiliBiliBangumiIE(BilibiliBaseIE):
if e.get('season_id') == season_id
), (None, None))
aid = episode_info.get('aid')
aid, cid = episode_info.get('aid', aid), episode_info.get('cid', cid)
duration = float_or_none(play_info.get('timelength'), scale=1000)
if storyboard_format := self._extract_storyboard(duration=duration, aid=aid, cid=cid):
formats.append(storyboard_format)
return {
'id': episode_id,
'formats': formats,
@ -907,10 +1039,11 @@ class BiliBiliBangumiIE(BilibiliBaseIE):
'season': str_or_none(season_title),
'season_id': str_or_none(season_id),
'season_number': season_number,
'duration': float_or_none(play_info.get('timelength'), scale=1000),
'subtitles': self.extract_subtitles(episode_id, episode_info.get('cid'), aid=aid),
'duration': duration,
'subtitles': self.extract_subtitles(episode_id, cid, aid=aid),
'__post_extractor': self.extract_comments(aid),
'http_headers': {'Referer': url},
'heatmap': list(self._extract_heatmap(cid)),
}
@ -948,7 +1081,8 @@ class BiliBiliBangumiMediaIE(BilibiliBaseIE):
'duration': 1525.777,
'timestamp': 1425074413,
'upload_date': '20150227',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'heatmap': list,
},
}],
}]
@ -1003,7 +1137,8 @@ class BiliBiliBangumiSeasonIE(BilibiliBaseIE):
'duration': 1436.992,
'timestamp': 1343185080,
'upload_date': '20120725',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'heatmap': list,
},
}],
}]
@ -1037,10 +1172,14 @@ class BilibiliCheeseBaseIE(BilibiliBaseIE):
query={'avid': aid, 'cid': cid, 'ep_id': ep_id, 'fnval': 16, 'fourk': 1},
headers=self._HEADERS, note='Downloading playinfo')['data']
formats = self.extract_formats(play_info)
duration = traverse_obj(episode_info, ('duration', {int_or_none}))
if storyboard_format := self._extract_storyboard(duration=duration, aid=aid, cid=cid):
formats.append(storyboard_format)
return {
'id': str_or_none(ep_id),
'episode_id': str_or_none(ep_id),
'formats': self.extract_formats(play_info),
'formats': formats,
'extractor_key': BilibiliCheeseIE.ie_key(),
'extractor': BilibiliCheeseIE.IE_NAME,
'webpage_url': f'https://www.bilibili.com/cheese/play/ep{ep_id}',
@ -1048,7 +1187,7 @@ class BilibiliCheeseBaseIE(BilibiliBaseIE):
'episode': ('title', {str}),
'title': {lambda v: v and join_nonempty('index', 'title', delim=' - ', from_dict=v)},
'alt_title': ('subtitle', {str}),
'duration': ('duration', {int_or_none}),
'duration': {value(duration)},
'episode_number': ('index', {int_or_none}),
'thumbnail': ('cover', {url_or_none}),
'timestamp': ('release_date', {int_or_none}),
@ -1061,6 +1200,7 @@ class BilibiliCheeseBaseIE(BilibiliBaseIE):
'subtitles': self.extract_subtitles(ep_id, cid, aid=aid),
'__post_extractor': self.extract_comments(aid),
'http_headers': self._HEADERS,
'heatmap': list(self._extract_heatmap(cid)),
}
def _download_season_info(self, query_key, video_id):
@ -1086,8 +1226,9 @@ class BilibiliCheeseIE(BilibiliCheeseBaseIE):
'duration': 221,
'timestamp': 1695549606,
'upload_date': '20230924',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'view_count': int,
'heatmap': list,
},
}]
@ -1119,8 +1260,9 @@ class BilibiliCheeseSeasonIE(BilibiliCheeseBaseIE):
'duration': 221,
'timestamp': 1695549606,
'upload_date': '20230924',
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'view_count': int,
'heatmap': list,
},
}],
'params': {'playlist_items': '1'},
@ -1492,6 +1634,7 @@ class BilibiliPlaylistIE(BilibiliSpaceListBaseIE):
'view_count': int,
'like_count': int,
'_old_archive_ids': ['bilibili 687146339_part1'],
'heatmap': [],
},
'params': {'noplaylist': True},
}, {
@ -1686,8 +1829,9 @@ class BiliBiliSearchIE(SearchInfoExtractor):
'comment_count': int,
'view_count': int,
'like_count': int,
'thumbnail': r're:^https?://.*\.(jpg|jpeg|png)$',
'thumbnail': r're:https?://.*\.(?:jpg|jpeg|png)$',
'_old_archive_ids': ['bilibili 988222410_part1'],
'heatmap': [],
},
}],
}]
@ -1734,7 +1878,7 @@ class BilibiliAudioIE(BilibiliAudioBaseIE):
'id': '1003142',
'ext': 'm4a',
'title': '【tsukimi】YELLOW / 神山羊',
'artist': 'tsukimi',
'artists': ['tsukimi'],
'comment_count': int,
'description': 'YELLOW的mp3版',
'duration': 183,
@ -1746,7 +1890,7 @@ class BilibiliAudioIE(BilibiliAudioBaseIE):
'thumbnail': r're:^https?://.+\.jpg',
'timestamp': 1564836614,
'upload_date': '20190803',
'uploader': 'tsukimi-つきみぐ',
'uploader': '十六夜tsukimiつきみぐ',
'view_count': int,
},
}
@ -1801,10 +1945,10 @@ class BilibiliAudioAlbumIE(BilibiliAudioBaseIE):
'url': 'https://www.bilibili.com/audio/am10624',
'info_dict': {
'id': '10624',
'title': '每日新曲推荐每日11:00更新',
'title': '新曲推荐',
'description': '每天11:00更新为你推送最新音乐',
},
'playlist_count': 19,
'playlist_mincount': 10,
}
def _real_extract(self, url):