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