You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
7.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urlparse
  6. from ..utils import (
  7. determine_ext,
  8. float_or_none,
  9. int_or_none,
  10. js_to_json,
  11. mimetype2ext,
  12. urljoin,
  13. )
  14. class JWPlatformBaseIE(InfoExtractor):
  15. @staticmethod
  16. def _find_jwplayer_data(webpage):
  17. # TODO: Merge this with JWPlayer-related codes in generic.py
  18. mobj = re.search(
  19. r'jwplayer\((?P<quote>[\'"])[^\'" ]+(?P=quote)\)\.setup\s*\((?P<options>[^)]+)\)',
  20. webpage)
  21. if mobj:
  22. return mobj.group('options')
  23. def _extract_jwplayer_data(self, webpage, video_id, *args, **kwargs):
  24. jwplayer_data = self._parse_json(
  25. self._find_jwplayer_data(webpage), video_id,
  26. transform_source=js_to_json)
  27. return self._parse_jwplayer_data(
  28. jwplayer_data, video_id, *args, **kwargs)
  29. def _parse_jwplayer_data(self, jwplayer_data, video_id=None, require_title=True,
  30. m3u8_id=None, mpd_id=None, rtmp_params=None, base_url=None):
  31. # JWPlayer backward compatibility: flattened playlists
  32. # https://github.com/jwplayer/jwplayer/blob/v7.4.3/src/js/api/config.js#L81-L96
  33. if 'playlist' not in jwplayer_data:
  34. jwplayer_data = {'playlist': [jwplayer_data]}
  35. entries = []
  36. # JWPlayer backward compatibility: single playlist item
  37. # https://github.com/jwplayer/jwplayer/blob/v7.7.0/src/js/playlist/playlist.js#L10
  38. if not isinstance(jwplayer_data['playlist'], list):
  39. jwplayer_data['playlist'] = [jwplayer_data['playlist']]
  40. for video_data in jwplayer_data['playlist']:
  41. # JWPlayer backward compatibility: flattened sources
  42. # https://github.com/jwplayer/jwplayer/blob/v7.4.3/src/js/playlist/item.js#L29-L35
  43. if 'sources' not in video_data:
  44. video_data['sources'] = [video_data]
  45. this_video_id = video_id or video_data['mediaid']
  46. formats = []
  47. for source in video_data['sources']:
  48. source_url = self._proto_relative_url(source['file'])
  49. if base_url:
  50. source_url = compat_urlparse.urljoin(base_url, source_url)
  51. source_type = source.get('type') or ''
  52. ext = mimetype2ext(source_type) or determine_ext(source_url)
  53. if source_type == 'hls' or ext == 'm3u8':
  54. formats.extend(self._extract_m3u8_formats(
  55. source_url, this_video_id, 'mp4', 'm3u8_native', m3u8_id=m3u8_id, fatal=False))
  56. elif ext == 'mpd':
  57. formats.extend(self._extract_mpd_formats(
  58. source_url, this_video_id, mpd_id=mpd_id, fatal=False))
  59. # https://github.com/jwplayer/jwplayer/blob/master/src/js/providers/default.js#L67
  60. elif source_type.startswith('audio') or ext in ('oga', 'aac', 'mp3', 'mpeg', 'vorbis'):
  61. formats.append({
  62. 'url': source_url,
  63. 'vcodec': 'none',
  64. 'ext': ext,
  65. })
  66. else:
  67. height = int_or_none(source.get('height'))
  68. if height is None:
  69. # Often no height is provided but there is a label in
  70. # format like 1080p.
  71. height = int_or_none(self._search_regex(
  72. r'^(\d{3,})[pP]$', source.get('label') or '',
  73. 'height', default=None))
  74. a_format = {
  75. 'url': source_url,
  76. 'width': int_or_none(source.get('width')),
  77. 'height': height,
  78. 'ext': ext,
  79. }
  80. if source_url.startswith('rtmp'):
  81. a_format['ext'] = 'flv'
  82. # See com/longtailvideo/jwplayer/media/RTMPMediaProvider.as
  83. # of jwplayer.flash.swf
  84. rtmp_url_parts = re.split(
  85. r'((?:mp4|mp3|flv):)', source_url, 1)
  86. if len(rtmp_url_parts) == 3:
  87. rtmp_url, prefix, play_path = rtmp_url_parts
  88. a_format.update({
  89. 'url': rtmp_url,
  90. 'play_path': prefix + play_path,
  91. })
  92. if rtmp_params:
  93. a_format.update(rtmp_params)
  94. formats.append(a_format)
  95. self._sort_formats(formats)
  96. subtitles = {}
  97. tracks = video_data.get('tracks')
  98. if tracks and isinstance(tracks, list):
  99. for track in tracks:
  100. if track.get('kind') != 'captions':
  101. continue
  102. track_url = urljoin(base_url, track.get('file'))
  103. if not track_url:
  104. continue
  105. subtitles.setdefault(track.get('label') or 'en', []).append({
  106. 'url': self._proto_relative_url(track_url)
  107. })
  108. entries.append({
  109. 'id': this_video_id,
  110. 'title': video_data['title'] if require_title else video_data.get('title'),
  111. 'description': video_data.get('description'),
  112. 'thumbnail': self._proto_relative_url(video_data.get('image')),
  113. 'timestamp': int_or_none(video_data.get('pubdate')),
  114. 'duration': float_or_none(jwplayer_data.get('duration') or video_data.get('duration')),
  115. 'subtitles': subtitles,
  116. 'formats': formats,
  117. })
  118. if len(entries) == 1:
  119. return entries[0]
  120. else:
  121. return self.playlist_result(entries)
  122. class JWPlatformIE(JWPlatformBaseIE):
  123. _VALID_URL = r'(?:https?://content\.jwplatform\.com/(?:feeds|players|jw6)/|jwplatform:)(?P<id>[a-zA-Z0-9]{8})'
  124. _TEST = {
  125. 'url': 'http://content.jwplatform.com/players/nPripu9l-ALJ3XQCI.js',
  126. 'md5': 'fa8899fa601eb7c83a64e9d568bdf325',
  127. 'info_dict': {
  128. 'id': 'nPripu9l',
  129. 'ext': 'mov',
  130. 'title': 'Big Buck Bunny Trailer',
  131. 'description': 'Big Buck Bunny is a short animated film by the Blender Institute. It is made using free and open source software.',
  132. 'upload_date': '20081127',
  133. 'timestamp': 1227796140,
  134. }
  135. }
  136. @staticmethod
  137. def _extract_url(webpage):
  138. mobj = re.search(
  139. r'<script[^>]+?src=["\'](?P<url>(?:https?:)?//content.jwplatform.com/players/[a-zA-Z0-9]{8})',
  140. webpage)
  141. if mobj:
  142. return mobj.group('url')
  143. def _real_extract(self, url):
  144. video_id = self._match_id(url)
  145. json_data = self._download_json('http://content.jwplatform.com/feeds/%s.json' % video_id, video_id)
  146. return self._parse_jwplayer_data(json_data, video_id)