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.

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