br.py 12 KB

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