[ie/reddit] Fix subtitles extraction (#10006)

Authored by: kclauhk
This commit is contained in:
kclauhk 2024-05-24 00:26:24 +08:00 committed by GitHub
parent eead3bbc01
commit 82f4f4444e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,9 +5,11 @@
ExtractorError, ExtractorError,
float_or_none, float_or_none,
int_or_none, int_or_none,
parse_qs,
traverse_obj, traverse_obj,
try_get, try_get,
unescapeHTML, unescapeHTML,
update_url_query,
urlencode_postdata, urlencode_postdata,
url_or_none, url_or_none,
) )
@ -76,7 +78,7 @@ class RedditIE(InfoExtractor):
'like_count': int, 'like_count': int,
'dislike_count': int, 'dislike_count': int,
'comment_count': int, 'comment_count': int,
'age_limit': 0, 'age_limit': 18,
'channel_id': 'u_creepyt0es', 'channel_id': 'u_creepyt0es',
}, },
'params': { 'params': {
@ -150,6 +152,51 @@ class RedditIE(InfoExtractor):
'like_count': int, 'like_count': int,
}, },
'skip': 'Requires account that has opted-in to the GenZedong subreddit', 'skip': 'Requires account that has opted-in to the GenZedong subreddit',
}, {
# subtitles in HLS manifest
'url': 'https://www.reddit.com/r/Unexpected/comments/1cl9h0u/the_insurance_claim_will_be_interesting/',
'info_dict': {
'id': 'a2mdj5d57qyc1',
'ext': 'mp4',
'display_id': '1cl9h0u',
'title': 'The insurance claim will be interesting',
'uploader': 'darrenpauli',
'channel_id': 'Unexpected',
'duration': 53,
'upload_date': '20240506',
'timestamp': 1714966382,
'age_limit': 0,
'comment_count': int,
'dislike_count': int,
'like_count': int,
'subtitles': {'en': 'mincount:1'},
},
'params': {
'skip_download': True,
},
}, {
# subtitles from caption-url
'url': 'https://www.reddit.com/r/soccer/comments/1cxwzso/tottenham_1_0_newcastle_united_james_maddison_31/',
'info_dict': {
'id': 'xbmj4t3igy1d1',
'ext': 'mp4',
'display_id': '1cxwzso',
'title': 'Tottenham [1] - 0 Newcastle United - James Maddison 31\'',
'uploader': 'Woodstovia',
'channel_id': 'soccer',
'duration': 30,
'upload_date': '20240522',
'timestamp': 1716373798,
'age_limit': 0,
'comment_count': int,
'dislike_count': int,
'like_count': int,
'subtitles': {'en': 'mincount:1'},
},
'params': {
'skip_download': True,
'writesubtitles': True,
},
}, { }, {
'url': 'https://www.reddit.com/r/videos/comments/6rrwyj', 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj',
'only_matching': True, 'only_matching': True,
@ -197,6 +244,12 @@ def _perform_login(self, username, password):
elif not traverse_obj(login, ('json', 'data', 'cookie', {str})): elif not traverse_obj(login, ('json', 'data', 'cookie', {str})):
raise ExtractorError('Unable to login, no cookie was returned') raise ExtractorError('Unable to login, no cookie was returned')
def _get_subtitles(self, video_id):
# Fallback if there were no subtitles provided by DASH or HLS manifests
caption_url = f'https://v.redd.it/{video_id}/wh_ben_en.vtt'
if self._is_valid_url(caption_url, video_id, item='subtitles'):
return {'en': [{'url': caption_url}]}
def _real_extract(self, url): def _real_extract(self, url):
host, slug, video_id = self._match_valid_url(url).group('host', 'slug', 'id') host, slug, video_id = self._match_valid_url(url).group('host', 'slug', 'id')
@ -307,6 +360,10 @@ def add_thumbnail(src):
dash_playlist_url = playlist_urls[0] or f'https://v.redd.it/{video_id}/DASHPlaylist.mpd' dash_playlist_url = playlist_urls[0] or f'https://v.redd.it/{video_id}/DASHPlaylist.mpd'
hls_playlist_url = playlist_urls[1] or f'https://v.redd.it/{video_id}/HLSPlaylist.m3u8' hls_playlist_url = playlist_urls[1] or f'https://v.redd.it/{video_id}/HLSPlaylist.m3u8'
qs = traverse_obj(parse_qs(hls_playlist_url), {
'f': ('f', 0, {lambda x: ','.join([x, 'subsAll']) if x else 'hd,subsAll'}),
})
hls_playlist_url = update_url_query(hls_playlist_url, qs)
formats = [{ formats = [{
'url': unescapeHTML(reddit_video['fallback_url']), 'url': unescapeHTML(reddit_video['fallback_url']),
@ -332,7 +389,7 @@ def add_thumbnail(src):
'id': video_id, 'id': video_id,
'display_id': display_id, 'display_id': display_id,
'formats': formats, 'formats': formats,
'subtitles': subtitles, 'subtitles': subtitles or self.extract_subtitles(video_id),
'duration': int_or_none(reddit_video.get('duration')), 'duration': int_or_none(reddit_video.get('duration')),
} }