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