]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/brightcove.py
Update to ytdl-2021.01.03
[yt-dlp.git] / youtube_dlc / extractor / brightcove.py
CommitLineData
dcdb292f 1# coding: utf-8
400e5810 2from __future__ import unicode_literals
592882aa 3
160c2773 4import base64
160c2773
RA
5import re
6import struct
fbaaad49 7
5996d21a 8from .adobepass import AdobePassIE
c8b232cc 9from .common import InfoExtractor
1cc79574 10from ..compat import (
36e6f62c 11 compat_etree_fromstring,
c8b232cc 12 compat_HTTPError,
7b0817e8 13 compat_parse_qs,
37aab278 14 compat_urllib_parse_urlparse,
1cc79574 15 compat_urlparse,
e20d0c1e 16 compat_xml_parse_error,
1cc79574
PH
17)
18from ..utils import (
c8b232cc 19 clean_html,
40158f55 20 extract_attributes,
c8b232cc 21 ExtractorError,
1cc79574
PH
22 find_xpath_attr,
23 fix_xml_ampersands,
a6624898 24 float_or_none,
ed126900 25 int_or_none,
c8b232cc
S
26 js_to_json,
27 mimetype2ext,
ed126900 28 parse_iso8601,
f455a934 29 smuggle_url,
b4eb0bc7 30 str_or_none,
29f7c58a 31 try_get,
fd91257c
S
32 unescapeHTML,
33 unsmuggle_url,
c8b232cc 34 UnsupportedError,
48254c3f 35 update_url_query,
b4eb0bc7 36 url_or_none,
cfe50f04 37)
fbaaad49 38
dd5bcdc4 39
4fcaa4f4 40class BrightcoveLegacyIE(InfoExtractor):
e721d857 41 IE_NAME = 'brightcove:legacy'
ec05fee4 42 _VALID_URL = r'(?:https?://.*brightcove\.com/(services|viewer).*?\?|brightcove:)(?P<query>.*)'
592882aa
JMF
43
44 _TESTS = [
45 {
4de1994b 46 # From http://www.8tv.cat/8aldia/videos/xavier-sala-i-martin-aquesta-tarda-a-8-al-dia/
400e5810 47 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1654948606001&flashID=myExperience&%40videoPlayer=2371591881001',
400e5810
PH
48 'md5': '5423e113865d26e40624dce2e4b45d95',
49 'note': 'Test Brightcove downloads and detection in GenericIE',
50 'info_dict': {
96bef88f
JMF
51 'id': '2371591881001',
52 'ext': 'mp4',
400e5810
PH
53 'title': 'Xavier Sala i Martín: “Un banc que no presta és un banc zombi que no serveix per a res”',
54 'uploader': '8TV',
55 'description': 'md5:a950cc4285c43e44d763d036710cd9cd',
41f5492f 56 'timestamp': 1368213670,
57 'upload_date': '20130510',
4d4cd35f 58 'uploader_id': '1589608506001',
f455a934
RA
59 },
60 'skip': 'The player has been deactivated by the content owner',
592882aa
JMF
61 },
62 {
4de1994b 63 # From http://medianetwork.oracle.com/video/player/1785452137001
400e5810 64 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1217746023001&flashID=myPlayer&%40videoPlayer=1785452137001',
400e5810 65 'info_dict': {
96bef88f
JMF
66 'id': '1785452137001',
67 'ext': 'flv',
400e5810
PH
68 'title': 'JVMLS 2012: Arrays 2.0 - Opportunities and Challenges',
69 'description': 'John Rose speaks at the JVM Language Summit, August 1, 2012.',
70 'uploader': 'Oracle',
41f5492f 71 'timestamp': 1344975024,
72 'upload_date': '20120814',
4d4cd35f 73 'uploader_id': '1460825906',
592882aa 74 },
f455a934 75 'skip': 'video not playable',
592882aa 76 },
fc4a0c2a
JMF
77 {
78 # From http://mashable.com/2013/10/26/thermoelectric-bracelet-lets-you-control-your-body-temperature/
400e5810
PH
79 'url': 'http://c.brightcove.com/services/viewer/federated_f9?&playerID=1265504713001&publisherID=AQ%7E%7E%2CAAABBzUwv1E%7E%2CxP-xFHVUstiMFlNYfvF4G9yFnNaqCw_9&videoID=2750934548001',
80 'info_dict': {
81 'id': '2750934548001',
82 'ext': 'mp4',
83 'title': 'This Bracelet Acts as a Personal Thermostat',
84 'description': 'md5:547b78c64f4112766ccf4e151c20b6a0',
f455a934 85 # 'uploader': 'Mashable',
41f5492f 86 'timestamp': 1382041798,
87 'upload_date': '20131017',
4d4cd35f 88 'uploader_id': '1130468786001',
fc4a0c2a
JMF
89 },
90 },
77526143
JMF
91 {
92 # test that the default referer works
93 # from http://national.ballet.ca/interact/video/Lost_in_Motion_II/
400e5810
PH
94 'url': 'http://link.brightcove.com/services/player/bcpid756015033001?bckey=AQ~~,AAAApYJi_Ck~,GxhXCegT1Dp39ilhXuxMJxasUhVNZiil&bctid=2878862109001',
95 'info_dict': {
96 'id': '2878862109001',
97 'ext': 'mp4',
98 'title': 'Lost in Motion II',
99 'description': 'md5:363109c02998fee92ec02211bd8000df',
100 'uploader': 'National Ballet of Canada',
77526143 101 },
c65aa4e9 102 'skip': 'Video gone',
5524b242
JMF
103 },
104 {
105 # test flv videos served by akamaihd.net
106 # From http://www.redbull.com/en/bike/stories/1331655643987/replay-uci-dh-world-cup-2014-from-fort-william
41f5492f 107 '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',
5524b242
JMF
108 # The md5 checksum changes on each download
109 'info_dict': {
41f5492f 110 'id': '3750436379001',
5524b242
JMF
111 'ext': 'flv',
112 'title': 'UCI MTB World Cup 2014: Fort William, UK - Downhill Finals',
41f5492f 113 'uploader': 'RBTV Old (do not use)',
5524b242 114 'description': 'UCI MTB World Cup 2014: Fort William, UK - Downhill Finals',
41f5492f 115 'timestamp': 1409122195,
116 'upload_date': '20140827',
4d4cd35f 117 'uploader_id': '710858724001',
5524b242 118 },
40158f55 119 'skip': 'Video gone',
5524b242 120 },
bd4e40df 121 {
c65aa4e9 122 # playlist with 'videoList'
bd4e40df
JMF
123 # from http://support.brightcove.com/en/video-cloud/docs/playlist-support-single-video-players
124 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=3550052898001&playerKey=AQ%7E%7E%2CAAABmA9XpXk%7E%2C-Kp7jNgisre1fG5OdqpAFUTcs0lP_ZoL',
125 'info_dict': {
126 'title': 'Sealife',
d47c26e1 127 'id': '3550319591001',
bd4e40df
JMF
128 },
129 'playlist_mincount': 7,
f455a934 130 'skip': 'Unsupported URL',
bd4e40df 131 },
c65aa4e9 132 {
067aa17e 133 # playlist with 'playlistTab' (https://github.com/ytdl-org/youtube-dl/issues/9965)
c65aa4e9
YCH
134 'url': 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=AQ%7E%7E,AAABXlLMdok%7E,NJ4EoMlZ4rZdx9eU1rkMVd8EaYPBBUlg',
135 'info_dict': {
136 'id': '1522758701001',
137 'title': 'Lesson 08',
138 },
139 'playlist_mincount': 10,
f455a934 140 'skip': 'Unsupported URL',
c65aa4e9 141 },
06d0ad9a
YCH
142 {
143 # playerID inferred from bcpid
144 # from http://www.un.org/chinese/News/story.asp?NewsID=27724
145 'url': 'https://link.brightcove.com/services/player/bcpid1722935254001/?bctid=5360463607001&autoStart=false&secureConnections=true&width=650&height=350',
146 'only_matching': True, # Tested in GenericIE
147 }
592882aa 148 ]
cfe50f04
JMF
149
150 @classmethod
a0566bbf 151 def _build_brightcove_url(cls, object_str):
cfe50f04
JMF
152 """
153 Build a Brightcove url from a xml string containing
154 <object class="BrightcoveExperience">{params}</object>
155 """
46e28a84 156
067aa17e 157 # Fix up some stupid HTML, see https://github.com/ytdl-org/youtube-dl/issues/1553
c1147c05 158 object_str = re.sub(r'(<param(?:\s+[a-zA-Z0-9_]+="[^"]*")*)>',
46e28a84 159 lambda m: m.group(1) + '/>', object_str)
067aa17e 160 # Fix up some stupid XML, see https://github.com/ytdl-org/youtube-dl/issues/1608
400e5810 161 object_str = object_str.replace('<--', '<!--')
d6f714f3
JMF
162 # remove namespace to simplify extraction
163 object_str = re.sub(r'(<object[^>]*)(xmlns=".*?")', r'\1', object_str)
7b0817e8 164 object_str = fix_xml_ampersands(object_str)
46e28a84 165
94c12557 166 try:
36e6f62c 167 object_doc = compat_etree_fromstring(object_str.encode('utf-8'))
e20d0c1e 168 except compat_xml_parse_error:
94c12557 169 return
7b0817e8
PH
170
171 fv_el = find_xpath_attr(object_doc, './param', 'name', 'flashVars')
47917f24
JMF
172 if fv_el is not None:
173 flashvars = dict(
174 (k, v[0])
175 for k, v in compat_parse_qs(fv_el.attrib['value']).items())
176 else:
177 flashvars = {}
7b0817e8 178
5964b598
YCH
179 data_url = object_doc.attrib.get('data', '')
180 data_url_params = compat_parse_qs(compat_urllib_parse_urlparse(data_url).query)
181
36de0a0e 182 def find_param(name):
7b0817e8
PH
183 if name in flashvars:
184 return flashvars[name]
d214fdb8
JMF
185 node = find_xpath_attr(object_doc, './param', 'name', name)
186 if node is not None:
187 return node.attrib['value']
5964b598 188 return data_url_params.get(name)
7b0817e8
PH
189
190 params = {}
191
16e2c8f7 192 playerID = find_param('playerID') or find_param('playerId')
7b0817e8
PH
193 if playerID is None:
194 raise ExtractorError('Cannot find player ID')
195 params['playerID'] = playerID
196
36de0a0e 197 playerKey = find_param('playerKey')
cfe50f04
JMF
198 # Not all pages define this value
199 if playerKey is not None:
d214fdb8 200 params['playerKey'] = playerKey
48254c3f 201 # These fields hold the id of the video
2cab4870 202 videoPlayer = find_param('@videoPlayer') or find_param('videoId') or find_param('videoID') or find_param('@videoList')
abb285fb 203 if videoPlayer is not None:
049a0f4d
RA
204 if isinstance(videoPlayer, list):
205 videoPlayer = videoPlayer[0]
2e76bdc8
S
206 videoPlayer = videoPlayer.strip()
207 # UUID is also possible for videoPlayer (e.g.
208 # http://www.popcornflix.com/hoodies-vs-hooligans/7f2d2b87-bbf2-4623-acfb-ea942b4f01dd
209 # or http://www8.hp.com/cn/zh/home.html)
210 if not (re.match(
211 r'^(?:\d+|[\da-fA-F]{8}-?[\da-fA-F]{4}-?[\da-fA-F]{4}-?[\da-fA-F]{4}-?[\da-fA-F]{12})$',
212 videoPlayer) or videoPlayer.startswith('ref:')):
049a0f4d 213 return None
d214fdb8 214 params['@videoPlayer'] = videoPlayer
36de0a0e 215 linkBase = find_param('linkBaseURL')
dd5bcdc4 216 if linkBase is not None:
d214fdb8 217 params['linkBaseURL'] = linkBase
af9cdee9
S
218 return cls._make_brightcove_url(params)
219
220 @classmethod
a0566bbf 221 def _build_brightcove_url_from_js(cls, object_js):
af9cdee9
S
222 # The layout of JS is as follows:
223 # customBC.createVideo = function (width, height, playerID, playerKey, videoPlayer, VideoRandomID) {
224 # // build Brightcove <object /> XML
225 # }
226 m = re.search(
4f66c16f 227 r'''(?x)customBC\.createVideo\(
af9cdee9
S
228 .*? # skipping width and height
229 ["\'](?P<playerID>\d+)["\']\s*,\s* # playerID
230 ["\'](?P<playerKey>AQ[^"\']{48})[^"\']*["\']\s*,\s* # playerKey begins with AQ and is 50 characters
231 # in length, however it's appended to itself
232 # in places, so truncate
233 ["\'](?P<videoID>\d+)["\'] # @videoPlayer
234 ''', object_js)
235 if m:
236 return cls._make_brightcove_url(m.groupdict())
237
238 @classmethod
239 def _make_brightcove_url(cls, params):
f455a934
RA
240 return update_url_query(
241 'http://c.brightcove.com/services/viewer/htmlFederated', params)
fbaaad49 242
eeb165e6
JMF
243 @classmethod
244 def _extract_brightcove_url(cls, webpage):
99877772 245 """Try to extract the brightcove url from the webpage, returns None
eeb165e6
JMF
246 if it can't be found
247 """
99877772
PH
248 urls = cls._extract_brightcove_urls(webpage)
249 return urls[0] if urls else None
250
251 @classmethod
252 def _extract_brightcove_urls(cls, webpage):
253 """Return a list of all Brightcove URLs from the webpage """
117bec93 254
e1ab5000 255 url_m = re.search(
53a664ed
S
256 r'''(?x)
257 <meta\s+
258 (?:property|itemprop)=([\'"])(?:og:video|embedURL)\1[^>]+
259 content=([\'"])(?P<url>https?://(?:secure|c)\.brightcove.com/(?:(?!\2).)+)\2
260 ''', webpage)
117bec93 261 if url_m:
53a664ed 262 url = unescapeHTML(url_m.group('url'))
381640e3
JMF
263 # Some sites don't add it, we can't download with this url, for example:
264 # http://www.ktvu.com/videos/news/raw-video-caltrain-releases-video-of-man-almost/vCTZdY/
53a664ed 265 if 'playerKey' in url or 'videoId' in url or 'idVideo' in url:
381640e3 266 return [url]
117bec93 267
99877772 268 matches = re.findall(
7b0817e8
PH
269 r'''(?sx)<object
270 (?:
99877772 271 [^>]+?class=[\'"][^>]*?BrightcoveExperience.*?[\'"] |
7b0817e8 272 [^>]*?>\s*<param\s+name="movie"\s+value="https?://[^/]*brightcove\.com/
476e1095 273 ).+?>\s*</object>''',
7b0817e8 274 webpage)
b4e1576a 275 if matches:
a0566bbf 276 return list(filter(None, [cls._build_brightcove_url(m) for m in matches]))
b4e1576a 277
06d0ad9a
YCH
278 matches = re.findall(r'(customBC\.createVideo\(.+?\);)', webpage)
279 if matches:
280 return list(filter(None, [
a0566bbf 281 cls._build_brightcove_url_from_js(custom_bc)
06d0ad9a 282 for custom_bc in matches]))
481ef51e 283 return [src for _, src in re.findall(
06d0ad9a 284 r'<iframe[^>]+src=([\'"])((?:https?:)?//link\.brightcove\.com/services/player/(?!\1).+)\1', webpage)]
eeb165e6 285
fbaaad49 286 def _real_extract(self, url):
79f82953
PH
287 url, smuggled_data = unsmuggle_url(url, {})
288
51040b72 289 # Change the 'videoId' and others field to '@videoPlayer'
53a664ed 290 url = re.sub(r'(?<=[?&])(videoI(d|D)|idVideo|bctid)', '%40videoPlayer', url)
51040b72
JMF
291 # Change bckey (used by bcove.me urls) to playerKey
292 url = re.sub(r'(?<=[?&])bckey', 'playerKey', url)
fbaaad49 293 mobj = re.match(self._VALID_URL, url)
6543f0dc
JMF
294 query_str = mobj.group('query')
295 query = compat_urlparse.parse_qs(query_str)
fbaaad49 296
6543f0dc
JMF
297 videoPlayer = query.get('@videoPlayer')
298 if videoPlayer:
79f82953 299 # We set the original url as the default 'Referer' header
f455a934
RA
300 referer = query.get('linkBaseURL', [None])[0] or smuggled_data.get('Referer', url)
301 video_id = videoPlayer[0]
06d0ad9a
YCH
302 if 'playerID' not in query:
303 mobj = re.search(r'/bcpid(\d+)', url)
304 if mobj is not None:
305 query['playerID'] = [mobj.group(1)]
160c2773
RA
306 publisher_id = query.get('publisherId')
307 if publisher_id and publisher_id[0].isdigit():
308 publisher_id = publisher_id[0]
309 if not publisher_id:
160c2773
RA
310 player_key = query.get('playerKey')
311 if player_key and ',' in player_key[0]:
312 player_key = player_key[0]
313 else:
314 player_id = query.get('playerID')
315 if player_id and player_id[0].isdigit():
f455a934
RA
316 headers = {}
317 if referer:
318 headers['Referer'] = referer
160c2773
RA
319 player_page = self._download_webpage(
320 'http://link.brightcove.com/services/player/bcpid' + player_id[0],
321 video_id, headers=headers, fatal=False)
322 if player_page:
323 player_key = self._search_regex(
324 r'<param\s+name="playerKey"\s+value="([\w~,-]+)"',
325 player_page, 'player key', fatal=False)
326 if player_key:
327 enc_pub_id = player_key.split(',')[1].replace('~', '=')
328 publisher_id = struct.unpack('>Q', base64.urlsafe_b64decode(enc_pub_id))[0]
f455a934
RA
329 if publisher_id:
330 brightcove_new_url = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s' % (publisher_id, video_id)
331 if referer:
332 brightcove_new_url = smuggle_url(brightcove_new_url, {'referrer': referer})
333 return self.url_result(brightcove_new_url, BrightcoveNewIE.ie_key(), video_id)
334 # TODO: figure out if it's possible to extract playlistId from playerKey
335 # elif 'playerKey' in query:
336 # player_key = query['playerKey']
337 # return self._get_playlist_info(player_key[0])
338 raise UnsupportedError(url)
ed126900 339
340
5996d21a 341class BrightcoveNewIE(AdobePassIE):
e721d857 342 IE_NAME = 'brightcove:new'
3031b7c4 343 _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:[^&]+)'
cb33d389 344 _TESTS = [{
ed126900 345 'url': 'http://players.brightcove.net/929656772001/e41d32dc-ec74-459e-a845-6c69f7b724ea_default/index.html?videoId=4463358922001',
c01e1a96 346 'md5': 'c8100925723840d4b0d243f7025703be',
ed126900 347 'info_dict': {
348 'id': '4463358922001',
c01e1a96 349 'ext': 'mp4',
ed126900 350 'title': 'Meet the man behind Popcorn Time',
c01e1a96 351 'description': 'md5:eac376a4fe366edc70279bfb681aea16',
cb33d389 352 'duration': 165.768,
c01e1a96 353 'timestamp': 1441391203,
354 'upload_date': '20150904',
c01e1a96 355 'uploader_id': '929656772001',
4b7dd170 356 'formats': 'mincount:20',
cb33d389
S
357 },
358 }, {
359 # with rtmp streams
360 'url': 'http://players.brightcove.net/4036320279001/5d112ed9-283f-485f-a7f9-33f42e8bc042_default/index.html?videoId=4279049078001',
361 'info_dict': {
362 'id': '4279049078001',
363 'ext': 'mp4',
364 'title': 'Titansgrave: Chapter 0',
365 'description': 'Titansgrave: Chapter 0',
366 'duration': 1242.058,
367 'timestamp': 1433556729,
368 'upload_date': '20150606',
369 'uploader_id': '4036320279001',
4b7dd170 370 'formats': 'mincount:39',
cb33d389
S
371 },
372 'params': {
08136dc1 373 # m3u8 download
cb33d389 374 'skip_download': True,
ed126900 375 }
3031b7c4
K
376 }, {
377 # playlist stream
378 'url': 'https://players.brightcove.net/1752604059001/S13cJdUBz_default/index.html?playlistId=5718313430001',
379 'info_dict': {
380 'id': '5718313430001',
381 'title': 'No Audio Playlist',
382 },
383 'playlist_count': 7,
384 'params': {
385 # m3u8 download
386 'skip_download': True,
387 }
388 }, {
389 'url': 'http://players.brightcove.net/5690807595001/HyZNerRl7_default/index.html?playlistId=5743160747001',
390 'only_matching': True,
4f29fa99
S
391 }, {
392 # ref: prefixed video id
393 'url': 'http://players.brightcove.net/3910869709001/21519b5c-4b3b-4363-accb-bdc8f358f823_default/index.html?videoId=ref:7069442',
394 'only_matching': True,
8ac73bdb 395 }, {
396 # non numeric ref: prefixed video id
397 'url': 'http://players.brightcove.net/710858724001/default_default/index.html?videoId=ref:event-stream-356',
398 'only_matching': True,
b219f5e5
S
399 }, {
400 # unavailable video without message but with error_code
401 'url': 'http://players.brightcove.net/1305187701/c832abfb-641b-44eb-9da0-2fe76786505f_default/index.html?videoId=4377407326001',
402 'only_matching': True,
cb33d389 403 }]
ed126900 404
7234d1d9 405 @staticmethod
f631b557
S
406 def _extract_url(ie, webpage):
407 urls = BrightcoveNewIE._extract_urls(ie, webpage)
7234d1d9
S
408 return urls[0] if urls else None
409
24af8529 410 @staticmethod
0254f93b 411 def _extract_urls(ie, webpage):
536f819e
S
412 # Reference:
413 # 1. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#setvideoiniframe
40158f55
JH
414 # 2. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#tag
415 # 3. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#setvideousingjavascript
416 # 4. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/in-page-embed-player-implementation.html
417 # 5. https://support.brightcove.com/en/video-cloud/docs/dynamically-assigning-videos-player
536f819e
S
418
419 entries = []
420
421 # Look for iframe embeds [1]
422 for _, url in re.findall(
354dbbd8 423 r'<iframe[^>]+src=(["\'])((?:https?:)?//players\.brightcove\.net/\d+/[^/]+/index\.html.+?)\1', webpage):
424 entries.append(url if url.startswith('http') else 'http:' + url)
d8a1caf0 425
40158f55
JH
426 # Look for <video> tags [2] and embed_in_page embeds [3]
427 # [2] looks like:
40158f55 428 for video, script_tag, account_id, player_id, embed in re.findall(
0254f93b 429 r'''(?isx)
2391941f 430 (<video(?:-js)?\s+[^>]*\bdata-video-id\s*=\s*['"]?[^>]+>)
0254f93b
S
431 (?:.*?
432 (<script[^>]+
433 src=["\'](?:https?:)?//players\.brightcove\.net/
434 (\d+)/([^/]+)_([^/]+)/index(?:\.min)?\.js
435 )
436 )?
437 ''', webpage):
40158f55
JH
438 attrs = extract_attributes(video)
439
440 # According to examples from [4] it's unclear whether video id
441 # may be optional and what to do when it is
442 video_id = attrs.get('data-video-id')
0254f93b
S
443 if not video_id:
444 continue
40158f55 445
0254f93b 446 account_id = account_id or attrs.get('data-account')
40158f55 447 if not account_id:
0254f93b
S
448 continue
449
450 player_id = player_id or attrs.get('data-player') or 'default'
451 embed = embed or attrs.get('data-embed') or 'default'
452
453 bc_url = 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s' % (
454 account_id, player_id, embed, video_id)
455
456 # Some brightcove videos may be embedded with video tag only and
457 # without script tag or any mentioning of brightcove at all. Such
458 # embeds are considered ambiguous since they are matched based only
459 # on data-video-id and data-account attributes and in the wild may
460 # not be brightcove embeds at all. Let's check reconstructed
461 # brightcove URLs in case of such embeds and only process valid
462 # ones. By this we ensure there is indeed a brightcove embed.
463 if not script_tag and not ie._is_valid_url(
464 bc_url, video_id, 'possible brightcove video'):
465 continue
466
467 entries.append(bc_url)
d8a1caf0 468
536f819e 469 return entries
ed126900 470
18d7aa6e 471 def _parse_brightcove_metadata(self, json_data, video_id, headers={}):
e47d19e9 472 title = json_data['name'].strip()
ed126900 473
29f7c58a 474 num_drm_sources = 0
ed126900 475 formats = []
29f7c58a 476 sources = json_data.get('sources') or []
477 for source in sources:
975b9c9a 478 container = source.get('container')
e910fe2f 479 ext = mimetype2ext(source.get('type'))
a6624898 480 src = source.get('src')
29f7c58a 481 # https://support.brightcove.com/playback-api-video-fields-reference#key_systems_object
482 if container == 'WVM' or source.get('key_systems'):
483 num_drm_sources += 1
87ab4fb1 484 continue
29f7c58a 485 elif ext == 'ism':
e910fe2f
RA
486 continue
487 elif ext == 'm3u8' or container == 'M2TS':
a6624898
S
488 if not src:
489 continue
7e5edcfd 490 formats.extend(self._extract_m3u8_formats(
ad55e101 491 src, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
e910fe2f 492 elif ext == 'mpd':
89807d6a 493 if not src:
494 continue
495 formats.extend(self._extract_mpd_formats(src, video_id, 'dash', fatal=False))
ed126900 496 else:
a6624898
S
497 streaming_src = source.get('streaming_src')
498 stream_name, app_name = source.get('stream_name'), source.get('app_name')
499 if not src and not streaming_src and (not stream_name or not app_name):
500 continue
501 tbr = float_or_none(source.get('avg_bitrate'), 1000)
502 height = int_or_none(source.get('height'))
89807d6a 503 width = int_or_none(source.get('width'))
a6624898
S
504 f = {
505 'tbr': tbr,
a6624898 506 'filesize': int_or_none(source.get('size')),
975b9c9a 507 'container': container,
e910fe2f 508 'ext': ext or container.lower(),
a6624898 509 }
89807d6a 510 if width == 0 and height == 0:
511 f.update({
512 'vcodec': 'none',
513 })
514 else:
515 f.update({
516 'width': width,
517 'height': height,
518 'vcodec': source.get('codec'),
519 })
ed126900 520
a6624898
S
521 def build_format_id(kind):
522 format_id = kind
523 if tbr:
524 format_id += '-%dk' % int(tbr)
525 if height:
526 format_id += '-%dp' % height
527 return format_id
528
529 if src or streaming_src:
530 f.update({
531 'url': src or streaming_src,
532 'format_id': build_format_id('http' if src else 'http-streaming'),
08136dc1 533 'source_preference': 0 if src else -1,
a6624898
S
534 })
535 else:
536 f.update({
537 'url': app_name,
538 'play_path': stream_name,
539 'format_id': build_format_id('rtmp'),
540 })
541 formats.append(f)
87ab4fb1 542
74caf528 543 if not formats:
29f7c58a 544 errors = json_data.get('errors')
545 if errors:
546 error = errors[0]
547 raise ExtractorError(
548 error.get('message') or error.get('error_subcode') or error['error_code'], expected=True)
549 if sources and num_drm_sources == len(sources):
550 raise ExtractorError('This video is DRM protected.', expected=True)
dd801bbe 551
ed126900 552 self._sort_formats(formats)
553
18d7aa6e
S
554 for f in formats:
555 f.setdefault('http_headers', {}).update(headers)
556
e47d19e9 557 subtitles = {}
558 for text_track in json_data.get('text_tracks', []):
b4eb0bc7
S
559 if text_track.get('kind') != 'captions':
560 continue
561 text_track_url = url_or_none(text_track.get('src'))
562 if not text_track_url:
563 continue
564 lang = (str_or_none(text_track.get('srclang'))
565 or str_or_none(text_track.get('label')) or 'en').lower()
566 subtitles.setdefault(lang, []).append({
567 'url': text_track_url,
568 })
a6624898 569
27e99078
RA
570 is_live = False
571 duration = float_or_none(json_data.get('duration'), 1000)
5996d21a 572 if duration is not None and duration <= 0:
27e99078
RA
573 is_live = True
574
ed126900 575 return {
576 'id': video_id,
27e99078 577 'title': self._live_title(title) if is_live else title,
f1b4af7d 578 'description': clean_html(json_data.get('description')),
e47d19e9 579 'thumbnail': json_data.get('thumbnail') or json_data.get('poster'),
27e99078 580 'duration': duration,
e47d19e9 581 'timestamp': parse_iso8601(json_data.get('published_at')),
4b7dd170 582 'uploader_id': json_data.get('account_id'),
a6624898 583 'formats': formats,
e47d19e9 584 'subtitles': subtitles,
585 'tags': json_data.get('tags', []),
27e99078 586 'is_live': is_live,
ed126900 587 }
4b7dd170
RA
588
589 def _real_extract(self, url):
590 url, smuggled_data = unsmuggle_url(url, {})
5f95927a
S
591 self._initialize_geo_bypass({
592 'countries': smuggled_data.get('geo_countries'),
593 'ip_blocks': smuggled_data.get('geo_ip_blocks'),
594 })
4b7dd170 595
3031b7c4 596 account_id, player_id, embed, content_type, video_id = re.match(self._VALID_URL, url).groups()
4b7dd170 597
75ef77c1
RA
598 policy_key_id = '%s_%s' % (account_id, player_id)
599 policy_key = self._downloader.cache.load('brightcove', policy_key_id)
06068087 600 policy_key_extracted = False
2d30b92e 601 store_pk = lambda x: self._downloader.cache.store('brightcove', policy_key_id, x)
06068087
RA
602
603 def extract_policy_key():
29f7c58a 604 base_url = 'http://players.brightcove.net/%s/%s_%s/' % (account_id, player_id, embed)
605 config = self._download_json(
606 base_url + 'config.json', video_id, fatal=False) or {}
607 policy_key = try_get(
608 config, lambda x: x['video_cloud']['policy_key'])
609 if not policy_key:
610 webpage = self._download_webpage(
611 base_url + 'index.min.js', video_id)
06068087 612
29f7c58a 613 catalog = self._search_regex(
614 r'catalog\(({.+?})\);', webpage, 'catalog', default=None)
75ef77c1 615 if catalog:
29f7c58a 616 catalog = self._parse_json(
617 js_to_json(catalog), video_id, fatal=False)
618 if catalog:
619 policy_key = catalog.get('policyKey')
620
621 if not policy_key:
622 policy_key = self._search_regex(
623 r'policyKey\s*:\s*(["\'])(?P<pk>.+?)\1',
624 webpage, 'policy key', group='pk')
06068087 625
2d30b92e 626 store_pk(policy_key)
06068087 627 return policy_key
4b7dd170 628
3031b7c4 629 api_url = 'https://edge.api.brightcove.com/playback/v1/accounts/%s/%ss/%s' % (account_id, content_type, video_id)
06068087 630 headers = {}
5399ab3f
S
631 referrer = smuggled_data.get('referrer')
632 if referrer:
633 headers.update({
634 'Referer': referrer,
635 'Origin': re.search(r'https?://[^/]+', referrer).group(0),
4b7dd170 636 })
06068087
RA
637
638 for _ in range(2):
639 if not policy_key:
640 policy_key = extract_policy_key()
641 policy_key_extracted = True
642 headers['Accept'] = 'application/json;pk=%s' % policy_key
643 try:
644 json_data = self._download_json(api_url, video_id, headers=headers)
645 break
646 except ExtractorError as e:
647 if isinstance(e.cause, compat_HTTPError) and e.cause.code in (401, 403):
648 json_data = self._parse_json(e.cause.read().decode(), video_id)[0]
649 message = json_data.get('message') or json_data['error_code']
650 if json_data.get('error_subcode') == 'CLIENT_GEO':
651 self.raise_geo_restricted(msg=message)
652 elif json_data.get('error_code') == 'INVALID_POLICY_KEY' and not policy_key_extracted:
653 policy_key = None
2d30b92e 654 store_pk(None)
06068087
RA
655 continue
656 raise ExtractorError(message, expected=True)
657 raise
4b7dd170
RA
658
659 errors = json_data.get('errors')
660 if errors and errors[0].get('error_subcode') == 'TVE_AUTH':
661 custom_fields = json_data['custom_fields']
662 tve_token = self._extract_mvpd_auth(
663 smuggled_data['source_url'], video_id,
664 custom_fields['bcadobepassrequestorid'],
665 custom_fields['bcadobepassresourceid'])
666 json_data = self._download_json(
667 api_url, video_id, headers={
668 'Accept': 'application/json;pk=%s' % policy_key
669 }, query={
670 'tveToken': tve_token,
671 })
672
3031b7c4
K
673 if content_type == 'playlist':
674 return self.playlist_result(
675 [self._parse_brightcove_metadata(vid, vid.get('id'), headers)
676 for vid in json_data.get('videos', []) if vid.get('id')],
677 json_data.get('id'), json_data.get('name'),
678 json_data.get('description'))
679
18d7aa6e
S
680 return self._parse_brightcove_metadata(
681 json_data, video_id, headers=headers)