]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/threeqsdn.py
[canvas] Extract subtitles from streaming manifests
[yt-dlp.git] / yt_dlp / extractor / threeqsdn.py
CommitLineData
5c86bfe7
S
1from __future__ import unicode_literals
2
3import re
4
5from .common import InfoExtractor
30a074c2 6from ..compat import compat_HTTPError
5c86bfe7
S
7from ..utils import (
8 determine_ext,
30a074c2 9 ExtractorError,
10 float_or_none,
11 int_or_none,
12 parse_iso8601,
5c86bfe7
S
13)
14
15
16class ThreeQSDNIE(InfoExtractor):
17 IE_NAME = '3qsdn'
18 IE_DESC = '3Q SDN'
19 _VALID_URL = r'https?://playout\.3qsdn\.com/(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
20 _TESTS = [{
30a074c2 21 # https://player.3qsdn.com/demo.html
22 'url': 'https://playout.3qsdn.com/7201c779-6b3c-11e7-a40e-002590c750be',
23 'md5': '64a57396b16fa011b15e0ea60edce918',
5c86bfe7 24 'info_dict': {
30a074c2 25 'id': '7201c779-6b3c-11e7-a40e-002590c750be',
5c86bfe7 26 'ext': 'mp4',
30a074c2 27 'title': 'Video Ads',
5c86bfe7 28 'is_live': False,
30a074c2 29 'description': 'Video Ads Demo',
30 'timestamp': 1500334803,
31 'upload_date': '20170717',
32 'duration': 888.032,
33 'subtitles': {
34 'eng': 'count:1',
35 },
5c86bfe7 36 },
30a074c2 37 'expected_warnings': ['Unknown MIME type application/mp4 in DASH manifest'],
5c86bfe7
S
38 }, {
39 # live video stream
30a074c2 40 'url': 'https://playout.3qsdn.com/66e68995-11ca-11e8-9273-002590c750be',
5c86bfe7 41 'info_dict': {
30a074c2 42 'id': '66e68995-11ca-11e8-9273-002590c750be',
5c86bfe7 43 'ext': 'mp4',
30a074c2 44 'title': 're:^66e68995-11ca-11e8-9273-002590c750be [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
7b0d333a
NP
45 'is_live': True,
46 },
47 'params': {
48 'skip_download': True, # m3u8 downloads
5c86bfe7
S
49 },
50 }, {
51 # live audio stream
52 'url': 'http://playout.3qsdn.com/9edf36e0-6bf2-11e2-a16a-9acf09e2db48',
53 'only_matching': True,
54 }, {
55 # live audio stream with some 404 URLs
56 'url': 'http://playout.3qsdn.com/ac5c3186-777a-11e2-9c30-9acf09e2db48',
57 'only_matching': True,
58 }, {
59 # geo restricted with 'This content is not available in your country'
60 'url': 'http://playout.3qsdn.com/d63a3ffe-75e8-11e2-9c30-9acf09e2db48',
61 'only_matching': True,
62 }, {
63 # geo restricted with 'playout.3qsdn.com/forbidden'
64 'url': 'http://playout.3qsdn.com/8e330f26-6ae2-11e2-a16a-9acf09e2db48',
65 'only_matching': True,
66 }, {
67 # live video with rtmp link
68 'url': 'https://playout.3qsdn.com/6092bb9e-8f72-11e4-a173-002590c750be',
69 'only_matching': True,
30a074c2 70 }, {
71 # ondemand from http://www.philharmonie.tv/veranstaltung/26/
72 'url': 'http://playout.3qsdn.com/0280d6b9-1215-11e6-b427-0cc47a188158?protocol=http',
73 'only_matching': True,
74 }, {
75 # live video stream
76 'url': 'https://playout.3qsdn.com/d755d94b-4ab9-11e3-9162-0025907ad44f?js=true',
77 'only_matching': True,
5c86bfe7
S
78 }]
79
5d39176f
S
80 @staticmethod
81 def _extract_url(webpage):
82 mobj = re.search(
83 r'<iframe[^>]+\b(?:data-)?src=(["\'])(?P<url>%s.*?)\1' % ThreeQSDNIE._VALID_URL, webpage)
84 if mobj:
85 return mobj.group('url')
86
5c86bfe7
S
87 def _real_extract(self, url):
88 video_id = self._match_id(url)
89
30a074c2 90 try:
91 config = self._download_json(
92 url.replace('://playout.3qsdn.com/', '://playout.3qsdn.com/config/'), video_id)
93 except ExtractorError as e:
94 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
95 self.raise_geo_restricted()
96 raise
5c86bfe7 97
30a074c2 98 live = config.get('streamContent') == 'live'
99 aspect = float_or_none(config.get('aspect'))
5c86bfe7
S
100
101 formats = []
30a074c2 102 for source_type, source in (config.get('sources') or {}).items():
103 if not source:
104 continue
105 if source_type == 'dash':
5c86bfe7 106 formats.extend(self._extract_mpd_formats(
30a074c2 107 source, video_id, mpd_id='mpd', fatal=False))
108 elif source_type == 'hls':
5c86bfe7 109 formats.extend(self._extract_m3u8_formats(
30a074c2 110 source, video_id, 'mp4', 'm3u8' if live else 'm3u8_native',
5c86bfe7 111 m3u8_id='hls', fatal=False))
30a074c2 112 elif source_type == 'progressive':
113 for s in source:
114 src = s.get('src')
115 if not (src and self._is_valid_url(src, video_id)):
116 continue
117 width = None
118 format_id = ['http']
119 ext = determine_ext(src)
120 if ext:
121 format_id.append(ext)
122 height = int_or_none(s.get('height'))
123 if height:
124 format_id.append('%dp' % height)
125 if aspect:
126 width = int(height * aspect)
127 formats.append({
128 'ext': ext,
129 'format_id': '-'.join(format_id),
130 'height': height,
131 'source_preference': 0,
132 'url': src,
133 'vcodec': 'none' if height == 0 else None,
134 'width': width,
135 })
54f37eea 136 # It seems like this would be correctly handled by default
137 # However, unless someone can confirm this, the old
138 # behaviour is being kept as-is
139 self._sort_formats(formats, ('res', 'source_preference'))
30a074c2 140
141 subtitles = {}
142 for subtitle in (config.get('subtitles') or []):
143 src = subtitle.get('src')
144 if not src:
5c86bfe7 145 continue
30a074c2 146 subtitles.setdefault(subtitle.get('label') or 'eng', []).append({
147 'url': src,
148 })
5c86bfe7 149
30a074c2 150 title = config.get('title') or video_id
5c86bfe7
S
151
152 return {
153 'id': video_id,
30a074c2 154 'title': self._live_title(title) if live else title,
155 'thumbnail': config.get('poster') or None,
156 'description': config.get('description') or None,
157 'timestamp': parse_iso8601(config.get('upload_date')),
158 'duration': float_or_none(config.get('vlength')) or None,
5c86bfe7
S
159 'is_live': live,
160 'formats': formats,
30a074c2 161 'subtitles': subtitles,
5c86bfe7 162 }