]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/mdr.py
[noco] Fix bitrates
[youtube-dl.git] / youtube_dl / extractor / mdr.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_urlparse
6 from ..utils import (
7     determine_ext,
8     int_or_none,
9     parse_duration,
10     parse_iso8601,
11     xpath_text,
12 )
13
14
15 class MDRIE(InfoExtractor):
16     IE_DESC = 'MDR.DE and KiKA'
17     _VALID_URL = r'https?://(?:www\.)?(?:mdr|kika)\.de/(?:.*)/[a-z]+(?P<id>\d+)(?:_.+?)?\.html'
18
19     _TESTS = [{
20         # MDR regularily deletes its videos
21         'url': 'http://www.mdr.de/fakt/video189002.html',
22         'only_matching': True,
23     }, {
24         # audio
25         'url': 'http://www.mdr.de/kultur/audio1312272_zc-15948bad_zs-86171fdd.html',
26         'md5': '64c4ee50f0a791deb9479cd7bbe9d2fa',
27         'info_dict': {
28             'id': '1312272',
29             'ext': 'mp3',
30             'title': 'Feuilleton vom 30. Oktober 2015',
31             'duration': 250,
32             'uploader': 'MITTELDEUTSCHER RUNDFUNK',
33         },
34     }, {
35         'url': 'http://www.kika.de/baumhaus/videos/video19636.html',
36         'md5': '4930515e36b06c111213e80d1e4aad0e',
37         'info_dict': {
38             'id': '19636',
39             'ext': 'mp4',
40             'title': 'Baumhaus vom 30. Oktober 2015',
41             'duration': 134,
42             'uploader': 'KIKA',
43         },
44     }, {
45         'url': 'http://www.kika.de/sendungen/einzelsendungen/weihnachtsprogramm/videos/video8182.html',
46         'md5': '5fe9c4dd7d71e3b238f04b8fdd588357',
47         'info_dict': {
48             'id': '8182',
49             'ext': 'mp4',
50             'title': 'Beutolomäus und der geheime Weihnachtswunsch',
51             'description': 'md5:b69d32d7b2c55cbe86945ab309d39bbd',
52             'timestamp': 1419047100,
53             'upload_date': '20141220',
54             'duration': 4628,
55             'uploader': 'KIKA',
56         },
57     }, {
58         'url': 'http://www.kika.de/baumhaus/sendungen/video19636_zc-fea7f8a0_zs-4bf89c60.html',
59         'only_matching': True,
60     }, {
61         'url': 'http://www.kika.de/sendungen/einzelsendungen/weihnachtsprogramm/einzelsendung2534.html',
62         'only_matching': True,
63     }]
64
65     def _real_extract(self, url):
66         video_id = self._match_id(url)
67
68         webpage = self._download_webpage(url, video_id)
69
70         data_url = self._search_regex(
71             r'dataURL\s*:\s*(["\'])(?P<url>/.+/(?:video|audio)[0-9]+-avCustom\.xml)\1',
72             webpage, 'data url', group='url')
73
74         doc = self._download_xml(
75             compat_urlparse.urljoin(url, data_url), video_id)
76
77         title = xpath_text(doc, ['./title', './broadcast/broadcastName'], 'title', fatal=True)
78
79         formats = []
80         processed_urls = []
81         for asset in doc.findall('./assets/asset'):
82             for source in (
83                     'progressiveDownload',
84                     'dynamicHttpStreamingRedirector',
85                     'adaptiveHttpStreamingRedirector'):
86                 url_el = asset.find('./%sUrl' % source)
87                 if url_el is None:
88                     continue
89
90                 video_url = url_el.text
91                 if video_url in processed_urls:
92                     continue
93
94                 processed_urls.append(video_url)
95
96                 vbr = int_or_none(xpath_text(asset, './bitrateVideo', 'vbr'), 1000)
97                 abr = int_or_none(xpath_text(asset, './bitrateAudio', 'abr'), 1000)
98
99                 ext = determine_ext(url_el.text)
100                 if ext == 'm3u8':
101                     url_formats = self._extract_m3u8_formats(
102                         video_url, video_id, 'mp4', entry_protocol='m3u8_native',
103                         preference=0, m3u8_id='HLS', fatal=False)
104                 elif ext == 'f4m':
105                     url_formats = self._extract_f4m_formats(
106                         video_url + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id,
107                         preference=0, f4m_id='HDS', fatal=False)
108                 else:
109                     media_type = xpath_text(asset, './mediaType', 'media type', default='MP4')
110                     vbr = int_or_none(xpath_text(asset, './bitrateVideo', 'vbr'), 1000)
111                     abr = int_or_none(xpath_text(asset, './bitrateAudio', 'abr'), 1000)
112                     filesize = int_or_none(xpath_text(asset, './fileSize', 'file size'))
113
114                     f = {
115                         'url': video_url,
116                         'format_id': '%s-%d' % (media_type, vbr or abr),
117                         'filesize': filesize,
118                         'abr': abr,
119                         'preference': 1,
120                     }
121
122                     if vbr:
123                         width = int_or_none(xpath_text(asset, './frameWidth', 'width'))
124                         height = int_or_none(xpath_text(asset, './frameHeight', 'height'))
125                         f.update({
126                             'vbr': vbr,
127                             'width': width,
128                             'height': height,
129                         })
130
131                     url_formats = [f]
132
133                 if not url_formats:
134                     continue
135
136                 if not vbr:
137                     for f in url_formats:
138                         abr = f.get('tbr') or abr
139                         if 'tbr' in f:
140                             del f['tbr']
141                         f.update({
142                             'abr': abr,
143                             'vcodec': 'none',
144                         })
145
146                 formats.extend(url_formats)
147
148         self._sort_formats(formats)
149
150         description = xpath_text(doc, './broadcast/broadcastDescription', 'description')
151         timestamp = parse_iso8601(
152             xpath_text(
153                 doc, [
154                     './broadcast/broadcastDate',
155                     './broadcast/broadcastStartDate',
156                     './broadcast/broadcastEndDate'],
157                 'timestamp', default=None))
158         duration = parse_duration(xpath_text(doc, './duration', 'duration'))
159         uploader = xpath_text(doc, './rights', 'uploader')
160
161         return {
162             'id': video_id,
163             'title': title,
164             'description': description,
165             'timestamp': timestamp,
166             'duration': duration,
167             'uploader': uploader,
168             'formats': formats,
169         }