5 from .common
import InfoExtractor
17 class UstreamIE(InfoExtractor
):
18 _VALID_URL
= r
'https?://(?:www\.)?(?:ustream\.tv|video\.ibm\.com)/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)'
20 _EMBED_REGEX
= [r
'<iframe[^>]+?src=(["\'])(?P
<url
>https?
://(?
:www\
.)?
(?
:ustream\
.tv|video\
.ibm\
.com
)/embed
/.+?
)\
1']
22 'url
': 'http
://www
.ustream
.tv
/recorded
/20274954',
23 'md5
': '088f151799e8f572f84eb62f17d73e5c
',
27 'title
': 'Young Americans
for Liberty February
7, 2012 2:28 AM
',
28 'description
': 'Young Americans
for Liberty February
7, 2012 2:28 AM
',
29 'timestamp
': 1328577035,
30 'upload_date
': '20120207',
31 'uploader
': 'yaliberty
',
32 'uploader_id
': '6780869',
35 # From http://sportscanada.tv/canadagames/index.php/week2/figure-skating/444
36 # Title and uploader available only from params JSON
37 'url
': 'http
://www
.ustream
.tv
/embed
/recorded
/59307601?ub
=ff0000
&lc
=ff0000
&oc
=ffffff
&uc
=ffffff
&v
=3&wmode
=direct
',
38 'md5
': '5a2abf40babeac9812ed20ae12d34e10
',
42 'title
': '-CG11
- Canada Games Figure Skating
',
43 'uploader
': 'sportscanadatv
',
45 'skip
': 'This Pro Broadcaster has chosen to remove this video
from the ustream
.tv site
.',
47 'url
': 'http
://www
.ustream
.tv
/embed
/10299409',
53 'url
': 'http
://www
.ustream
.tv
/recorded
/91343263',
57 'title
': 'GitHub Universe
- General Session
- Day
1',
58 'upload_date
': '20160914',
59 'description
': 'GitHub Universe
- General Session
- Day
1',
60 'timestamp
': 1473872730,
61 'uploader
': 'wa0dnskeqkr
',
62 'uploader_id
': '38977840',
65 'skip_download
': True, # m3u8 download
68 'url
': 'https
://video
.ibm
.com
/embed
/recorded
/128240221?
&autoplay
=true
&controls
=true
&volume
=100',
69 'only_matching
': True,
72 def _get_stream_info(self, url, video_id, app_id_ver, extra_note=None):
76 rnd = lambda x: random.randrange(int(x))
81 conn_info = self._download_json(
82 f'http
://r
{rnd(1e8
)}-1-{video_id}
-recorded
-lp
-live
.ums
.ustream
.tv
/1/ustream
',
83 video_id, note='Downloading connection info
' + extra_note,
86 'appId
': app_id_ver[0],
87 'appVersion
': app_id_ver[1],
88 'rsid
': f'{num_to_hex(rnd(1e8
))}:{num_to_hex(rnd(1e8
))}',
89 'rpin
': f'_rpin
.{rnd(1e15
)}',
92 'application
': 'recorded
',
94 host = conn_info[0]['args
'][0]['host
']
95 connection_id = conn_info[0]['args
'][0]['connectionId
']
97 return self._download_json(
98 f'http
://{host}
/1/ustream?connectionId
={connection_id}
',
99 video_id, note='Downloading stream info
' + extra_note)
101 def _get_streams(self, url, video_id, app_id_ver):
102 # Sometimes the return dict does not have 'stream
'
103 for trial_count in range(3):
104 stream_info = self._get_stream_info(
105 url, video_id, app_id_ver,
106 extra_note=f' (try {trial_count
+ 1})' if trial_count > 0 else '')
107 if 'stream
' in stream_info[0]['args
'][0]:
108 return stream_info[0]['args
'][0]['stream
']
111 def _parse_segmented_mp4(self, dash_stream_info):
112 def resolve_dash_template(template, idx, chunk_hash):
113 return template.replace('%', str(idx), 1).replace('%', chunk_hash)
116 for stream in dash_stream_info['streams
']:
117 # Use only one provider to avoid too many formats
118 provider = dash_stream_info['providers
'][0]
120 'url
': resolve_dash_template(
121 provider['url
'] + stream['initUrl
'], 0, dash_stream_info['hashes
']['0']),
123 for idx in range(dash_stream_info['videoLength
'] // dash_stream_info['chunkTime
']):
125 'url
': resolve_dash_template(
126 provider['url
'] + stream['segmentUrl
'], idx,
127 dash_stream_info['hashes
'][str(idx // 10 * 10)]),
129 content_type = stream['contentType
']
130 kind = content_type.split('/')[0]
132 'format_id
': join_nonempty(
133 'dash
', kind, str_or_none(stream.get('bitrate
'))),
134 'protocol
': 'http_dash_segments
',
135 # TODO: generate a MPD doc for external players?
136 'url
': encode_data_uri(b'<MPD
/>', 'text
/xml
'),
137 'ext
': mimetype2ext(content_type),
138 'height
': stream.get('height
'),
139 'width
': stream.get('width
'),
140 'fragments
': fragments,
144 'vcodec
': stream.get('codec
'),
146 'vbr
': stream.get('bitrate
'),
151 'acodec
': stream.get('codec
'),
152 'abr
': stream.get('bitrate
'),
157 def _real_extract(self, url):
158 m = self._match_valid_url(url)
159 video_id = m.group('id')
161 # some sites use this embed format (see: https://github.com/ytdl-org/youtube-dl/issues/2990)
162 if m.group('type') == 'embed
/recorded
':
163 video_id = m.group('id')
164 desktop_url = 'http
://www
.ustream
.tv
/recorded
/' + video_id
165 return self.url_result(desktop_url, 'Ustream
')
166 if m.group('type') == 'embed
':
167 video_id = m.group('id')
168 webpage = self._download_webpage(url, video_id)
169 content_video_ids = self._parse_json(self._search_regex(
170 r'ustream\
.vars\
.offAirContentVideoIds
=([^
;]+);', webpage,
171 'content video IDs
'), video_id)
172 return self.playlist_result(
173 (self.url_result('http
://www
.ustream
.tv
/recorded
/' + u, 'Ustream
') for u in content_video_ids),
176 params = self._download_json(
177 f'https
://api
.ustream
.tv
/videos
/{video_id}
.json
', video_id)
179 error = params.get('error
')
181 raise ExtractorError(
182 f'{self
.IE_NAME
} returned error
: {error}
', expected=True)
184 video = params['video
']
186 title = video['title
']
187 filesize = float_or_none(video.get('file_size
'))
193 'filesize
': filesize,
194 } for format_id, video_url in video['media_urls
'].items() if video_url]
197 hls_streams = self._get_streams(url, video_id, app_id_ver=(11, 2))
199 # m3u8_native leads to intermittent ContentTooShortError
200 formats.extend(self._extract_m3u8_formats(
201 hls_streams[0]['url
'], video_id, ext='mp4
', m3u8_id='hls
'))
204 # DASH streams handling is incomplete as 'url
' is missing
205 dash_streams = self._get_streams(url, video_id, app_id_ver=(3, 1))
207 formats.extend(self._parse_segmented_mp4(dash_streams))
210 description = video.get('description
')
211 timestamp = int_or_none(video.get('created_at
'))
212 duration = float_or_none(video.get('length
'))
213 view_count = int_or_none(video.get('views
'))
215 uploader = video.get('owner
', {}).get('username
')
216 uploader_id = video.get('owner
', {}).get('id')
220 'url
': thumbnail_url,
221 } for thumbnail_id, thumbnail_url in video.get('thumbnail
', {}).items()]
226 'description
': description,
227 'thumbnails
': thumbnails,
228 'timestamp
': timestamp,
229 'duration
': duration,
230 'view_count
': view_count,
231 'uploader
': uploader,
232 'uploader_id
': uploader_id,
237 class UstreamChannelIE(InfoExtractor):
238 _VALID_URL = r'https?
://(?
:www\
.)?ustream\
.tv
/channel
/(?P
<slug
>.+)'
239 IE_NAME = 'ustream
:channel
'
241 'url
': 'http
://www
.ustream
.tv
/channel
/channeljapan
',
245 'playlist_mincount
': 17,
248 def _real_extract(self, url):
249 m = self._match_valid_url(url)
250 display_id = m.group('slug
')
251 webpage = self._download_webpage(url, display_id)
252 channel_id = self._html_search_meta('ustream
:channel_id
', webpage)
254 BASE = 'http
://www
.ustream
.tv
'
255 next_url = f'/ajax
/socialstream
/videos
/{channel_id}
/1.json
'
258 reply = self._download_json(
259 urllib.parse.urljoin(BASE, next_url), display_id,
260 note=f'Downloading video
information (next
: {len(video_ids
) + 1})')
261 video_ids.extend(re.findall(r'data
-content
-id="(\d.*)"', reply['data
']))
262 next_url = reply['nextUrl
']
265 self.url_result('http
://www
.ustream
.tv
/recorded
/' + vid, 'Ustream
')
266 for vid in video_ids]
270 'display_id
': display_id,