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