aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/extractor/elonet.py
diff options
context:
space:
mode:
authorpukkandan <pukkandan.ytdlp@gmail.com>2021-04-28 19:02:43 +0530
committerGitHub <noreply@github.com>2021-04-28 19:02:43 +0530
commitbe6202f12b97858b9d716e608394b51065d0419f (patch)
tree71f920777c24d9d81c990f0bf57d66e9d5bbaff7 /yt_dlp/extractor/elonet.py
parentdb9a564b6a5c31472f8298969584eead0b59fa1c (diff)
parente8f834cd8dfc07011d1080321e42bc130e7201bb (diff)
downloadhypervideo-pre-be6202f12b97858b9d716e608394b51065d0419f.tar.lz
hypervideo-pre-be6202f12b97858b9d716e608394b51065d0419f.tar.xz
hypervideo-pre-be6202f12b97858b9d716e608394b51065d0419f.zip
Subtitle extraction from streaming media manifests #247
Authored by fstirlitz Modified from: https://github.com/ytdl-org/youtube-dl/pull/6144 Closes: #73 Fixes: https://github.com/ytdl-org/youtube-dl/issues/6106 https://github.com/ytdl-org/youtube-dl/issues/14977 https://github.com/ytdl-org/youtube-dl/issues/21438 https://github.com/ytdl-org/youtube-dl/issues/23609 https://github.com/ytdl-org/youtube-dl/issues/28132 Might also fix (untested): https://github.com/ytdl-org/youtube-dl/issues/15424 https://github.com/ytdl-org/youtube-dl/issues/18267 https://github.com/ytdl-org/youtube-dl/issues/23899 https://github.com/ytdl-org/youtube-dl/issues/24375 https://github.com/ytdl-org/youtube-dl/issues/24595 https://github.com/ytdl-org/youtube-dl/issues/27899 Related: https://github.com/ytdl-org/youtube-dl/issues/22379 https://github.com/ytdl-org/youtube-dl/pull/24517 https://github.com/ytdl-org/youtube-dl/pull/24886 https://github.com/ytdl-org/youtube-dl/pull/27215 Notes: * The functions `extractor.common._extract_..._formats` are still kept for compatibility * Only some extractors have currently been moved to using `_extract_..._formats_and_subtitles` * Direct subtitle manifests (without a master) are not supported and are wrongly identified as containing video formats * AES support is untested * The fragmented TTML subtitles extracted from DASH/ISM are valid, but are unsupported by `ffmpeg` and most video players * Their XML fragments can be dumped using `ffmpeg -i in.mp4 -f data -map 0 -c copy out.ttml`. Once the unnecessary headers are stripped out of this, it becomes a valid self-contained ttml file * The ttml subs downloaded from DASH manifests can also be directly opened with <https://github.com/SubtitleEdit> * Fragmented WebVTT files extracted from DASH/ISM are also unsupported by most tools * Unlike the ttml files, the XML fragments of these cannot be dumped using `ffmpeg` * The webtt subs extracted from DASH can be parsed by <https://github.com/gpac/gpac> * But validity of the those extracted from ISM are untested
Diffstat (limited to 'yt_dlp/extractor/elonet.py')
-rw-r--r--yt_dlp/extractor/elonet.py82
1 files changed, 17 insertions, 65 deletions
diff --git a/yt_dlp/extractor/elonet.py b/yt_dlp/extractor/elonet.py
index 3647c0a9c..eefba4e24 100644
--- a/yt_dlp/extractor/elonet.py
+++ b/yt_dlp/extractor/elonet.py
@@ -1,9 +1,7 @@
# coding: utf-8
from __future__ import unicode_literals
-import os
import re
-import tempfile
from .common import InfoExtractor
from ..utils import (
@@ -12,12 +10,12 @@ from ..utils import (
try_get,
)
from ..compat import compat_str
-from ..downloader.hls import HlsFD
class ElonetIE(InfoExtractor):
_VALID_URL = r'https?://elonet\.finna\.fi/Record/kavi\.elonet_elokuva_(?P<id>[0-9]+)'
- _TEST = {
+ _TESTS = [{
+ # m3u8 with subtitles
'url': 'https://elonet.finna.fi/Record/kavi.elonet_elokuva_107867',
'md5': '8efc954b96c543711707f87de757caea',
'info_dict': {
@@ -27,62 +25,17 @@ class ElonetIE(InfoExtractor):
'description': 'Valkoinen peura (1952) on Erik Blombergin ohjaama ja yhdessä Mirjami Kuosmasen kanssa käsikirjoittama tarunomainen kertomus valkoisen peuran hahmossa lii...',
'thumbnail': 'https://elonet.finna.fi/Cover/Show?id=kavi.elonet_elokuva_107867&index=0&size=large',
},
- }
-
- def _download_m3u8_chunked_subtitle(self, chunklist_url):
- """
- Download VTT subtitles from pieces in manifest URL.
- Return a string containing joined chunks with extra headers removed.
- """
- with tempfile.NamedTemporaryFile(delete=True) as outfile:
- fname = outfile.name
- hlsdl = HlsFD(self._downloader, {})
- hlsdl.download(compat_str(fname), {"url": chunklist_url})
- with open(fname, 'r') as fin:
- # Remove (some) headers
- fdata = re.sub(r'X-TIMESTAMP-MAP.*\n+|WEBVTT\n+', '', fin.read())
- os.remove(fname)
- return "WEBVTT\n\n" + fdata
-
- def _parse_m3u8_subtitles(self, m3u8_doc, m3u8_url):
- """
- Parse subtitles from HLS / m3u8 manifest.
- """
- subtitles = {}
- baseurl = m3u8_url[:m3u8_url.rindex('/') + 1]
- for line in m3u8_doc.split('\n'):
- if 'EXT-X-MEDIA:TYPE=SUBTITLES' in line:
- lang = self._search_regex(
- r'LANGUAGE="(.+?)"', line, 'lang', default=False)
- uri = self._search_regex(
- r'URI="(.+?)"', line, 'uri', default=False)
- if lang and uri:
- data = self._download_m3u8_chunked_subtitle(baseurl + uri)
- subtitles[lang] = [{'ext': 'vtt', 'data': data}]
- return subtitles
-
- def _parse_mpd_subtitles(self, mpd_doc):
- """
- Parse subtitles from MPD manifest.
- """
- ns = '{urn:mpeg:dash:schema:mpd:2011}'
- subtitles = {}
- for aset in mpd_doc.findall(".//%sAdaptationSet[@mimeType='text/vtt']" % (ns)):
- lang = aset.attrib.get('lang', 'unk')
- url = aset.find("./%sRepresentation/%sBaseURL" % (ns, ns)).text
- subtitles[lang] = [{'ext': 'vtt', 'url': url}]
- return subtitles
-
- def _get_subtitles(self, fmt, doc, url):
- if fmt == 'm3u8':
- subs = self._parse_m3u8_subtitles(doc, url)
- elif fmt == 'mpd':
- subs = self._parse_mpd_subtitles(doc)
- else:
- self.report_warning(
- "Cannot download subtitles from '%s' streams." % (fmt))
- subs = {}
- return subs
+ }, {
+ # DASH with subtitles
+ 'url': 'https://elonet.finna.fi/Record/kavi.elonet_elokuva_116539',
+ 'info_dict': {
+ 'id': '116539',
+ 'ext': 'mp4',
+ 'title': 'Minulla on tiikeri',
+ 'description': 'Pienellä pojalla, joka asuu kerrostalossa, on kotieläimenä tiikeri. Se on kuitenkin salaisuus. Kerrostalon räpätäti on Kotilaisen täti, joka on aina vali...',
+ 'thumbnail': 'https://elonet.finna.fi/Cover/Show?id=kavi.elonet_elokuva_116539&index=0&size=large&source=Solr',
+ }
+ }]
def _real_extract(self, url):
video_id = self._match_id(url)
@@ -101,8 +54,8 @@ class ElonetIE(InfoExtractor):
self._parse_json(json_s, video_id),
lambda x: x[0]["src"], compat_str)
formats = []
+ subtitles = {}
if re.search(r'\.m3u8\??', src):
- fmt = 'm3u8'
res = self._download_webpage_handle(
# elonet servers have certificate problems
src.replace('https:', 'http:'), video_id,
@@ -111,11 +64,10 @@ class ElonetIE(InfoExtractor):
if res:
doc, urlh = res
url = urlh.geturl()
- formats = self._parse_m3u8_formats(doc, url)
+ formats, subtitles = self._parse_m3u8_formats_and_subtitles(doc, url)
for f in formats:
f['ext'] = 'mp4'
elif re.search(r'\.mpd\??', src):
- fmt = 'mpd'
res = self._download_xml_handle(
src, video_id,
note='Downloading MPD manifest',
@@ -123,7 +75,7 @@ class ElonetIE(InfoExtractor):
if res:
doc, urlh = res
url = base_url(urlh.geturl())
- formats = self._parse_mpd_formats(doc, mpd_base_url=url)
+ formats, subtitles = self._parse_mpd_formats_and_subtitles(doc, mpd_base_url=url)
else:
raise ExtractorError("Unknown streaming format")
@@ -133,5 +85,5 @@ class ElonetIE(InfoExtractor):
'description': description,
'thumbnail': thumbnail,
'formats': formats,
- 'subtitles': self.extract_subtitles(fmt, doc, url),
+ 'subtitles': subtitles,
}