2 from __future__
import unicode_literals
8 from .common
import InfoExtractor
9 from .adobepass
import AdobePassIE
10 from ..compat
import (
11 compat_etree_fromstring
,
13 compat_urllib_parse_urlparse
,
15 compat_xml_parse_error
,
37 class BrightcoveLegacyIE(InfoExtractor
):
38 IE_NAME
= 'brightcove:legacy'
39 _VALID_URL
= r
'(?:https?://.*brightcove\.com/(services|viewer).*?\?|brightcove:)(?P<query>.*)'
43 # From http://www.8tv.cat/8aldia/videos/xavier-sala-i-martin-aquesta-tarda-a-8-al-dia/
44 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1654948606001&flashID=myExperience&%40videoPlayer=2371591881001',
45 'md5': '5423e113865d26e40624dce2e4b45d95',
46 'note': 'Test Brightcove downloads and detection in GenericIE',
48 'id': '2371591881001',
50 'title': 'Xavier Sala i Martín: “Un banc que no presta és un banc zombi que no serveix per a res”',
52 'description': 'md5:a950cc4285c43e44d763d036710cd9cd',
53 'timestamp': 1368213670,
54 'upload_date': '20130510',
55 'uploader_id': '1589608506001',
57 'skip': 'The player has been deactivated by the content owner',
60 # From http://medianetwork.oracle.com/video/player/1785452137001
61 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1217746023001&flashID=myPlayer&%40videoPlayer=1785452137001',
63 'id': '1785452137001',
65 'title': 'JVMLS 2012: Arrays 2.0 - Opportunities and Challenges',
66 'description': 'John Rose speaks at the JVM Language Summit, August 1, 2012.',
68 'timestamp': 1344975024,
69 'upload_date': '20120814',
70 'uploader_id': '1460825906',
72 'skip': 'video not playable',
75 # From http://mashable.com/2013/10/26/thermoelectric-bracelet-lets-you-control-your-body-temperature/
76 'url': 'http://c.brightcove.com/services/viewer/federated_f9?&playerID=1265504713001&publisherID=AQ%7E%7E%2CAAABBzUwv1E%7E%2CxP-xFHVUstiMFlNYfvF4G9yFnNaqCw_9&videoID=2750934548001',
78 'id': '2750934548001',
80 'title': 'This Bracelet Acts as a Personal Thermostat',
81 'description': 'md5:547b78c64f4112766ccf4e151c20b6a0',
82 # 'uploader': 'Mashable',
83 'timestamp': 1382041798,
84 'upload_date': '20131017',
85 'uploader_id': '1130468786001',
89 # test that the default referer works
90 # from http://national.ballet.ca/interact/video/Lost_in_Motion_II/
91 'url': 'http://link.brightcove.com/services/player/bcpid756015033001?bckey=AQ~~,AAAApYJi_Ck~,GxhXCegT1Dp39ilhXuxMJxasUhVNZiil&bctid=2878862109001',
93 'id': '2878862109001',
95 'title': 'Lost in Motion II',
96 'description': 'md5:363109c02998fee92ec02211bd8000df',
97 'uploader': 'National Ballet of Canada',
102 # test flv videos served by akamaihd.net
103 # From http://www.redbull.com/en/bike/stories/1331655643987/replay-uci-dh-world-cup-2014-from-fort-william
104 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?%40videoPlayer=ref%3Aevent-stream-356&linkBaseURL=http%3A%2F%2Fwww.redbull.com%2Fen%2Fbike%2Fvideos%2F1331655630249%2Freplay-uci-fort-william-2014-dh&playerKey=AQ%7E%7E%2CAAAApYJ7UqE%7E%2Cxqr_zXk0I-zzNndy8NlHogrCb5QdyZRf&playerID=1398061561001#__youtubedl_smuggle=%7B%22Referer%22%3A+%22http%3A%2F%2Fwww.redbull.com%2Fen%2Fbike%2Fstories%2F1331655643987%2Freplay-uci-dh-world-cup-2014-from-fort-william%22%7D',
105 # The md5 checksum changes on each download
107 'id': '3750436379001',
109 'title': 'UCI MTB World Cup 2014: Fort William, UK - Downhill Finals',
110 'uploader': 'RBTV Old (do not use)',
111 'description': 'UCI MTB World Cup 2014: Fort William, UK - Downhill Finals',
112 'timestamp': 1409122195,
113 'upload_date': '20140827',
114 'uploader_id': '710858724001',
116 'skip': 'Video gone',
119 # playlist with 'videoList'
120 # from http://support.brightcove.com/en/video-cloud/docs/playlist-support-single-video-players
121 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=3550052898001&playerKey=AQ%7E%7E%2CAAABmA9XpXk%7E%2C-Kp7jNgisre1fG5OdqpAFUTcs0lP_ZoL',
124 'id': '3550319591001',
126 'playlist_mincount': 7,
127 'skip': 'Unsupported URL',
130 # playlist with 'playlistTab' (https://github.com/ytdl-org/youtube-dl/issues/9965)
131 'url': 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=AQ%7E%7E,AAABXlLMdok%7E,NJ4EoMlZ4rZdx9eU1rkMVd8EaYPBBUlg',
133 'id': '1522758701001',
134 'title': 'Lesson 08',
136 'playlist_mincount': 10,
137 'skip': 'Unsupported URL',
140 # playerID inferred from bcpid
141 # from http://www.un.org/chinese/News/story.asp?NewsID=27724
142 'url': 'https://link.brightcove.com/services/player/bcpid1722935254001/?bctid=5360463607001&autoStart=false&secureConnections=true&width=650&height=350',
143 'only_matching': True, # Tested in GenericIE
148 def _build_brighcove_url(cls
, object_str
):
150 Build a Brightcove url from a xml string containing
151 <object class="BrightcoveExperience">{params}</object>
154 # Fix up some stupid HTML, see https://github.com/ytdl-org/youtube-dl/issues/1553
155 object_str
= re
.sub(r
'(<param(?:\s+[a-zA-Z0-9_]+="[^"]*")*)>',
156 lambda m
: m
.group(1) + '/>', object_str
)
157 # Fix up some stupid XML, see https://github.com/ytdl-org/youtube-dl/issues/1608
158 object_str
= object_str
.replace('<--', '<!--')
159 # remove namespace to simplify extraction
160 object_str
= re
.sub(r
'(<object[^>]*)(xmlns=".*?")', r
'\1', object_str
)
161 object_str
= fix_xml_ampersands(object_str
)
164 object_doc
= compat_etree_fromstring(object_str
.encode('utf-8'))
165 except compat_xml_parse_error
:
168 fv_el
= find_xpath_attr(object_doc
, './param', 'name', 'flashVars')
169 if fv_el
is not None:
172 for k
, v
in compat_parse_qs(fv_el
.attrib
['value']).items())
176 data_url
= object_doc
.attrib
.get('data', '')
177 data_url_params
= compat_parse_qs(compat_urllib_parse_urlparse(data_url
).query
)
179 def find_param(name
):
180 if name
in flashvars
:
181 return flashvars
[name
]
182 node
= find_xpath_attr(object_doc
, './param', 'name', name
)
184 return node
.attrib
['value']
185 return data_url_params
.get(name
)
189 playerID
= find_param('playerID') or find_param('playerId')
191 raise ExtractorError('Cannot find player ID')
192 params
['playerID'] = playerID
194 playerKey
= find_param('playerKey')
195 # Not all pages define this value
196 if playerKey
is not None:
197 params
['playerKey'] = playerKey
198 # These fields hold the id of the video
199 videoPlayer
= find_param('@videoPlayer') or find_param('videoId') or find_param('videoID') or find_param('@videoList')
200 if videoPlayer
is not None:
201 if isinstance(videoPlayer
, list):
202 videoPlayer
= videoPlayer
[0]
203 videoPlayer
= videoPlayer
.strip()
204 # UUID is also possible for videoPlayer (e.g.
205 # http://www.popcornflix.com/hoodies-vs-hooligans/7f2d2b87-bbf2-4623-acfb-ea942b4f01dd
206 # or http://www8.hp.com/cn/zh/home.html)
208 r
'^(?:\d+|[\da-fA-F]{8}-?[\da-fA-F]{4}-?[\da-fA-F]{4}-?[\da-fA-F]{4}-?[\da-fA-F]{12})$',
209 videoPlayer
) or videoPlayer
.startswith('ref:')):
211 params
['@videoPlayer'] = videoPlayer
212 linkBase
= find_param('linkBaseURL')
213 if linkBase
is not None:
214 params
['linkBaseURL'] = linkBase
215 return cls
._make
_brightcove
_url
(params
)
218 def _build_brighcove_url_from_js(cls
, object_js
):
219 # The layout of JS is as follows:
220 # customBC.createVideo = function (width, height, playerID, playerKey, videoPlayer, VideoRandomID) {
221 # // build Brightcove <object /> XML
224 r
'''(?x)customBC\.createVideo\(
225 .*? # skipping width and height
226 ["\'](?P
<playerID
>\d
+)["\']\s*,\s* # playerID
227 ["\'](?P
<playerKey
>AQ
[^
"\']{48})[^"\']*["\']\s*,\s* # playerKey begins with AQ and is 50 characters
228 # in length, however it's appended to itself
229 # in places, so truncate
230 ["\'](?P
<videoID
>\d
+)["\'] # @videoPlayer
233 return cls._make_brightcove_url(m.groupdict())
236 def _make_brightcove_url(cls, params):
237 return update_url_query(
238 'http://c.brightcove.com/services/viewer/htmlFederated', params)
241 def _extract_brightcove_url(cls, webpage):
242 """Try to extract the brightcove url from the webpage, returns None
245 urls = cls._extract_brightcove_urls(webpage)
246 return urls[0] if urls else None
249 def _extract_brightcove_urls(cls, webpage):
250 """Return a list of all Brightcove URLs from the webpage """
255 (?:property|itemprop)=([\'"])(?
:og
:video|embedURL
)\
1[^
>]+
256 content
=([\'"])(?P<url>https?://(?:secure|c)\.brightcove.com/(?:(?!\2).)+)\2
259 url = unescapeHTML(url_m.group('url'))
260 # Some sites don't add it, we can't download with this url, for example:
261 # http://www.ktvu.com/videos/news/raw-video-caltrain-releases-video-of-man-almost/vCTZdY/
262 if 'playerKey' in url or 'videoId' in url or 'idVideo' in url:
265 matches = re.findall(
268 [^>]+?class=[\'"][^
>]*?BrightcoveExperience
.*?
[\'"] |
269 [^>]*?>\s*<param\s+name="movie
"\s+value="https?
://[^
/]*brightcove\
.com
/
270 ).+?
>\s
*</object>''',
273 return list(filter(None, [cls._build_brighcove_url(m) for m in matches]))
275 matches = re.findall(r'(customBC\.createVideo\(.+?\);)', webpage)
277 return list(filter(None, [
278 cls._build_brighcove_url_from_js(custom_bc)
279 for custom_bc in matches]))
280 return [src for _, src in re.findall(
281 r'<iframe[^>]+src=([\'"])((?:https?:)?//link\.brightcove\.com/services/player/(?!\1).+)\1', webpage)]
283 def _real_extract(self, url):
284 url, smuggled_data = unsmuggle_url(url, {})
286 # Change the 'videoId' and others field to '@videoPlayer'
287 url = re.sub(r'(?<=[?&])(videoI(d|D)|idVideo|bctid)', '%40videoPlayer', url)
288 # Change bckey (used by bcove.me urls) to playerKey
289 url = re.sub(r'(?<=[?&])bckey', 'playerKey', url)
290 mobj = re.match(self._VALID_URL, url)
291 query_str = mobj.group('query')
292 query = compat_urlparse.parse_qs(query_str)
294 videoPlayer = query.get('@videoPlayer')
296 # We set the original url as the default 'Referer' header
297 referer = query.get('linkBaseURL', [None])[0] or smuggled_data.get('Referer', url)
298 video_id = videoPlayer[0]
299 if 'playerID' not in query:
300 mobj = re.search(r'/bcpid(\d+)', url)
302 query['playerID'] = [mobj.group(1)]
303 publisher_id = query.get('publisherId')
304 if publisher_id and publisher_id[0].isdigit():
305 publisher_id = publisher_id[0]
307 player_key = query.get('playerKey')
308 if player_key and ',' in player_key[0]:
309 player_key = player_key[0]
311 player_id = query.get('playerID')
312 if player_id and player_id[0].isdigit():
315 headers['Referer'] = referer
316 player_page = self._download_webpage(
317 'http://link.brightcove.com/services/player/bcpid' + player_id[0],
318 video_id, headers=headers, fatal=False)
320 player_key = self._search_regex(
321 r'<param\s+name="playerKey"\s+value="([\w~,-]+)"',
322 player_page, 'player key', fatal=False)
324 enc_pub_id = player_key.split(',')[1].replace('~', '=')
325 publisher_id = struct.unpack('>Q', base64.urlsafe_b64decode(enc_pub_id))[0]
327 brightcove_new_url = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s' % (publisher_id, video_id)
329 brightcove_new_url = smuggle_url(brightcove_new_url, {'referrer': referer})
330 return self.url_result(brightcove_new_url, BrightcoveNewIE.ie_key(), video_id)
331 # TODO: figure out if it's possible to extract playlistId from playerKey
332 # elif 'playerKey' in query:
333 # player_key = query['playerKey']
334 # return self._get_playlist_info(player_key[0])
335 raise UnsupportedError(url)
338 class BrightcoveNewIE(AdobePassIE):
339 IE_NAME = 'brightcove:new'
340 _VALID_URL = r'https?://players\.brightcove\.net/(?P<account_id>\d+)/(?P<player_id>[^/]+)_(?P<embed>[^/]+)/index\.html\?.*(?P<content_type>video|playlist)Id=(?P<video_id>\d+|ref:[^&]+)'
342 'url': 'http://players.brightcove.net/929656772001/e41d32dc-ec74-459e-a845-6c69f7b724ea_default/index.html?videoId=4463358922001',
343 'md5': 'c8100925723840d4b0d243f7025703be',
345 'id': '4463358922001',
347 'title': 'Meet the man behind Popcorn Time',
348 'description': 'md5:eac376a4fe366edc70279bfb681aea16',
350 'timestamp': 1441391203,
351 'upload_date': '20150904',
352 'uploader_id': '929656772001',
353 'formats': 'mincount:20',
357 'url': 'http://players.brightcove.net/4036320279001/5d112ed9-283f-485f-a7f9-33f42e8bc042_default/index.html?videoId=4279049078001',
359 'id': '4279049078001',
361 'title': 'Titansgrave: Chapter 0',
362 'description': 'Titansgrave: Chapter 0',
363 'duration': 1242.058,
364 'timestamp': 1433556729,
365 'upload_date': '20150606',
366 'uploader_id': '4036320279001',
367 'formats': 'mincount:39',
371 'skip_download': True,
375 'url': 'https://players.brightcove.net/1752604059001/S13cJdUBz_default/index.html?playlistId=5718313430001',
377 'id': '5718313430001',
378 'title': 'No Audio Playlist',
383 'skip_download': True,
386 'url': 'http://players.brightcove.net/5690807595001/HyZNerRl7_default/index.html?playlistId=5743160747001',
387 'only_matching': True,
389 # ref: prefixed video id
390 'url': 'http://players.brightcove.net/3910869709001/21519b5c-4b3b-4363-accb-bdc8f358f823_default/index.html?videoId=ref:7069442',
391 'only_matching': True,
393 # non numeric ref: prefixed video id
394 'url': 'http://players.brightcove.net/710858724001/default_default/index.html?videoId=ref:event-stream-356',
395 'only_matching': True,
397 # unavailable video without message but with error_code
398 'url': 'http://players.brightcove.net/1305187701/c832abfb-641b-44eb-9da0-2fe76786505f_default/index.html?videoId=4377407326001',
399 'only_matching': True,
403 def _extract_url(ie, webpage):
404 urls = BrightcoveNewIE._extract_urls(ie, webpage)
405 return urls[0] if urls else None
408 def _extract_urls(ie, webpage):
410 # 1. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#setvideoiniframe
411 # 2. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#tag
412 # 3. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#setvideousingjavascript
413 # 4. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/in-page-embed-player-implementation.html
414 # 5. https://support.brightcove.com/en/video-cloud/docs/dynamically-assigning-videos-player
418 # Look for iframe embeds [1]
419 for _, url in re.findall(
420 r'<iframe[^>]+src=(["\'])((?:https?:)?//players\.brightcove\.net/\d+/[^/]+/index\.html.+?)\1', webpage):
421 entries.append(url if url.startswith('http') else 'http:' + url)
423 # Look for <video> tags [2] and embed_in_page embeds [3]
425 for video, script_tag, account_id, player_id, embed in re.findall(
427 (<video\s
+[^
>]*\bdata
-video
-id\s
*=\s
*['"]?[^>]+>)
430 src=["\'](?:https?:)?//players\.brightcove\.net/
431 (\d+)/([^/]+)_([^/]+)/index(?:\.min)?\.js
435 attrs = extract_attributes(video)
437 # According to examples from [4] it's unclear whether video
id
438 # may be optional and what to do when it is
439 video_id
= attrs
.get('data-video-id')
443 account_id
= account_id
or attrs
.get('data-account')
447 player_id
= player_id
or attrs
.get('data-player') or 'default'
448 embed
= embed
or attrs
.get('data-embed') or 'default'
450 bc_url
= 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s' % (
451 account_id
, player_id
, embed
, video_id
)
453 # Some brightcove videos may be embedded with video tag only and
454 # without script tag or any mentioning of brightcove at all. Such
455 # embeds are considered ambiguous since they are matched based only
456 # on data-video-id and data-account attributes and in the wild may
457 # not be brightcove embeds at all. Let's check reconstructed
458 # brightcove URLs in case of such embeds and only process valid
459 # ones. By this we ensure there is indeed a brightcove embed.
460 if not script_tag
and not ie
._is
_valid
_url
(
461 bc_url
, video_id
, 'possible brightcove video'):
464 entries
.append(bc_url
)
468 def _parse_brightcove_metadata(self
, json_data
, video_id
, headers
={}):
469 title
= json_data
['name'].strip()
472 for source
in json_data
.get('sources', []):
473 container
= source
.get('container')
474 ext
= mimetype2ext(source
.get('type'))
475 src
= source
.get('src')
476 # https://support.brightcove.com/playback-api-video-fields-reference#key_systems_object
477 if ext
== 'ism' or container
== 'WVM' or source
.get('key_systems'):
479 elif ext
== 'm3u8' or container
== 'M2TS':
482 formats
.extend(self
._extract
_m
3u8_formats
(
483 src
, video_id
, 'mp4', 'm3u8_native', m3u8_id
='hls', fatal
=False))
487 formats
.extend(self
._extract
_mpd
_formats
(src
, video_id
, 'dash', fatal
=False))
489 streaming_src
= source
.get('streaming_src')
490 stream_name
, app_name
= source
.get('stream_name'), source
.get('app_name')
491 if not src
and not streaming_src
and (not stream_name
or not app_name
):
493 tbr
= float_or_none(source
.get('avg_bitrate'), 1000)
494 height
= int_or_none(source
.get('height'))
495 width
= int_or_none(source
.get('width'))
498 'filesize': int_or_none(source
.get('size')),
499 'container': container
,
500 'ext': ext
or container
.lower(),
502 if width
== 0 and height
== 0:
510 'vcodec': source
.get('codec'),
513 def build_format_id(kind
):
516 format_id
+= '-%dk' % int(tbr
)
518 format_id
+= '-%dp' % height
521 if src
or streaming_src
:
523 'url': src
or streaming_src
,
524 'format_id': build_format_id('http' if src
else 'http-streaming'),
525 'source_preference': 0 if src
else -1,
530 'play_path': stream_name
,
531 'format_id': build_format_id('rtmp'),
535 # for sonyliv.com DRM protected videos
536 s3_source_url
= json_data
.get('custom_fields', {}).get('s3sourceurl')
539 'url': s3_source_url
,
540 'format_id': 'source',
543 errors
= json_data
.get('errors')
544 if not formats
and errors
:
546 raise ExtractorError(
547 error
.get('message') or error
.get('error_subcode') or error
['error_code'], expected
=True)
549 self
._sort
_formats
(formats
)
552 f
.setdefault('http_headers', {}).update(headers
)
555 for text_track
in json_data
.get('text_tracks', []):
556 if text_track
.get('src'):
557 subtitles
.setdefault(text_track
.get('srclang'), []).append({
558 'url': text_track
['src'],
562 duration
= float_or_none(json_data
.get('duration'), 1000)
563 if duration
is not None and duration
<= 0:
568 'title': self
._live
_title
(title
) if is_live
else title
,
569 'description': clean_html(json_data
.get('description')),
570 'thumbnail': json_data
.get('thumbnail') or json_data
.get('poster'),
571 'duration': duration
,
572 'timestamp': parse_iso8601(json_data
.get('published_at')),
573 'uploader_id': json_data
.get('account_id'),
575 'subtitles': subtitles
,
576 'tags': json_data
.get('tags', []),
580 def _real_extract(self
, url
):
581 url
, smuggled_data
= unsmuggle_url(url
, {})
582 self
._initialize
_geo
_bypass
({
583 'countries': smuggled_data
.get('geo_countries'),
584 'ip_blocks': smuggled_data
.get('geo_ip_blocks'),
587 account_id
, player_id
, embed
, content_type
, video_id
= re
.match(self
._VALID
_URL
, url
).groups()
589 webpage
= self
._download
_webpage
(
590 'http://players.brightcove.net/%s/%s_%s/index.min.js'
591 % (account_id
, player_id
, embed
), video_id
)
595 catalog
= self
._search
_regex
(
596 r
'catalog\(({.+?})\);', webpage
, 'catalog', default
=None)
598 catalog
= self
._parse
_json
(
599 js_to_json(catalog
), video_id
, fatal
=False)
601 policy_key
= catalog
.get('policyKey')
604 policy_key
= self
._search
_regex
(
605 r
'policyKey\s*:\s*(["\'])(?P
<pk
>.+?
)\
1',
606 webpage, 'policy key
', group='pk
')
608 api_url = 'https
://edge
.api
.brightcove
.com
/playback
/v1
/accounts
/%s/%ss/%s' % (account_id, content_type, video_id)
610 'Accept
': 'application
/json
;pk
=%s' % policy_key,
612 referrer = smuggled_data.get('referrer
')
616 'Origin
': re.search(r'https?
://[^
/]+', referrer).group(0),
619 json_data = self._download_json(api_url, video_id, headers=headers)
620 except ExtractorError as e:
621 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
622 json_data = self._parse_json(e.cause.read().decode(), video_id)[0]
623 message = json_data.get('message
') or json_data['error_code
']
624 if json_data.get('error_subcode
') == 'CLIENT_GEO
':
625 self.raise_geo_restricted(msg=message)
626 raise ExtractorError(message, expected=True)
629 errors = json_data.get('errors
')
630 if errors and errors[0].get('error_subcode
') == 'TVE_AUTH
':
631 custom_fields = json_data['custom_fields
']
632 tve_token = self._extract_mvpd_auth(
633 smuggled_data['source_url
'], video_id,
634 custom_fields['bcadobepassrequestorid
'],
635 custom_fields['bcadobepassresourceid
'])
636 json_data = self._download_json(
637 api_url, video_id, headers={
638 'Accept
': 'application
/json
;pk
=%s' % policy_key
640 'tveToken
': tve_token,
643 if content_type == 'playlist
':
644 return self.playlist_result(
645 [self._parse_brightcove_metadata(vid, vid.get('id'), headers)
646 for vid in json_data.get('videos
', []) if vid.get('id')],
647 json_data.get('id'), json_data.get('name
'),
648 json_data.get('description
'))
650 return self._parse_brightcove_metadata(
651 json_data, video_id, headers=headers)