]>
Commit | Line | Data |
---|---|---|
592882aa | 1 | # encoding: utf-8 |
400e5810 | 2 | from __future__ import unicode_literals |
592882aa | 3 | |
fbaaad49 JMF |
4 | import re |
5 | import json | |
6 | ||
7 | from .common import InfoExtractor | |
1cc79574 | 8 | from ..compat import ( |
36e6f62c | 9 | compat_etree_fromstring, |
7b0817e8 | 10 | compat_parse_qs, |
1cc79574 | 11 | compat_str, |
37aab278 | 12 | compat_urllib_parse_urlparse, |
1cc79574 | 13 | compat_urlparse, |
e20d0c1e | 14 | compat_xml_parse_error, |
d13bdc38 | 15 | compat_HTTPError, |
1cc79574 PH |
16 | ) |
17 | from ..utils import ( | |
5524b242 | 18 | determine_ext, |
592882aa | 19 | ExtractorError, |
1cc79574 PH |
20 | find_xpath_attr, |
21 | fix_xml_ampersands, | |
a6624898 | 22 | float_or_none, |
fd91257c | 23 | js_to_json, |
ed126900 | 24 | int_or_none, |
25 | parse_iso8601, | |
fd91257c S |
26 | unescapeHTML, |
27 | unsmuggle_url, | |
48254c3f | 28 | update_url_query, |
f1b4af7d | 29 | clean_html, |
cfe50f04 | 30 | ) |
fbaaad49 | 31 | |
dd5bcdc4 | 32 | |
4fcaa4f4 | 33 | class BrightcoveLegacyIE(InfoExtractor): |
e721d857 | 34 | IE_NAME = 'brightcove:legacy' |
ec05fee4 | 35 | _VALID_URL = r'(?:https?://.*brightcove\.com/(services|viewer).*?\?|brightcove:)(?P<query>.*)' |
48254c3f | 36 | _FEDERATED_URL = 'http://c.brightcove.com/services/viewer/htmlFederated' |
592882aa JMF |
37 | |
38 | _TESTS = [ | |
39 | { | |
4de1994b | 40 | # From http://www.8tv.cat/8aldia/videos/xavier-sala-i-martin-aquesta-tarda-a-8-al-dia/ |
400e5810 | 41 | 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1654948606001&flashID=myExperience&%40videoPlayer=2371591881001', |
400e5810 PH |
42 | 'md5': '5423e113865d26e40624dce2e4b45d95', |
43 | 'note': 'Test Brightcove downloads and detection in GenericIE', | |
44 | 'info_dict': { | |
96bef88f JMF |
45 | 'id': '2371591881001', |
46 | 'ext': 'mp4', | |
400e5810 PH |
47 | 'title': 'Xavier Sala i Martín: “Un banc que no presta és un banc zombi que no serveix per a res”', |
48 | 'uploader': '8TV', | |
49 | 'description': 'md5:a950cc4285c43e44d763d036710cd9cd', | |
41f5492f | 50 | 'timestamp': 1368213670, |
51 | 'upload_date': '20130510', | |
4d4cd35f | 52 | 'uploader_id': '1589608506001', |
592882aa JMF |
53 | } |
54 | }, | |
55 | { | |
4de1994b | 56 | # From http://medianetwork.oracle.com/video/player/1785452137001 |
400e5810 | 57 | 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=1217746023001&flashID=myPlayer&%40videoPlayer=1785452137001', |
400e5810 | 58 | 'info_dict': { |
96bef88f JMF |
59 | 'id': '1785452137001', |
60 | 'ext': 'flv', | |
400e5810 PH |
61 | 'title': 'JVMLS 2012: Arrays 2.0 - Opportunities and Challenges', |
62 | 'description': 'John Rose speaks at the JVM Language Summit, August 1, 2012.', | |
63 | 'uploader': 'Oracle', | |
41f5492f | 64 | 'timestamp': 1344975024, |
65 | 'upload_date': '20120814', | |
4d4cd35f | 66 | 'uploader_id': '1460825906', |
592882aa JMF |
67 | }, |
68 | }, | |
fc4a0c2a JMF |
69 | { |
70 | # From http://mashable.com/2013/10/26/thermoelectric-bracelet-lets-you-control-your-body-temperature/ | |
400e5810 PH |
71 | 'url': 'http://c.brightcove.com/services/viewer/federated_f9?&playerID=1265504713001&publisherID=AQ%7E%7E%2CAAABBzUwv1E%7E%2CxP-xFHVUstiMFlNYfvF4G9yFnNaqCw_9&videoID=2750934548001', |
72 | 'info_dict': { | |
73 | 'id': '2750934548001', | |
74 | 'ext': 'mp4', | |
75 | 'title': 'This Bracelet Acts as a Personal Thermostat', | |
76 | 'description': 'md5:547b78c64f4112766ccf4e151c20b6a0', | |
77 | 'uploader': 'Mashable', | |
41f5492f | 78 | 'timestamp': 1382041798, |
79 | 'upload_date': '20131017', | |
4d4cd35f | 80 | 'uploader_id': '1130468786001', |
fc4a0c2a JMF |
81 | }, |
82 | }, | |
77526143 JMF |
83 | { |
84 | # test that the default referer works | |
85 | # from http://national.ballet.ca/interact/video/Lost_in_Motion_II/ | |
400e5810 PH |
86 | 'url': 'http://link.brightcove.com/services/player/bcpid756015033001?bckey=AQ~~,AAAApYJi_Ck~,GxhXCegT1Dp39ilhXuxMJxasUhVNZiil&bctid=2878862109001', |
87 | 'info_dict': { | |
88 | 'id': '2878862109001', | |
89 | 'ext': 'mp4', | |
90 | 'title': 'Lost in Motion II', | |
91 | 'description': 'md5:363109c02998fee92ec02211bd8000df', | |
92 | 'uploader': 'National Ballet of Canada', | |
77526143 | 93 | }, |
c65aa4e9 | 94 | 'skip': 'Video gone', |
5524b242 JMF |
95 | }, |
96 | { | |
97 | # test flv videos served by akamaihd.net | |
98 | # From http://www.redbull.com/en/bike/stories/1331655643987/replay-uci-dh-world-cup-2014-from-fort-william | |
41f5492f | 99 | '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 |
100 | # The md5 checksum changes on each download |
101 | 'info_dict': { | |
41f5492f | 102 | 'id': '3750436379001', |
5524b242 JMF |
103 | 'ext': 'flv', |
104 | 'title': 'UCI MTB World Cup 2014: Fort William, UK - Downhill Finals', | |
41f5492f | 105 | 'uploader': 'RBTV Old (do not use)', |
5524b242 | 106 | 'description': 'UCI MTB World Cup 2014: Fort William, UK - Downhill Finals', |
41f5492f | 107 | 'timestamp': 1409122195, |
108 | 'upload_date': '20140827', | |
4d4cd35f | 109 | 'uploader_id': '710858724001', |
5524b242 JMF |
110 | }, |
111 | }, | |
bd4e40df | 112 | { |
c65aa4e9 | 113 | # playlist with 'videoList' |
bd4e40df JMF |
114 | # from http://support.brightcove.com/en/video-cloud/docs/playlist-support-single-video-players |
115 | 'url': 'http://c.brightcove.com/services/viewer/htmlFederated?playerID=3550052898001&playerKey=AQ%7E%7E%2CAAABmA9XpXk%7E%2C-Kp7jNgisre1fG5OdqpAFUTcs0lP_ZoL', | |
116 | 'info_dict': { | |
117 | 'title': 'Sealife', | |
d47c26e1 | 118 | 'id': '3550319591001', |
bd4e40df JMF |
119 | }, |
120 | 'playlist_mincount': 7, | |
121 | }, | |
c65aa4e9 YCH |
122 | { |
123 | # playlist with 'playlistTab' (https://github.com/rg3/youtube-dl/issues/9965) | |
124 | 'url': 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=AQ%7E%7E,AAABXlLMdok%7E,NJ4EoMlZ4rZdx9eU1rkMVd8EaYPBBUlg', | |
125 | 'info_dict': { | |
126 | 'id': '1522758701001', | |
127 | 'title': 'Lesson 08', | |
128 | }, | |
129 | 'playlist_mincount': 10, | |
130 | }, | |
592882aa | 131 | ] |
41f5492f | 132 | FLV_VCODECS = { |
133 | 1: 'SORENSON', | |
134 | 2: 'ON2', | |
135 | 3: 'H264', | |
136 | 4: 'VP8', | |
137 | } | |
cfe50f04 JMF |
138 | |
139 | @classmethod | |
140 | def _build_brighcove_url(cls, object_str): | |
141 | """ | |
142 | Build a Brightcove url from a xml string containing | |
143 | <object class="BrightcoveExperience">{params}</object> | |
144 | """ | |
46e28a84 PH |
145 | |
146 | # Fix up some stupid HTML, see https://github.com/rg3/youtube-dl/issues/1553 | |
c1147c05 | 147 | object_str = re.sub(r'(<param(?:\s+[a-zA-Z0-9_]+="[^"]*")*)>', |
46e28a84 | 148 | lambda m: m.group(1) + '/>', object_str) |
2d0efe70 | 149 | # Fix up some stupid XML, see https://github.com/rg3/youtube-dl/issues/1608 |
400e5810 | 150 | object_str = object_str.replace('<--', '<!--') |
d6f714f3 JMF |
151 | # remove namespace to simplify extraction |
152 | object_str = re.sub(r'(<object[^>]*)(xmlns=".*?")', r'\1', object_str) | |
7b0817e8 | 153 | object_str = fix_xml_ampersands(object_str) |
46e28a84 | 154 | |
94c12557 | 155 | try: |
36e6f62c | 156 | object_doc = compat_etree_fromstring(object_str.encode('utf-8')) |
e20d0c1e | 157 | except compat_xml_parse_error: |
94c12557 | 158 | return |
7b0817e8 PH |
159 | |
160 | fv_el = find_xpath_attr(object_doc, './param', 'name', 'flashVars') | |
47917f24 JMF |
161 | if fv_el is not None: |
162 | flashvars = dict( | |
163 | (k, v[0]) | |
164 | for k, v in compat_parse_qs(fv_el.attrib['value']).items()) | |
165 | else: | |
166 | flashvars = {} | |
7b0817e8 | 167 | |
5964b598 YCH |
168 | data_url = object_doc.attrib.get('data', '') |
169 | data_url_params = compat_parse_qs(compat_urllib_parse_urlparse(data_url).query) | |
170 | ||
36de0a0e | 171 | def find_param(name): |
7b0817e8 PH |
172 | if name in flashvars: |
173 | return flashvars[name] | |
d214fdb8 JMF |
174 | node = find_xpath_attr(object_doc, './param', 'name', name) |
175 | if node is not None: | |
176 | return node.attrib['value'] | |
5964b598 | 177 | return data_url_params.get(name) |
7b0817e8 PH |
178 | |
179 | params = {} | |
180 | ||
181 | playerID = find_param('playerID') | |
182 | if playerID is None: | |
183 | raise ExtractorError('Cannot find player ID') | |
184 | params['playerID'] = playerID | |
185 | ||
36de0a0e | 186 | playerKey = find_param('playerKey') |
cfe50f04 JMF |
187 | # Not all pages define this value |
188 | if playerKey is not None: | |
d214fdb8 | 189 | params['playerKey'] = playerKey |
48254c3f | 190 | # These fields hold the id of the video |
2cab4870 | 191 | videoPlayer = find_param('@videoPlayer') or find_param('videoId') or find_param('videoID') or find_param('@videoList') |
abb285fb | 192 | if videoPlayer is not None: |
d214fdb8 | 193 | params['@videoPlayer'] = videoPlayer |
36de0a0e | 194 | linkBase = find_param('linkBaseURL') |
dd5bcdc4 | 195 | if linkBase is not None: |
d214fdb8 | 196 | params['linkBaseURL'] = linkBase |
af9cdee9 S |
197 | return cls._make_brightcove_url(params) |
198 | ||
199 | @classmethod | |
200 | def _build_brighcove_url_from_js(cls, object_js): | |
201 | # The layout of JS is as follows: | |
202 | # customBC.createVideo = function (width, height, playerID, playerKey, videoPlayer, VideoRandomID) { | |
203 | # // build Brightcove <object /> XML | |
204 | # } | |
205 | m = re.search( | |
206 | r'''(?x)customBC.\createVideo\( | |
207 | .*? # skipping width and height | |
208 | ["\'](?P<playerID>\d+)["\']\s*,\s* # playerID | |
209 | ["\'](?P<playerKey>AQ[^"\']{48})[^"\']*["\']\s*,\s* # playerKey begins with AQ and is 50 characters | |
210 | # in length, however it's appended to itself | |
211 | # in places, so truncate | |
212 | ["\'](?P<videoID>\d+)["\'] # @videoPlayer | |
213 | ''', object_js) | |
214 | if m: | |
215 | return cls._make_brightcove_url(m.groupdict()) | |
216 | ||
217 | @classmethod | |
218 | def _make_brightcove_url(cls, params): | |
48254c3f | 219 | return update_url_query(cls._FEDERATED_URL, params) |
fbaaad49 | 220 | |
eeb165e6 JMF |
221 | @classmethod |
222 | def _extract_brightcove_url(cls, webpage): | |
99877772 | 223 | """Try to extract the brightcove url from the webpage, returns None |
eeb165e6 JMF |
224 | if it can't be found |
225 | """ | |
99877772 PH |
226 | urls = cls._extract_brightcove_urls(webpage) |
227 | return urls[0] if urls else None | |
228 | ||
229 | @classmethod | |
230 | def _extract_brightcove_urls(cls, webpage): | |
231 | """Return a list of all Brightcove URLs from the webpage """ | |
117bec93 | 232 | |
e1ab5000 | 233 | url_m = re.search( |
b26733ba | 234 | r'<meta\s+property=[\'"]og:video[\'"]\s+content=[\'"](https?://(?:secure|c)\.brightcove.com/[^\'"]+)[\'"]', |
e1ab5000 | 235 | webpage) |
117bec93 | 236 | if url_m: |
381640e3 JMF |
237 | url = unescapeHTML(url_m.group(1)) |
238 | # Some sites don't add it, we can't download with this url, for example: | |
239 | # http://www.ktvu.com/videos/news/raw-video-caltrain-releases-video-of-man-almost/vCTZdY/ | |
e1ab5000 | 240 | if 'playerKey' in url or 'videoId' in url: |
381640e3 | 241 | return [url] |
117bec93 | 242 | |
99877772 | 243 | matches = re.findall( |
7b0817e8 PH |
244 | r'''(?sx)<object |
245 | (?: | |
99877772 | 246 | [^>]+?class=[\'"][^>]*?BrightcoveExperience.*?[\'"] | |
7b0817e8 | 247 | [^>]*?>\s*<param\s+name="movie"\s+value="https?://[^/]*brightcove\.com/ |
476e1095 | 248 | ).+?>\s*</object>''', |
7b0817e8 | 249 | webpage) |
b4e1576a | 250 | if matches: |
251 | return list(filter(None, [cls._build_brighcove_url(m) for m in matches])) | |
252 | ||
af9cdee9 S |
253 | return list(filter(None, [ |
254 | cls._build_brighcove_url_from_js(custom_bc) | |
255 | for custom_bc in re.findall(r'(customBC\.createVideo\(.+?\);)', webpage)])) | |
eeb165e6 | 256 | |
fbaaad49 | 257 | def _real_extract(self, url): |
79f82953 PH |
258 | url, smuggled_data = unsmuggle_url(url, {}) |
259 | ||
51040b72 JMF |
260 | # Change the 'videoId' and others field to '@videoPlayer' |
261 | url = re.sub(r'(?<=[?&])(videoI(d|D)|bctid)', '%40videoPlayer', url) | |
262 | # Change bckey (used by bcove.me urls) to playerKey | |
263 | url = re.sub(r'(?<=[?&])bckey', 'playerKey', url) | |
fbaaad49 | 264 | mobj = re.match(self._VALID_URL, url) |
6543f0dc JMF |
265 | query_str = mobj.group('query') |
266 | query = compat_urlparse.parse_qs(query_str) | |
fbaaad49 | 267 | |
6543f0dc JMF |
268 | videoPlayer = query.get('@videoPlayer') |
269 | if videoPlayer: | |
79f82953 PH |
270 | # We set the original url as the default 'Referer' header |
271 | referer = smuggled_data.get('Referer', url) | |
272 | return self._get_video_info( | |
48254c3f | 273 | videoPlayer[0], query, referer=referer) |
68575900 | 274 | elif 'playerKey' in query: |
6543f0dc JMF |
275 | player_key = query['playerKey'] |
276 | return self._get_playlist_info(player_key[0]) | |
68575900 PH |
277 | else: |
278 | raise ExtractorError( | |
279 | 'Cannot find playerKey= variable. Did you forget quotes in a shell invocation?', | |
280 | expected=True) | |
abb285fb | 281 | |
48254c3f | 282 | def _get_video_info(self, video_id, query, referer=None): |
283 | headers = {} | |
dd5bcdc4 JMF |
284 | linkBase = query.get('linkBaseURL') |
285 | if linkBase is not None: | |
77526143 JMF |
286 | referer = linkBase[0] |
287 | if referer is not None: | |
48254c3f | 288 | headers['Referer'] = referer |
289 | webpage = self._download_webpage(self._FEDERATED_URL, video_id, headers=headers, query=query) | |
fbaaad49 | 290 | |
a5ed3e57 | 291 | error_msg = self._html_search_regex( |
4d46c1c6 | 292 | r"<h1>We're sorry.</h1>([\s\n]*<p>.*?</p>)+", webpage, |
a5ed3e57 PH |
293 | 'error message', default=None) |
294 | if error_msg is not None: | |
295 | raise ExtractorError( | |
296 | 'brightcove said: %s' % error_msg, expected=True) | |
297 | ||
fbaaad49 | 298 | self.report_extraction(video_id) |
a013eba6 | 299 | info = self._search_regex(r'var experienceJSON = ({.*});', webpage, 'json') |
fbaaad49 JMF |
300 | info = json.loads(info)['data'] |
301 | video_info = info['programmedContent']['videoPlayer']['mediaDTO'] | |
7b0817e8 | 302 | video_info['_youtubedl_adServerURL'] = info.get('adServerURL') |
abb285fb JMF |
303 | |
304 | return self._extract_video_info(video_info) | |
305 | ||
306 | def _get_playlist_info(self, player_key): | |
117bec93 PH |
307 | info_url = 'http://c.brightcove.com/services/json/experience/runtime/?command=get_programming_for_experience&playerKey=%s' % player_key |
308 | playlist_info = self._download_webpage( | |
309 | info_url, player_key, 'Downloading playlist information') | |
abb285fb | 310 | |
59145479 | 311 | json_data = json.loads(playlist_info) |
c65aa4e9 YCH |
312 | if 'videoList' in json_data: |
313 | playlist_info = json_data['videoList'] | |
314 | playlist_dto = playlist_info['mediaCollectionDTO'] | |
315 | elif 'playlistTabs' in json_data: | |
316 | playlist_info = json_data['playlistTabs'] | |
317 | playlist_dto = playlist_info['lineupListDTO']['playlistDTOs'][0] | |
318 | else: | |
400e5810 | 319 | raise ExtractorError('Empty playlist') |
c65aa4e9 YCH |
320 | |
321 | videos = [self._extract_video_info(video_info) for video_info in playlist_dto['videoDTOs']] | |
abb285fb | 322 | |
d47c26e1 | 323 | return self.playlist_result(videos, playlist_id='%s' % playlist_info['id'], |
c65aa4e9 | 324 | playlist_title=playlist_dto['displayName']) |
abb285fb JMF |
325 | |
326 | def _extract_video_info(self, video_info): | |
ad55e101 | 327 | video_id = compat_str(video_info['id']) |
23576edb | 328 | publisher_id = video_info.get('publisherId') |
592882aa | 329 | info = { |
ad55e101 | 330 | 'id': video_id, |
066f6a06 | 331 | 'title': video_info['displayName'].strip(), |
592882aa JMF |
332 | 'description': video_info.get('shortDescription'), |
333 | 'thumbnail': video_info.get('videoStillURL') or video_info.get('thumbnailURL'), | |
334 | 'uploader': video_info.get('publisherName'), | |
23576edb | 335 | 'uploader_id': compat_str(publisher_id) if publisher_id else None, |
41f5492f | 336 | 'duration': float_or_none(video_info.get('length'), 1000), |
337 | 'timestamp': int_or_none(video_info.get('creationDate'), 1000), | |
592882aa | 338 | } |
abb285fb | 339 | |
d6c340ca | 340 | renditions = video_info.get('renditions', []) + video_info.get('IOSRenditions', []) |
592882aa | 341 | if renditions: |
5524b242 JMF |
342 | formats = [] |
343 | for rend in renditions: | |
344 | url = rend['defaultURL'] | |
b5af6fcd NJ |
345 | if not url: |
346 | continue | |
233d37fb | 347 | ext = None |
5524b242 | 348 | if rend['remote']: |
37aab278 NJ |
349 | url_comp = compat_urllib_parse_urlparse(url) |
350 | if url_comp.path.endswith('.m3u8'): | |
351 | formats.extend( | |
ad55e101 | 352 | self._extract_m3u8_formats( |
353 | url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)) | |
37aab278 NJ |
354 | continue |
355 | elif 'akamaihd.net' in url_comp.netloc: | |
356 | # This type of renditions are served through | |
357 | # akamaihd.net, but they don't use f4m manifests | |
358 | url = url.replace('control/', '') + '?&v=3.3.0&fp=13&r=FEEFJ&g=RTSJIMBMPFPB' | |
359 | ext = 'flv' | |
233d37fb | 360 | if ext is None: |
5524b242 | 361 | ext = determine_ext(url) |
8334637f | 362 | tbr = int_or_none(rend.get('encodingRate'), 1000) |
d6c340ca | 363 | a_format = { |
41f5492f | 364 | 'format_id': 'http%s' % ('-%s' % tbr if tbr else ''), |
5524b242 JMF |
365 | 'url': url, |
366 | 'ext': ext, | |
41f5492f | 367 | 'filesize': int_or_none(rend.get('size')) or None, |
368 | 'tbr': tbr, | |
d6c340ca | 369 | } |
41f5492f | 370 | if rend.get('audioOnly'): |
371 | a_format.update({ | |
372 | 'vcodec': 'none', | |
373 | }) | |
374 | else: | |
375 | a_format.update({ | |
376 | 'height': int_or_none(rend.get('frameHeight')), | |
377 | 'width': int_or_none(rend.get('frameWidth')), | |
378 | 'vcodec': rend.get('videoCodec'), | |
379 | }) | |
d6c340ca YCH |
380 | |
381 | # m3u8 manifests with remote == false are media playlists | |
382 | # Not calling _extract_m3u8_formats here to save network traffic | |
383 | if ext == 'm3u8': | |
384 | a_format.update({ | |
41f5492f | 385 | 'format_id': 'hls%s' % ('-%s' % tbr if tbr else ''), |
d6c340ca | 386 | 'ext': 'mp4', |
ad55e101 | 387 | 'protocol': 'm3u8_native', |
d6c340ca YCH |
388 | }) |
389 | ||
390 | formats.append(a_format) | |
35eacd0d | 391 | self._sort_formats(formats) |
5524b242 | 392 | info['formats'] = formats |
592882aa JMF |
393 | elif video_info.get('FLVFullLengthURL') is not None: |
394 | info.update({ | |
395 | 'url': video_info['FLVFullLengthURL'], | |
41f5492f | 396 | 'vcodec': self.FLV_VCODECS.get(video_info.get('FLVFullCodec')), |
397 | 'filesize': int_or_none(video_info.get('FLVFullSize')), | |
592882aa | 398 | }) |
7b0817e8 PH |
399 | |
400 | if self._downloader.params.get('include_ads', False): | |
401 | adServerURL = video_info.get('_youtubedl_adServerURL') | |
402 | if adServerURL: | |
403 | ad_info = { | |
404 | '_type': 'url', | |
405 | 'url': adServerURL, | |
406 | } | |
407 | if 'url' in info: | |
408 | return { | |
409 | '_type': 'playlist', | |
410 | 'title': info['title'], | |
411 | 'entries': [ad_info, info], | |
412 | } | |
413 | else: | |
414 | return ad_info | |
415 | ||
d614aa40 | 416 | if 'url' not in info and not info.get('formats'): |
ad55e101 | 417 | raise ExtractorError('Unable to extract video url for %s' % video_id) |
592882aa | 418 | return info |
ed126900 | 419 | |
420 | ||
5c17f0a6 | 421 | class BrightcoveNewIE(InfoExtractor): |
e721d857 | 422 | IE_NAME = 'brightcove:new' |
8ac73bdb | 423 | _VALID_URL = r'https?://players\.brightcove\.net/(?P<account_id>\d+)/(?P<player_id>[^/]+)_(?P<embed>[^/]+)/index\.html\?.*videoId=(?P<video_id>\d+|ref:[^&]+)' |
cb33d389 | 424 | _TESTS = [{ |
ed126900 | 425 | 'url': 'http://players.brightcove.net/929656772001/e41d32dc-ec74-459e-a845-6c69f7b724ea_default/index.html?videoId=4463358922001', |
c01e1a96 | 426 | 'md5': 'c8100925723840d4b0d243f7025703be', |
ed126900 | 427 | 'info_dict': { |
428 | 'id': '4463358922001', | |
c01e1a96 | 429 | 'ext': 'mp4', |
ed126900 | 430 | 'title': 'Meet the man behind Popcorn Time', |
c01e1a96 | 431 | 'description': 'md5:eac376a4fe366edc70279bfb681aea16', |
cb33d389 | 432 | 'duration': 165.768, |
c01e1a96 | 433 | 'timestamp': 1441391203, |
434 | 'upload_date': '20150904', | |
c01e1a96 | 435 | 'uploader_id': '929656772001', |
cb33d389 S |
436 | 'formats': 'mincount:22', |
437 | }, | |
438 | }, { | |
439 | # with rtmp streams | |
440 | 'url': 'http://players.brightcove.net/4036320279001/5d112ed9-283f-485f-a7f9-33f42e8bc042_default/index.html?videoId=4279049078001', | |
441 | 'info_dict': { | |
442 | 'id': '4279049078001', | |
443 | 'ext': 'mp4', | |
444 | 'title': 'Titansgrave: Chapter 0', | |
445 | 'description': 'Titansgrave: Chapter 0', | |
446 | 'duration': 1242.058, | |
447 | 'timestamp': 1433556729, | |
448 | 'upload_date': '20150606', | |
449 | 'uploader_id': '4036320279001', | |
450 | 'formats': 'mincount:41', | |
451 | }, | |
452 | 'params': { | |
08136dc1 | 453 | # m3u8 download |
cb33d389 | 454 | 'skip_download': True, |
ed126900 | 455 | } |
4f29fa99 S |
456 | }, { |
457 | # ref: prefixed video id | |
458 | 'url': 'http://players.brightcove.net/3910869709001/21519b5c-4b3b-4363-accb-bdc8f358f823_default/index.html?videoId=ref:7069442', | |
459 | 'only_matching': True, | |
8ac73bdb | 460 | }, { |
461 | # non numeric ref: prefixed video id | |
462 | 'url': 'http://players.brightcove.net/710858724001/default_default/index.html?videoId=ref:event-stream-356', | |
463 | 'only_matching': True, | |
b219f5e5 S |
464 | }, { |
465 | # unavailable video without message but with error_code | |
466 | 'url': 'http://players.brightcove.net/1305187701/c832abfb-641b-44eb-9da0-2fe76786505f_default/index.html?videoId=4377407326001', | |
467 | 'only_matching': True, | |
cb33d389 | 468 | }] |
ed126900 | 469 | |
7234d1d9 S |
470 | @staticmethod |
471 | def _extract_url(webpage): | |
472 | urls = BrightcoveNewIE._extract_urls(webpage) | |
473 | return urls[0] if urls else None | |
474 | ||
24af8529 S |
475 | @staticmethod |
476 | def _extract_urls(webpage): | |
536f819e S |
477 | # Reference: |
478 | # 1. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#setvideoiniframe | |
f81ccbb3 | 479 | # 2. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/publish-video.html#setvideousingjavascript |
536f819e | 480 | # 3. http://docs.brightcove.com/en/video-cloud/brightcove-player/guides/embed-in-page.html |
5b72fda1 | 481 | # 4. https://support.brightcove.com/en/video-cloud/docs/dynamically-assigning-videos-player |
536f819e S |
482 | |
483 | entries = [] | |
484 | ||
485 | # Look for iframe embeds [1] | |
486 | for _, url in re.findall( | |
354dbbd8 | 487 | r'<iframe[^>]+src=(["\'])((?:https?:)?//players\.brightcove\.net/\d+/[^/]+/index\.html.+?)\1', webpage): |
488 | entries.append(url if url.startswith('http') else 'http:' + url) | |
d8a1caf0 | 489 | |
536f819e | 490 | # Look for embed_in_page embeds [2] |
536f819e | 491 | for video_id, account_id, player_id, embed in re.findall( |
d8a1caf0 S |
492 | # According to examples from [3] it's unclear whether video id |
493 | # may be optional and what to do when it is | |
5b72fda1 | 494 | # According to [4] data-video-id may be prefixed with ref: |
536f819e S |
495 | r'''(?sx) |
496 | <video[^>]+ | |
64d4f31d | 497 | data-video-id=["\'](\d+|ref:[^"\']+)["\'][^>]*>.*? |
536f819e S |
498 | </video>.*? |
499 | <script[^>]+ | |
500 | src=["\'](?:https?:)?//players\.brightcove\.net/ | |
fea7295b | 501 | (\d+)/([^/]+)_([^/]+)/index(?:\.min)?\.js |
536f819e | 502 | ''', webpage): |
24af8529 | 503 | entries.append( |
536f819e | 504 | 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s' |
24af8529 | 505 | % (account_id, player_id, embed, video_id)) |
d8a1caf0 | 506 | |
536f819e | 507 | return entries |
ed126900 | 508 | |
509 | def _real_extract(self, url): | |
9550ca50 | 510 | account_id, player_id, embed, video_id = re.match(self._VALID_URL, url).groups() |
ed126900 | 511 | |
a6624898 S |
512 | webpage = self._download_webpage( |
513 | 'http://players.brightcove.net/%s/%s_%s/index.min.js' | |
514 | % (account_id, player_id, embed), video_id) | |
ed126900 | 515 | |
a6624898 S |
516 | policy_key = None |
517 | ||
518 | catalog = self._search_regex( | |
519 | r'catalog\(({.+?})\);', webpage, 'catalog', default=None) | |
520 | if catalog: | |
521 | catalog = self._parse_json( | |
522 | js_to_json(catalog), video_id, fatal=False) | |
523 | if catalog: | |
524 | policy_key = catalog.get('policyKey') | |
525 | ||
526 | if not policy_key: | |
527 | policy_key = self._search_regex( | |
528 | r'policyKey\s*:\s*(["\'])(?P<pk>.+?)\1', | |
529 | webpage, 'policy key', group='pk') | |
ed126900 | 530 | |
48254c3f | 531 | api_url = 'https://edge.api.brightcove.com/playback/v1/accounts/%s/videos/%s' % (account_id, video_id) |
d13bdc38 | 532 | try: |
48254c3f | 533 | json_data = self._download_json(api_url, video_id, headers={ |
534 | 'Accept': 'application/json;pk=%s' % policy_key | |
535 | }) | |
d13bdc38 | 536 | except ExtractorError as e: |
537 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: | |
b219f5e5 S |
538 | json_data = self._parse_json(e.cause.read().decode(), video_id)[0] |
539 | raise ExtractorError( | |
540 | json_data.get('message') or json_data['error_code'], expected=True) | |
d13bdc38 | 541 | raise |
ed126900 | 542 | |
e47d19e9 | 543 | title = json_data['name'].strip() |
ed126900 | 544 | |
545 | formats = [] | |
a6624898 | 546 | for source in json_data.get('sources', []): |
975b9c9a | 547 | container = source.get('container') |
ed126900 | 548 | source_type = source.get('type') |
a6624898 | 549 | src = source.get('src') |
975b9c9a | 550 | if source_type == 'application/x-mpegURL' or container == 'M2TS': |
a6624898 S |
551 | if not src: |
552 | continue | |
7e5edcfd | 553 | formats.extend(self._extract_m3u8_formats( |
ad55e101 | 554 | src, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)) |
89807d6a | 555 | elif source_type == 'application/dash+xml': |
556 | if not src: | |
557 | continue | |
558 | formats.extend(self._extract_mpd_formats(src, video_id, 'dash', fatal=False)) | |
ed126900 | 559 | else: |
a6624898 S |
560 | streaming_src = source.get('streaming_src') |
561 | stream_name, app_name = source.get('stream_name'), source.get('app_name') | |
562 | if not src and not streaming_src and (not stream_name or not app_name): | |
563 | continue | |
564 | tbr = float_or_none(source.get('avg_bitrate'), 1000) | |
565 | height = int_or_none(source.get('height')) | |
89807d6a | 566 | width = int_or_none(source.get('width')) |
a6624898 S |
567 | f = { |
568 | 'tbr': tbr, | |
a6624898 | 569 | 'filesize': int_or_none(source.get('size')), |
975b9c9a | 570 | 'container': container, |
89807d6a | 571 | 'ext': container.lower(), |
a6624898 | 572 | } |
89807d6a | 573 | if width == 0 and height == 0: |
574 | f.update({ | |
575 | 'vcodec': 'none', | |
576 | }) | |
577 | else: | |
578 | f.update({ | |
579 | 'width': width, | |
580 | 'height': height, | |
581 | 'vcodec': source.get('codec'), | |
582 | }) | |
ed126900 | 583 | |
a6624898 S |
584 | def build_format_id(kind): |
585 | format_id = kind | |
586 | if tbr: | |
587 | format_id += '-%dk' % int(tbr) | |
588 | if height: | |
589 | format_id += '-%dp' % height | |
590 | return format_id | |
591 | ||
592 | if src or streaming_src: | |
593 | f.update({ | |
594 | 'url': src or streaming_src, | |
595 | 'format_id': build_format_id('http' if src else 'http-streaming'), | |
08136dc1 | 596 | 'source_preference': 0 if src else -1, |
a6624898 S |
597 | }) |
598 | else: | |
599 | f.update({ | |
600 | 'url': app_name, | |
601 | 'play_path': stream_name, | |
602 | 'format_id': build_format_id('rtmp'), | |
603 | }) | |
604 | formats.append(f) | |
dd801bbe RA |
605 | |
606 | errors = json_data.get('errors') | |
607 | if not formats and errors: | |
608 | error = errors[0] | |
609 | raise ExtractorError( | |
610 | error.get('message') or error.get('error_subcode') or error['error_code'], expected=True) | |
611 | ||
ed126900 | 612 | self._sort_formats(formats) |
613 | ||
e47d19e9 | 614 | subtitles = {} |
615 | for text_track in json_data.get('text_tracks', []): | |
616 | if text_track.get('src'): | |
617 | subtitles.setdefault(text_track.get('srclang'), []).append({ | |
618 | 'url': text_track['src'], | |
619 | }) | |
a6624898 | 620 | |
ed126900 | 621 | return { |
622 | 'id': video_id, | |
623 | 'title': title, | |
f1b4af7d | 624 | 'description': clean_html(json_data.get('description')), |
e47d19e9 | 625 | 'thumbnail': json_data.get('thumbnail') or json_data.get('poster'), |
626 | 'duration': float_or_none(json_data.get('duration'), 1000), | |
627 | 'timestamp': parse_iso8601(json_data.get('published_at')), | |
c01e1a96 | 628 | 'uploader_id': account_id, |
a6624898 | 629 | 'formats': formats, |
e47d19e9 | 630 | 'subtitles': subtitles, |
631 | 'tags': json_data.get('tags', []), | |
ed126900 | 632 | } |