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