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.

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