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.

101 lines
4.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. def _parse_jwplayer_data(self, jwplayer_data, video_id, require_title=True, m3u8_id=None, rtmp_params=None):
  12. video_data = jwplayer_data['playlist'][0]
  13. formats = []
  14. for source in video_data['sources']:
  15. source_url = self._proto_relative_url(source['file'])
  16. source_type = source.get('type') or ''
  17. if source_type in ('application/vnd.apple.mpegurl', 'hls') or determine_ext(source_url) == 'm3u8':
  18. formats.extend(self._extract_m3u8_formats(
  19. source_url, video_id, 'mp4', 'm3u8_native', m3u8_id=m3u8_id, fatal=False))
  20. elif source_type.startswith('audio'):
  21. formats.append({
  22. 'url': source_url,
  23. 'vcodec': 'none',
  24. })
  25. else:
  26. a_format = {
  27. 'url': source_url,
  28. 'width': int_or_none(source.get('width')),
  29. 'height': int_or_none(source.get('height')),
  30. }
  31. if source_url.startswith('rtmp'):
  32. a_format['ext'] = 'flv',
  33. # See com/longtailvideo/jwplayer/media/RTMPMediaProvider.as
  34. # of jwplayer.flash.swf
  35. rtmp_url_parts = re.split(
  36. r'((?:mp4|mp3|flv):)', source_url, 1)
  37. if len(rtmp_url_parts) == 3:
  38. rtmp_url, prefix, play_path = rtmp_url_parts
  39. a_format.update({
  40. 'url': rtmp_url,
  41. 'play_path': prefix + play_path,
  42. })
  43. if rtmp_params:
  44. a_format.update(rtmp_params)
  45. formats.append(a_format)
  46. self._sort_formats(formats)
  47. subtitles = {}
  48. tracks = video_data.get('tracks')
  49. if tracks and isinstance(tracks, list):
  50. for track in tracks:
  51. if track.get('file') and track.get('kind') == 'captions':
  52. subtitles.setdefault(track.get('label') or 'en', []).append({
  53. 'url': self._proto_relative_url(track['file'])
  54. })
  55. return {
  56. 'id': video_id,
  57. 'title': video_data['title'] if require_title else video_data.get('title'),
  58. 'description': video_data.get('description'),
  59. 'thumbnail': self._proto_relative_url(video_data.get('image')),
  60. 'timestamp': int_or_none(video_data.get('pubdate')),
  61. 'duration': float_or_none(jwplayer_data.get('duration')),
  62. 'subtitles': subtitles,
  63. 'formats': formats,
  64. }
  65. class JWPlatformIE(JWPlatformBaseIE):
  66. _VALID_URL = r'(?:https?://content\.jwplatform\.com/(?:feeds|players|jw6)/|jwplatform:)(?P<id>[a-zA-Z0-9]{8})'
  67. _TEST = {
  68. 'url': 'http://content.jwplatform.com/players/nPripu9l-ALJ3XQCI.js',
  69. 'md5': 'fa8899fa601eb7c83a64e9d568bdf325',
  70. 'info_dict': {
  71. 'id': 'nPripu9l',
  72. 'ext': 'mov',
  73. 'title': 'Big Buck Bunny Trailer',
  74. 'description': 'Big Buck Bunny is a short animated film by the Blender Institute. It is made using free and open source software.',
  75. 'upload_date': '20081127',
  76. 'timestamp': 1227796140,
  77. }
  78. }
  79. @staticmethod
  80. def _extract_url(webpage):
  81. mobj = re.search(
  82. r'<script[^>]+?src=["\'](?P<url>(?:https?:)?//content.jwplatform.com/players/[a-zA-Z0-9]{8})',
  83. webpage)
  84. if mobj:
  85. return mobj.group('url')
  86. def _real_extract(self, url):
  87. video_id = self._match_id(url)
  88. json_data = self._download_json('http://content.jwplatform.com/feeds/%s.json' % video_id, video_id)
  89. return self._parse_jwplayer_data(json_data, video_id)