streamable.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. float_or_none,
  5. int_or_none,
  6. parse_codecs,
  7. try_get,
  8. )
  9. class StreamableIE(InfoExtractor):
  10. _VALID_URL = r'https?://streamable\.com/(?:[es]/)?(?P<id>\w+)'
  11. _EMBED_REGEX = [r'<iframe[^>]+\bsrc=(?P<q1>[\'"])(?P<url>(?:https?:)?//streamable\.com/.+?)(?P=q1)']
  12. _TESTS = [
  13. {
  14. 'url': 'https://streamable.com/dnd1',
  15. 'md5': '3e3bc5ca088b48c2d436529b64397fef',
  16. 'info_dict': {
  17. 'id': 'dnd1',
  18. 'ext': 'mp4',
  19. 'title': 'Mikel Oiarzabal scores to make it 0-3 for La Real against Espanyol',
  20. 'thumbnail': r're:https?://.*\.jpg$',
  21. 'uploader': 'teabaker',
  22. 'timestamp': 1454964157.35115,
  23. 'upload_date': '20160208',
  24. 'duration': 61.516,
  25. 'view_count': int,
  26. },
  27. },
  28. # older video without bitrate, width/height, codecs, etc. info
  29. {
  30. 'url': 'https://streamable.com/moo',
  31. 'md5': '2cf6923639b87fba3279ad0df3a64e73',
  32. 'info_dict': {
  33. 'id': 'moo',
  34. 'ext': 'mp4',
  35. 'title': '"Please don\'t eat me!"',
  36. 'thumbnail': r're:https?://.*\.jpg$',
  37. 'timestamp': 1426115495,
  38. 'upload_date': '20150311',
  39. 'duration': 12,
  40. 'view_count': int,
  41. },
  42. },
  43. {
  44. 'url': 'https://streamable.com/e/dnd1',
  45. 'only_matching': True,
  46. },
  47. {
  48. 'url': 'https://streamable.com/s/okkqk/drxjds',
  49. 'only_matching': True,
  50. },
  51. ]
  52. def _real_extract(self, url):
  53. video_id = self._match_id(url)
  54. # Note: Using the ajax API, as the public Streamable API doesn't seem
  55. # to return video info like the title properly sometimes, and doesn't
  56. # include info like the video duration
  57. video = self._download_json(
  58. f'https://ajax.streamable.com/videos/{video_id}', video_id)
  59. # Format IDs:
  60. # 0 The video is being uploaded
  61. # 1 The video is being processed
  62. # 2 The video has at least one file ready
  63. # 3 The video is unavailable due to an error
  64. status = video.get('status')
  65. if status != 2:
  66. raise ExtractorError(
  67. 'This video is currently unavailable. It may still be uploading or processing.',
  68. expected=True)
  69. title = video.get('reddit_title') or video['title']
  70. formats = []
  71. for key, info in video['files'].items():
  72. if not info.get('url'):
  73. continue
  74. formats.append({
  75. 'format_id': key,
  76. 'url': self._proto_relative_url(info['url']),
  77. 'width': int_or_none(info.get('width')),
  78. 'height': int_or_none(info.get('height')),
  79. 'filesize': int_or_none(info.get('size')),
  80. 'fps': int_or_none(info.get('framerate')),
  81. 'vbr': float_or_none(info.get('bitrate'), 1000),
  82. 'vcodec': parse_codecs(try_get(info, lambda x: x['input_metadata']['video_codec_name'])).get('vcodec'),
  83. 'acodec': parse_codecs(try_get(info, lambda x: x['input_metadata']['audio_codec_name'])).get('acodec'),
  84. })
  85. return {
  86. 'id': video_id,
  87. 'title': title,
  88. 'description': video.get('description'),
  89. 'thumbnail': self._proto_relative_url(video.get('thumbnail_url')),
  90. 'uploader': video.get('owner', {}).get('user_name'),
  91. 'timestamp': float_or_none(video.get('date_added')),
  92. 'duration': float_or_none(video.get('duration')),
  93. 'view_count': int_or_none(video.get('plays')),
  94. 'formats': formats,
  95. }