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.

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