5 from .common
import InfoExtractor
22 class KalturaIE(InfoExtractor
):
25 kaltura:(?P<partner_id>\w+):(?P<id>\w+)(?::(?P<player_type>\w+))?|
27 (:?(?:www|cdnapi(?:sec)?)\.)?kaltura\.com(?::\d+)?/
31 index\.php/(?:kwidget|extwidget/preview)|
33 html5/html5lib/[^/]+/mwEmbedFrame\.php
35 )(?:/(?P<path>[^?]+))?(?:\?(?P<query>.*))?
38 _SERVICE_URL
= 'http://cdnapi.kaltura.com'
39 _SERVICE_BASE
= '/api_v3/service/multirequest'
40 # See https://github.com/kaltura/server/blob/master/plugins/content/caption/base/lib/model/enums/CaptionType.php
48 'url': 'kaltura:269692:1_1jc2y3e4',
49 'md5': '3adcbdb3dcc02d647539e53f284ba171',
53 'title': 'Straight from the Heart',
54 'upload_date': '20131219',
55 'uploader_id': 'mlundberg@wolfgangsvault.com',
56 'description': 'The Allman Brothers Band, 12/16/1981',
57 'thumbnail': 're:^https?://.*/thumbnail/.*',
60 'skip': 'The access to this service is forbidden since the specified partner is blocked'
63 'url': 'http://www.kaltura.com/index.php/kwidget/cache_st/1300318621/wid/_269692/uiconf_id/3873291/entry_id/1_1jc2y3e4',
64 'only_matching': True,
67 'url': 'https://cdnapisec.kaltura.com/index.php/kwidget/wid/_557781/uiconf_id/22845202/entry_id/1_plr1syf3',
68 'only_matching': True,
71 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.30.2/mwEmbedFrame.php/p/1337/uiconf_id/20540612/entry_id/1_sf5ovm7u?wid=_243342',
72 'only_matching': True,
75 # video with subtitles
76 'url': 'kaltura:111032:1_cw786r8q',
77 'only_matching': True,
80 # video with ttml subtitles (no fileExt)
81 'url': 'kaltura:1926081:0_l5ye1133',
85 'title': 'What Can You Do With Python?',
86 'upload_date': '20160221',
87 'uploader_id': 'stork',
88 'thumbnail': 're:^https?://.*/thumbnail/.*',
96 'skip': 'Gone. Maybe https://www.safaribooksonline.com/library/tutorials/introduction-to-python-anon/3469/',
98 'skip_download': True,
102 'url': 'https://www.kaltura.com/index.php/extwidget/preview/partner_id/1770401/uiconf_id/37307382/entry_id/0_58u8kme7/embed/iframe?&flashvars[streamerType]=auto',
103 'only_matching': True,
106 'url': 'https://www.kaltura.com:443/index.php/extwidget/preview/partner_id/1770401/uiconf_id/37307382/entry_id/0_58u8kme7/embed/iframe?&flashvars[streamerType]=auto',
107 'only_matching': True,
110 # unavailable source format
111 'url': 'kaltura:513551:1_66x4rg7o',
112 'only_matching': True,
115 # html5lib URL using kwidget player
116 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.46/mwEmbedFrame.php/p/691292/uiconf_id/20499062/entry_id/0_c076mna6?wid=_691292&iframeembed=true&playerId=kaltura_player_1420508608&entry_id=0_c076mna6&flashvars%5BakamaiHD.loadingPolicy%5D=preInitialize&flashvars%5BakamaiHD.asyncInit%5D=true&flashvars%5BstreamerType%5D=hdnetwork',
120 'title': 'md5:4883e7acbcbf42583a2dddc97dee4855',
122 'uploader_id': 'commons@swinburne.edu.au',
123 'timestamp': 1408086874,
125 'upload_date': '20140815',
126 'thumbnail': 'http://cfvod.kaltura.com/p/691292/sp/69129200/thumbnail/entry_id/0_c076mna6/version/100022',
130 # html5lib playlist URL using kwidget player
131 'url': 'https://cdnapisec.kaltura.com/html5/html5lib/v2.89/mwEmbedFrame.php/p/2019031/uiconf_id/40436601?wid=1_4j3m32cv&iframeembed=true&playerId=kaltura_player_&flashvars[playlistAPI.kpl0Id]=1_jovey5nu&flashvars[ks]=&&flashvars[imageDefaultDuration]=30&flashvars[localizationCode]=en&flashvars[leadWithHTML5]=true&flashvars[forceMobileHTML5]=true&flashvars[nextPrevBtn.plugin]=true&flashvars[hotspots.plugin]=true&flashvars[sideBarContainer.plugin]=true&flashvars[sideBarContainer.position]=left&flashvars[sideBarContainer.clickToClose]=true&flashvars[chapters.plugin]=true&flashvars[chapters.layout]=vertical&flashvars[chapters.thumbnailRotator]=false&flashvars[streamSelector.plugin]=true&flashvars[EmbedPlayer.SpinnerTarget]=videoHolder&flashvars[dualScreen.plugin]=true&flashvars[playlistAPI.playlistUrl]=https://canvasgatechtest.kaf.kaltura.com/playlist/details/{playlistAPI.kpl0Id}/categoryid/126428551',
134 'title': '00-00 Introduction'
141 'title': 'CS7646_00-00 Introductio_Introduction',
143 'thumbnail': 'http://cfvod.kaltura.com/p/2019031/sp/201903100/thumbnail/entry_id/1_b1y5hlvx/version/100001',
145 'timestamp': 1533154447,
146 'upload_date': '20180801',
147 'uploader_id': 'djoyner3',
153 'title': 'CS7646_00-00 Introductio_Three parts to the course',
155 'thumbnail': 'http://cfvod.kaltura.com/p/2019031/sp/201903100/thumbnail/entry_id/1_jfb7mdpn/version/100001',
157 'timestamp': 1533154489,
158 'upload_date': '20180801',
159 'uploader_id': 'djoyner3',
165 'title': 'CS7646_00-00 Introductio_Textbooks',
167 'thumbnail': 'http://cfvod.kaltura.com/p/2019031/sp/201903100/thumbnail/entry_id/1_8xflxdp7/version/100001',
169 'timestamp': 1533154512,
170 'upload_date': '20180801',
171 'uploader_id': 'djoyner3',
177 'title': 'CS7646_00-00 Introductio_Prerequisites',
179 'thumbnail': 'http://cfvod.kaltura.com/p/2019031/sp/201903100/thumbnail/entry_id/1_3hqew8kn/version/100001',
181 'timestamp': 1533154536,
182 'upload_date': '20180801',
183 'uploader_id': 'djoyner3',
191 def _extract_embed_urls(cls
, url
, webpage
):
192 # Embed codes: https://knowledge.kaltura.com/embedding-kaltura-media-players-your-site
196 kWidget\.(?:thumb)?[Ee]mbed\(
198 (?P<q1>['"])wid(?P=q1)\s*:\s*
199 (?P<q2>['"])_?(?P<partner_id>(?:(?!(?P=q2)).)+)(?P=q2),.*?
200 (?P<q3>['"])entry_?[Ii]d(?P=q3)\s*:\s*
201 (?P<q4>['"])(?P<id>(?:(?!(?P=q4)).)+)(?P=q4)(?:,|\s*\})
206 (?:https?:)?//cdnapi(?:sec)?\.kaltura\.com(?::\d+)?/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+)(?:(?!(?P=q1)).)*
211 (?P<q2>["'])entry_?[Ii]d(?P=q2)
213 \[\s*(?P<q2_1>["'])entry_?[Ii]d(?P=q2_1)\s*\]\s*=\s*
215 (?P<q3>["'])(?P<id>(?:(?!(?P=q3)).)+)(?P=q3)
219 <(?:iframe[^>]+src|meta[^>]+\bcontent)=(?P<q1>["'])\s*
220 (?:https?:)?//(?:(?:www|cdnapi(?:sec)?)\.)?kaltura\.com/(?:(?!(?P=q1)).)*\b(?:p|partner_id)/(?P<partner_id>\d+)
222 [?&;]entry_id=(?P<id>(?:(?!(?P=q1))[^&])+)
228 for mobj
in finditer
:
229 embed_info
= mobj
.groupdict()
230 for k
, v
in embed_info
.items():
232 embed_info
[k
] = v
.strip()
233 embed_url
= 'kaltura:%(partner_id)s:%(id)s' % embed_info
234 escaped_pid
= re
.escape(embed_info
['partner_id'])
235 service_mobj
= re
.search(
236 r
'<script[^>]+src=(["\'])(?P
<id>(?
:https?
:)?
//(?
:(?
!\
1).)+)/p
/%s/sp
/%s00/embedIframeJs
' % (escaped_pid, escaped_pid),
239 embed_url = smuggle_url(embed_url, {'service_url
': service_mobj.group('id')})
240 urls.append(embed_url)
243 def _kaltura_api_call(self, video_id, actions, service_url=None, *args, **kwargs):
245 params.update({i: a for i, a in enumerate(actions[1:], start=1)})
247 data = self._download_json(
248 (service_url or self._SERVICE_URL) + self._SERVICE_BASE,
249 video_id, data=json.dumps(params).encode('utf
-8'),
251 'Content
-Type
': 'application
/json
',
252 'Accept
-Encoding
': 'gzip
, deflate
, br
',
255 for idx, status in enumerate(data):
256 if not isinstance(status, dict):
258 if status.get('objectType
') == 'KalturaAPIException
':
259 raise ExtractorError(
260 '%s said
: %s (%d)' % (self.IE_NAME, status['message
'], idx))
262 data[1] = traverse_obj(data, (1, 'objects
', 0))
266 def _get_video_info(self, video_id, partner_id, service_url=None, player_type='html5
'):
267 assert player_type in ('html5
', 'kwidget
')
268 if player_type == 'kwidget
':
269 return self._get_video_info_kwidget(video_id, partner_id, service_url)
271 return self._get_video_info_html5(video_id, partner_id, service_url)
273 def _get_video_info_html5(self, video_id, partner_id, service_url=None):
276 'apiVersion
': '3.3.0',
277 'clientTag
': 'html5
:v3
.1
.0',
278 'format
': 1, # JSON, 2 = XML, 3 = PHP
280 'partnerId
': partner_id,
284 'service
': 'session
',
285 'action
': 'startWidgetSession
',
286 'widgetId
': self._build_widget_id(partner_id),
291 'filter': {'redirectFromEntryId
': video_id},
292 'service
': 'baseentry
',
293 'ks
': '{1:result
:ks
}',
296 'fields
': 'createdAt
,dataUrl
,duration
,name
,plays
,thumbnailUrl
,userId
',
301 'action
': 'getbyentryid
',
303 'service
': 'flavorAsset
',
304 'ks
': '{1:result
:ks
}',
309 'filter:entryIdEqual
': video_id,
310 'service
': 'caption_captionasset
',
311 'ks
': '{1:result
:ks
}',
314 return self._kaltura_api_call(
315 video_id, actions, service_url, note='Downloading video info
JSON (Kaltura html5 player
)')
317 def _get_video_info_kwidget(self, video_id, partner_id, service_url=None):
320 'service
': 'multirequest
',
323 'clientTag
': 'kwidget
:v2
.89
',
324 'format
': 1, # JSON, 2 = XML, 3 = PHP
331 'service
': 'session
',
332 'action
': 'startWidgetSession
',
333 'widgetId
': self._build_widget_id(partner_id),
338 'service
': 'session
',
339 'action
': 'startwidgetsession
',
340 'widgetId
': self._build_widget_id(partner_id),
343 'clientTag
': 'kwidget
:v2
.89
',
345 'ks
': '{1:result
:ks
}'
350 'filter': {'redirectFromEntryId
': video_id},
351 'service
': 'baseentry
',
352 'ks
': '{1:result
:ks
}',
355 'fields
': 'createdAt
,dataUrl
,duration
,name
,plays
,thumbnailUrl
,userId
',
360 'action
': 'getbyentryid
',
362 'service
': 'flavorAsset
',
363 'ks
': '{1:result
:ks
}',
368 'filter:entryIdEqual
': video_id,
369 'service
': 'caption_captionasset
',
370 'ks
': '{1:result
:ks
}',
373 # second object (representing the second start widget session) is None
374 header, _, _info, flavor_assets, captions = self._kaltura_api_call(
375 video_id, actions, service_url, note='Downloading video info
JSON (Kaltura kwidget player
)')
376 info = _info['objects
'][0]
377 return header, info, flavor_assets, captions
379 def _build_widget_id(self, partner_id):
380 return partner_id if '_
' in partner_id else f'_{partner_id}
'
382 IFRAME_PACKAGE_DATA_REGEX = r'window\
.kalturaIframePackageData\s
*='
384 def _real_extract(self, url):
385 url, smuggled_data = unsmuggle_url(url, {})
387 mobj = self._match_valid_url(url)
388 partner_id, entry_id, player_type = mobj.group('partner_id
', 'id', 'player_type
')
389 ks, captions = None, None
391 player_type = 'kwidget
' if 'html5lib
/v2
' in url else 'html5
'
392 if partner_id and entry_id:
393 _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, smuggled_data.get('service_url
'), player_type=player_type)
395 path, query = mobj.group('path
', 'query
')
396 if not path and not query:
397 raise ExtractorError('Invalid URL
', expected=True)
400 params = compat_parse_qs(query)
402 splitted_path = path.split('/')
403 params.update(dict((zip(splitted_path[::2], [[v] for v in splitted_path[1::2]]))))
405 partner_id = remove_start(params['wid
'][0], '_
')
407 partner_id = params['p
'][0]
408 elif 'partner_id
' in params:
409 partner_id = params['partner_id
'][0]
411 raise ExtractorError('Invalid URL
', expected=True)
412 if 'entry_id
' in params:
413 entry_id = params['entry_id
'][0]
414 _, info, flavor_assets, captions = self._get_video_info(entry_id, partner_id, player_type=player_type)
415 elif 'uiconf_id
' in params and 'flashvars
[referenceId
]' in params:
416 reference_id = params['flashvars
[referenceId
]'][0]
417 webpage = self._download_webpage(url, reference_id)
418 entry_data = self._search_json(
419 self.IFRAME_PACKAGE_DATA_REGEX, webpage,
420 'kalturaIframePackageData
', reference_id)['entryResult
']
421 info, flavor_assets = entry_data['meta
'], entry_data['contextData
']['flavorAssets
']
422 entry_id = info['id']
423 # Unfortunately, data returned in kalturaIframePackageData lacks
424 # captions so we will try requesting the complete data using
425 # regular approach since we now know the entry_id
427 _, info, flavor_assets, captions = self._get_video_info(
428 entry_id, partner_id, player_type=player_type)
429 except ExtractorError:
430 # Regular scenario failed but we already have everything
431 # extracted apart from captions and can process at least
434 elif 'uiconf_id
' in params and 'flashvars
[playlistAPI
.kpl0Id
]' in params:
435 playlist_id = params['flashvars
[playlistAPI
.kpl0Id
]'][0]
436 webpage = self._download_webpage(url, playlist_id)
437 playlist_data = self._search_json(
438 self.IFRAME_PACKAGE_DATA_REGEX, webpage,
439 'kalturaIframePackageData
', playlist_id)['playlistResult
']
440 return self.playlist_from_matches(
441 traverse_obj(playlist_data, (playlist_id, 'items
', ..., 'id')),
442 playlist_id, traverse_obj(playlist_data, (playlist_id, 'name
')),
443 ie=KalturaIE, getter=lambda x: f'kaltura
:{partner_id}
:{x}
:{player_type}
')
445 raise ExtractorError('Invalid URL
', expected=True)
446 ks = params.get('flashvars
[ks
]', [None])[0]
448 return self._per_video_extract(smuggled_data, entry_id, info, ks, flavor_assets, captions)
450 def _per_video_extract(self, smuggled_data, entry_id, info, ks, flavor_assets, captions):
451 source_url = smuggled_data.get('source_url
')
453 referrer = base64.b64encode(
454 '://'.join(compat_urlparse.urlparse(source_url)[:2])
455 .encode('utf
-8')).decode('utf
-8')
459 def sign_url(unsigned_url):
461 unsigned_url += '/ks
/%s' % ks
463 unsigned_url += '?referrer
=%s' % referrer
466 data_url = info['dataUrl
']
467 if '/flvclipper
/' in data_url:
468 data_url = re.sub(r'/flvclipper
/.*', '/serveFlavor
', data_url)
472 for f in flavor_assets:
473 # Continue if asset is not ready
474 if f.get('status
') != 2:
476 # Original format that's
not available (e
.g
. kaltura
:1926081:0_c
03e
1b
5g
)
478 if f
.get('fileExt') == 'chun':
480 # DRM-protected video, cannot be decrypted
481 if not self
.get_param('allow_unplayable_formats') and f
.get('fileExt') == 'wvm':
483 if not f
.get('fileExt'):
484 # QT indicates QuickTime; some videos have broken fileExt
485 if f
.get('containerFormat') == 'qt':
489 video_url
= sign_url(
490 '%s/flavorId/%s' % (data_url
, f
['id']))
491 format_id
= '%(fileExt)s-%(bitrate)s' % f
492 # Source format may not be available (e.g. kaltura:513551:1_66x4rg7o)
493 if f
.get('isOriginal') is True and not self
._is
_valid
_url
(
494 video_url
, entry_id
, format_id
):
496 # audio-only has no videoCodecId (e.g. kaltura:1926081:0_c03e1b5g
498 vcodec
= 'none' if 'videoCodecId' not in f
and f
.get(
499 'frameRate') == 0 else f
.get('videoCodecId')
501 'format_id': format_id
,
502 'ext': f
.get('fileExt'),
503 'tbr': int_or_none(f
['bitrate']),
504 'fps': int_or_none(f
.get('frameRate')),
505 'filesize_approx': int_or_none(f
.get('size'), invscale
=1024),
506 'container': f
.get('containerFormat'),
508 'height': int_or_none(f
.get('height')),
509 'width': int_or_none(f
.get('width')),
512 if '/playManifest/' in data_url
:
513 m3u8_url
= sign_url(data_url
.replace(
514 'format/url', 'format/applehttp'))
515 fmts
, subs
= self
._extract
_m
3u8_formats
_and
_subtitles
(
516 m3u8_url
, entry_id
, 'mp4', 'm3u8_native',
517 m3u8_id
='hls', fatal
=False)
519 self
._merge
_subtitles
(subs
, target
=subtitles
)
522 for caption
in captions
.get('objects', []):
523 # Continue if caption is not ready
524 if caption
.get('status') != 2:
526 if not caption
.get('id'):
528 caption_format
= int_or_none(caption
.get('format'))
529 subtitles
.setdefault(caption
.get('languageCode') or caption
.get('language'), []).append({
530 'url': '%s/api_v3/service/caption_captionasset/action/serve/captionAssetId/%s' % (self
._SERVICE
_URL
, caption
['id']),
531 'ext': caption
.get('fileExt') or self
._CAPTION
_TYPES
.get(caption_format
) or 'ttml',
536 'title': info
['name'],
538 'subtitles': subtitles
,
539 'description': clean_html(info
.get('description')),
540 'thumbnail': info
.get('thumbnailUrl'),
541 'duration': info
.get('duration'),
542 'timestamp': info
.get('createdAt'),
543 'uploader_id': format_field(info
, 'userId', ignore
=('None', None)),
544 'view_count': int_or_none(info
.get('plays')),