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.

127 lines
5.0 KiB

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