]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/br.py
[youtube] De-prioritize auto-generated thumbnails
[yt-dlp.git] / yt_dlp / extractor / br.py
1 import json
2
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 ExtractorError,
7 int_or_none,
8 parse_duration,
9 parse_iso8601,
10 xpath_element,
11 xpath_text,
12 )
13
14
15 class BRIE(InfoExtractor):
16 IE_DESC = 'Bayerischer Rundfunk'
17 _VALID_URL = r'(?P<base_url>https?://(?:www\.)?br(?:-klassik)?\.de)/(?:[a-z0-9\-_]+/)+(?P<id>[a-z0-9\-_]+)\.html'
18
19 _TESTS = [
20 {
21 'url': 'http://www.br.de/mediathek/video/sendungen/abendschau/betriebliche-altersvorsorge-104.html',
22 'md5': '83a0477cf0b8451027eb566d88b51106',
23 'info_dict': {
24 'id': '48f656ef-287e-486f-be86-459122db22cc',
25 'ext': 'mp4',
26 'title': 'Die böse Überraschung',
27 'description': 'md5:ce9ac81b466ce775b8018f6801b48ac9',
28 'duration': 180,
29 'uploader': 'Reinhard Weber',
30 'upload_date': '20150422',
31 },
32 'skip': '404 not found',
33 },
34 {
35 'url': 'http://www.br.de/nachrichten/oberbayern/inhalt/muenchner-polizeipraesident-schreiber-gestorben-100.html',
36 'md5': 'af3a3a4aa43ff0ce6a89504c67f427ef',
37 'info_dict': {
38 'id': 'a4b83e34-123d-4b81-9f4e-c0d3121a4e05',
39 'ext': 'flv',
40 'title': 'Manfred Schreiber ist tot',
41 'description': 'md5:b454d867f2a9fc524ebe88c3f5092d97',
42 'duration': 26,
43 },
44 'skip': '404 not found',
45 },
46 {
47 'url': 'https://www.br-klassik.de/audio/peeping-tom-premierenkritik-dance-festival-muenchen-100.html',
48 'md5': '8b5b27c0b090f3b35eac4ab3f7a73d3d',
49 'info_dict': {
50 'id': '74c603c9-26d3-48bb-b85b-079aeed66e0b',
51 'ext': 'aac',
52 'title': 'Kurzweilig und sehr bewegend',
53 'description': 'md5:0351996e3283d64adeb38ede91fac54e',
54 'duration': 296,
55 },
56 'skip': '404 not found',
57 },
58 {
59 'url': 'http://www.br.de/radio/bayern1/service/team/videos/team-video-erdelt100.html',
60 'md5': 'dbab0aef2e047060ea7a21fc1ce1078a',
61 'info_dict': {
62 'id': '6ba73750-d405-45d3-861d-1ce8c524e059',
63 'ext': 'mp4',
64 'title': 'Umweltbewusster Häuslebauer',
65 'description': 'md5:d52dae9792d00226348c1dbb13c9bae2',
66 'duration': 116,
67 }
68 },
69 {
70 'url': 'http://www.br.de/fernsehen/br-alpha/sendungen/kant-fuer-anfaenger/kritik-der-reinen-vernunft/kant-kritik-01-metaphysik100.html',
71 'md5': '23bca295f1650d698f94fc570977dae3',
72 'info_dict': {
73 'id': 'd982c9ce-8648-4753-b358-98abb8aec43d',
74 'ext': 'mp4',
75 'title': 'Folge 1 - Metaphysik',
76 'description': 'md5:bb659990e9e59905c3d41e369db1fbe3',
77 'duration': 893,
78 'uploader': 'Eva Maria Steimle',
79 'upload_date': '20170208',
80 }
81 },
82 ]
83
84 def _real_extract(self, url):
85 base_url, display_id = self._match_valid_url(url).groups()
86 page = self._download_webpage(url, display_id)
87 xml_url = self._search_regex(
88 r"return BRavFramework\.register\(BRavFramework\('avPlayer_(?:[a-f0-9-]{36})'\)\.setup\({dataURL:'(/(?:[a-z0-9\-]+/)+[a-z0-9/~_.-]+)'}\)\);", page, 'XMLURL')
89 xml = self._download_xml(base_url + xml_url, display_id)
90
91 medias = []
92
93 for xml_media in xml.findall('video') + xml.findall('audio'):
94 media_id = xml_media.get('externalId')
95 media = {
96 'id': media_id,
97 'title': xpath_text(xml_media, 'title', 'title', True),
98 'duration': parse_duration(xpath_text(xml_media, 'duration')),
99 'formats': self._extract_formats(xpath_element(
100 xml_media, 'assets'), media_id),
101 'thumbnails': self._extract_thumbnails(xpath_element(
102 xml_media, 'teaserImage/variants'), base_url),
103 'description': xpath_text(xml_media, 'desc'),
104 'webpage_url': xpath_text(xml_media, 'permalink'),
105 'uploader': xpath_text(xml_media, 'author'),
106 }
107 broadcast_date = xpath_text(xml_media, 'broadcastDate')
108 if broadcast_date:
109 media['upload_date'] = ''.join(reversed(broadcast_date.split('.')))
110 medias.append(media)
111
112 if len(medias) > 1:
113 self.report_warning(
114 'found multiple medias; please '
115 'report this with the video URL to http://yt-dl.org/bug')
116 if not medias:
117 raise ExtractorError('No media entries found')
118 return medias[0]
119
120 def _extract_formats(self, assets, media_id):
121 formats = []
122 for asset in assets.findall('asset'):
123 format_url = xpath_text(asset, ['downloadUrl', 'url'])
124 asset_type = asset.get('type')
125 if asset_type.startswith('HDS'):
126 formats.extend(self._extract_f4m_formats(
127 format_url + '?hdcore=3.2.0', media_id, f4m_id='hds', fatal=False))
128 elif asset_type.startswith('HLS'):
129 formats.extend(self._extract_m3u8_formats(
130 format_url, media_id, 'mp4', 'm3u8_native', m3u8_id='hds', fatal=False))
131 else:
132 format_info = {
133 'ext': xpath_text(asset, 'mediaType'),
134 'width': int_or_none(xpath_text(asset, 'frameWidth')),
135 'height': int_or_none(xpath_text(asset, 'frameHeight')),
136 'tbr': int_or_none(xpath_text(asset, 'bitrateVideo')),
137 'abr': int_or_none(xpath_text(asset, 'bitrateAudio')),
138 'vcodec': xpath_text(asset, 'codecVideo'),
139 'acodec': xpath_text(asset, 'codecAudio'),
140 'container': xpath_text(asset, 'mediaType'),
141 'filesize': int_or_none(xpath_text(asset, 'size')),
142 }
143 format_url = self._proto_relative_url(format_url)
144 if format_url:
145 http_format_info = format_info.copy()
146 http_format_info.update({
147 'url': format_url,
148 'format_id': 'http-%s' % asset_type,
149 })
150 formats.append(http_format_info)
151 server_prefix = xpath_text(asset, 'serverPrefix')
152 if server_prefix:
153 rtmp_format_info = format_info.copy()
154 rtmp_format_info.update({
155 'url': server_prefix,
156 'play_path': xpath_text(asset, 'fileName'),
157 'format_id': 'rtmp-%s' % asset_type,
158 })
159 formats.append(rtmp_format_info)
160 self._sort_formats(formats)
161 return formats
162
163 def _extract_thumbnails(self, variants, base_url):
164 thumbnails = [{
165 'url': base_url + xpath_text(variant, 'url'),
166 'width': int_or_none(xpath_text(variant, 'width')),
167 'height': int_or_none(xpath_text(variant, 'height')),
168 } for variant in variants.findall('variant') if xpath_text(variant, 'url')]
169 thumbnails.sort(key=lambda x: x['width'] * x['height'], reverse=True)
170 return thumbnails
171
172
173 class BRMediathekIE(InfoExtractor):
174 IE_DESC = 'Bayerischer Rundfunk Mediathek'
175 _VALID_URL = r'https?://(?:www\.)?br\.de/mediathek//?video/(?:[^/?&#]+?-)?(?P<id>av:[0-9a-f]{24})'
176
177 _TESTS = [{
178 'url': 'https://www.br.de/mediathek/video/gesundheit-die-sendung-vom-28112017-av:5a1e6a6e8fce6d001871cc8e',
179 'md5': 'fdc3d485835966d1622587d08ba632ec',
180 'info_dict': {
181 'id': 'av:5a1e6a6e8fce6d001871cc8e',
182 'ext': 'mp4',
183 'title': 'Die Sendung vom 28.11.2017',
184 'description': 'md5:6000cdca5912ab2277e5b7339f201ccc',
185 'timestamp': 1511942766,
186 'upload_date': '20171129',
187 }
188 }, {
189 'url': 'https://www.br.de/mediathek//video/av:61b0db581aed360007558c12',
190 'only_matching': True,
191 }]
192
193 def _real_extract(self, url):
194 clip_id = self._match_id(url)
195
196 clip = self._download_json(
197 'https://proxy-base.master.mango.express/graphql',
198 clip_id, data=json.dumps({
199 "query": """{
200 viewer {
201 clip(id: "%s") {
202 title
203 description
204 duration
205 createdAt
206 ageRestriction
207 videoFiles {
208 edges {
209 node {
210 publicLocation
211 fileSize
212 videoProfile {
213 width
214 height
215 bitrate
216 encoding
217 }
218 }
219 }
220 }
221 captionFiles {
222 edges {
223 node {
224 publicLocation
225 }
226 }
227 }
228 teaserImages {
229 edges {
230 node {
231 imageFiles {
232 edges {
233 node {
234 publicLocation
235 width
236 height
237 }
238 }
239 }
240 }
241 }
242 }
243 }
244 }
245 }""" % clip_id}).encode(), headers={
246 'Content-Type': 'application/json',
247 })['data']['viewer']['clip']
248 title = clip['title']
249
250 formats = []
251 for edge in clip.get('videoFiles', {}).get('edges', []):
252 node = edge.get('node', {})
253 n_url = node.get('publicLocation')
254 if not n_url:
255 continue
256 ext = determine_ext(n_url)
257 if ext == 'm3u8':
258 formats.extend(self._extract_m3u8_formats(
259 n_url, clip_id, 'mp4', 'm3u8_native',
260 m3u8_id='hls', fatal=False))
261 else:
262 video_profile = node.get('videoProfile', {})
263 tbr = int_or_none(video_profile.get('bitrate'))
264 format_id = 'http'
265 if tbr:
266 format_id += '-%d' % tbr
267 formats.append({
268 'format_id': format_id,
269 'url': n_url,
270 'width': int_or_none(video_profile.get('width')),
271 'height': int_or_none(video_profile.get('height')),
272 'tbr': tbr,
273 'filesize': int_or_none(node.get('fileSize')),
274 })
275 self._sort_formats(formats)
276
277 subtitles = {}
278 for edge in clip.get('captionFiles', {}).get('edges', []):
279 node = edge.get('node', {})
280 n_url = node.get('publicLocation')
281 if not n_url:
282 continue
283 subtitles.setdefault('de', []).append({
284 'url': n_url,
285 })
286
287 thumbnails = []
288 for edge in clip.get('teaserImages', {}).get('edges', []):
289 for image_edge in edge.get('node', {}).get('imageFiles', {}).get('edges', []):
290 node = image_edge.get('node', {})
291 n_url = node.get('publicLocation')
292 if not n_url:
293 continue
294 thumbnails.append({
295 'url': n_url,
296 'width': int_or_none(node.get('width')),
297 'height': int_or_none(node.get('height')),
298 })
299
300 return {
301 'id': clip_id,
302 'title': title,
303 'description': clip.get('description'),
304 'duration': int_or_none(clip.get('duration')),
305 'timestamp': parse_iso8601(clip.get('createdAt')),
306 'age_limit': int_or_none(clip.get('ageRestriction')),
307 'formats': formats,
308 'subtitles': subtitles,
309 'thumbnails': thumbnails,
310 }