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