Release 2024.12.03
[yt-dlp3.git] / yt_dlp / extractor / mdr.py
blobdfda3cc534afa4308a9a1d012133eda6568a6c62
1 import urllib.parse
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 int_or_none,
7 join_nonempty,
8 parse_duration,
9 parse_iso8601,
10 url_or_none,
11 xpath_text,
15 class MDRIE(InfoExtractor):
16 IE_DESC = 'MDR.DE'
17 _VALID_URL = r'https?://(?:www\.)?mdr\.de/(?:.*)/[a-z-]+-?(?P<id>\d+)(?:_.+?)?\.html'
19 _GEO_COUNTRIES = ['DE']
21 _TESTS = [{
22 # MDR regularly deletes its videos
23 'url': 'http://www.mdr.de/fakt/video189002.html',
24 'only_matching': True,
25 }, {
26 # audio
27 'url': 'http://www.mdr.de/kultur/audio1312272_zc-15948bad_zs-86171fdd.html',
28 'md5': '64c4ee50f0a791deb9479cd7bbe9d2fa',
29 'info_dict': {
30 'id': '1312272',
31 'ext': 'mp3',
32 'title': 'Feuilleton vom 30. Oktober 2015',
33 'duration': 250,
34 'uploader': 'MITTELDEUTSCHER RUNDFUNK',
36 'skip': '404 not found',
37 }, {
38 # audio with alternative playerURL pattern
39 'url': 'http://www.mdr.de/kultur/videos-und-audios/audio-radio/operation-mindfuck-robert-wilson100.html',
40 'info_dict': {
41 'id': '100',
42 'ext': 'mp4',
43 'title': 'Feature: Operation Mindfuck - Robert Anton Wilson',
44 'duration': 3239,
45 'uploader': 'MITTELDEUTSCHER RUNDFUNK',
47 'skip': '404 not found',
48 }, {
49 'url': 'http://www.mdr.de/mediathek/mdr-videos/a/video-1334.html',
50 'only_matching': True,
53 def _real_extract(self, url):
54 video_id = self._match_id(url)
56 webpage = self._download_webpage(url, video_id)
58 data_url = self._search_regex(
59 r'(?:dataURL|playerXml(?:["\'])?)\s*:\s*(["\'])(?P<url>.+?-avCustom\.xml)\1',
60 webpage, 'data url', group='url').replace(r'\/', '/')
62 doc = self._download_xml(
63 urllib.parse.urljoin(url, data_url), video_id)
65 title = xpath_text(doc, ['./title', './broadcast/broadcastName'], 'title', fatal=True)
67 type_ = xpath_text(doc, './type', default=None)
69 formats = []
70 processed_urls = []
71 for asset in doc.findall('./assets/asset'):
72 for source in (
73 'download',
74 'progressiveDownload',
75 'dynamicHttpStreamingRedirector',
76 'adaptiveHttpStreamingRedirector'):
77 url_el = asset.find(f'./{source}Url')
78 if url_el is None:
79 continue
81 video_url = url_or_none(url_el.text)
82 if not video_url or video_url in processed_urls:
83 continue
85 processed_urls.append(video_url)
87 ext = determine_ext(video_url)
88 if ext == 'm3u8':
89 formats.extend(self._extract_m3u8_formats(
90 video_url, video_id, 'mp4', entry_protocol='m3u8_native',
91 quality=1, m3u8_id='HLS', fatal=False))
92 elif ext == 'f4m':
93 formats.extend(self._extract_f4m_formats(
94 video_url + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id,
95 quality=1, f4m_id='HDS', fatal=False))
96 else:
97 media_type = xpath_text(asset, './mediaType', 'media type', default='MP4')
98 vbr = int_or_none(xpath_text(asset, './bitrateVideo', 'vbr'), 1000)
99 abr = int_or_none(xpath_text(asset, './bitrateAudio', 'abr'), 1000)
100 filesize = int_or_none(xpath_text(asset, './fileSize', 'file size'))
102 f = {
103 'url': video_url,
104 'format_id': join_nonempty(media_type, vbr or abr),
105 'filesize': filesize,
106 'abr': abr,
107 'vbr': vbr,
110 if vbr:
111 f.update({
112 'width': int_or_none(xpath_text(asset, './frameWidth', 'width')),
113 'height': int_or_none(xpath_text(asset, './frameHeight', 'height')),
116 if type_ == 'audio':
117 f['vcodec'] = 'none'
119 formats.append(f)
121 description = xpath_text(doc, './broadcast/broadcastDescription', 'description')
122 timestamp = parse_iso8601(
123 xpath_text(
124 doc, [
125 './broadcast/broadcastDate',
126 './broadcast/broadcastStartDate',
127 './broadcast/broadcastEndDate'],
128 'timestamp', default=None))
129 duration = parse_duration(xpath_text(doc, './duration', 'duration'))
130 uploader = xpath_text(doc, './rights', 'uploader')
132 return {
133 'id': video_id,
134 'title': title,
135 'description': description,
136 'timestamp': timestamp,
137 'duration': duration,
138 'uploader': uploader,
139 'formats': formats,