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