[generic] Extract subtitles from video.js (#3156)

Authored by: Lesmiscore
This commit is contained in:
Lesmiscore (Naoya Ozaki) 2022-03-23 15:28:53 +09:00 committed by GitHub
parent c70c418d33
commit c2d2ee40eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,7 @@
) )
from ..utils import ( from ..utils import (
determine_ext, determine_ext,
dict_get,
ExtractorError, ExtractorError,
float_or_none, float_or_none,
HEADRequest, HEADRequest,
@ -31,6 +32,7 @@
parse_resolution, parse_resolution,
sanitized_Request, sanitized_Request,
smuggle_url, smuggle_url,
str_or_none,
unescapeHTML, unescapeHTML,
unified_timestamp, unified_timestamp,
unsmuggle_url, unsmuggle_url,
@ -3778,11 +3780,12 @@ def _real_extract(self, url):
# Video.js embed # Video.js embed
mobj = re.search( mobj = re.search(
r'(?s)\bvideojs\s*\(.+?\.src\s*\(\s*((?:\[.+?\]|{.+?}))\s*\)\s*;', r'(?s)\bvideojs\s*\(.+?([a-zA-Z0-9_$]+)\.src\s*\(\s*((?:\[.+?\]|{.+?}))\s*\)\s*;',
webpage) webpage)
if mobj is not None: if mobj is not None:
varname = mobj.group(1)
sources = self._parse_json( sources = self._parse_json(
mobj.group(1), video_id, transform_source=js_to_json, mobj.group(2), video_id, transform_source=js_to_json,
fatal=False) or [] fatal=False) or []
if not isinstance(sources, list): if not isinstance(sources, list):
sources = [sources] sources = [sources]
@ -3819,6 +3822,21 @@ def _real_extract(self, url):
'Referer': full_response.geturl(), 'Referer': full_response.geturl(),
}, },
}) })
# https://docs.videojs.com/player#addRemoteTextTrack
# https://html.spec.whatwg.org/multipage/media.html#htmltrackelement
for sub_match in re.finditer(rf'(?s){re.escape(varname)}' r'\.addRemoteTextTrack\(({.+?})\s*,\s*(?:true|false)\)', webpage):
sub = self._parse_json(
sub_match.group(1), video_id, transform_source=js_to_json, fatal=False) or {}
src = str_or_none(sub.get('src'))
if not src:
continue
subtitles.setdefault(dict_get(sub, ('language', 'srclang')) or 'und', []).append({
'url': compat_urlparse.urljoin(url, src),
'name': sub.get('label'),
'http_headers': {
'Referer': full_response.geturl(),
},
})
if formats or subtitles: if formats or subtitles:
self.report_detected('video.js embed') self.report_detected('video.js embed')
self._sort_formats(formats) self._sort_formats(formats)