def getDirectLinks(self, url, formats = 'flv, mp4', dash=True, dashSepareteList = False):
     printDBG('YouTubeParser.getDirectLinks')
     if config.plugins.iptvplayer.ytUseDF.value: # temporary workaround, similar code should be executed after getDirectLinks
         dash = False
     list = []
     try:
         list = YoutubeIE()._real_extract(url)
     except:
         printExc()
         if dashSepareteList:
             return [], []
         else: return []
     
     retList = []
     # filter dash
     dashAudioLists = []
     dashVideoLists = []
     if dash:
         # separete audio and video links
         for item in list:
             if 'mp4a' == item['ext']:
                 dashAudioLists.append(item)
             elif 'mp4v' == item['ext']:
                 dashVideoLists.append(item)
         # sort by quality -> format
         def _key(x):
             if x['format'].startswith('>'):
                  int(x['format'][1:-1])
             else:
                  int(x['format'][:-1])
                 
         dashAudioLists = sorted(dashAudioLists, key=_key, reverse=True)
         dashVideoLists = sorted(dashVideoLists, key=_key, reverse=True)
         
     for item in list:
         printDBG(">>>>>>>>>>>>>>>>>>>>>")
         printDBG( item )
         printDBG("<<<<<<<<<<<<<<<<<<<<<")
         if -1 < formats.find( item['ext'] ):
             if 'yes' == item['m3u8']:
                 format = re.search('([0-9]+?)p$', item['format'])
                 if format != None:
                     item['format'] = format.group(1) + "x"
                     item['ext']  = item['ext'] + "_M3U8"
                     item['url']  = decorateUrl(item['url'], {"iptv_proto":"m3u8"})
                     retList.append(item)
             else:
                 format = re.search('([0-9]+?x[0-9]+?$)', item['format'])
                 if format != None:
                     item['format'] = format.group(1)
                     item['url']  = decorateUrl(item['url'])
                     retList.append(item)
     
     dashList = []
     if len(dashAudioLists):
         # use best audio
         for item in dashVideoLists:
             item = dict(item)
             item["url"] = decorateUrl("merge://audio_url|video_url", {'audio_url':dashAudioLists[0]['url'], 'video_url':item['url'], 'prefered_merger':'MP4box'})
             dashList.append(item)
             
     if dashSepareteList:
         return retList, dashList
     else:
         retList.extend(dashList)
         return retList
Esempio n. 2
0
    def getDirectLinks(self,
                       url,
                       formats='flv, mp4',
                       dash=True,
                       dashSepareteList=False,
                       allowVP9=None,
                       allowAgeGate=None):
        printDBG("YouTubeParser.getDirectLinks")
        list = []
        try:
            if self.cm.isValidUrl(url) and '/channel/' in url and url.endswith(
                    '/live'):
                sts, data = self.cm.getPage(url)
                if sts:
                    videoId = self.cm.ph.getSearchGroups(
                        data,
                        '''<meta[^>]+?itemprop=['"]videoId['"][^>]+?content=['"]([^'^"]+?)['"]'''
                    )[0]
                    if videoId == '':
                        videoId = self.cm.ph.getSearchGroups(
                            data,
                            '''['"]REDIRECT_TO_VIDEO['"]\s*\,\s*['"]([^'^"]+?)['"]'''
                        )[0]
                    if videoId != '':
                        url = 'https://www.youtube.com/watch?v=' + videoId
            list = YoutubeIE()._real_extract(url,
                                             allowVP9=allowVP9,
                                             allowAgeGate=allowAgeGate)
        except Exception:
            printExc()
            if dashSepareteList:
                return [], []
            else:
                return []

        reNum = re.compile('([0-9]+)')
        retHLSList = []
        retList = []
        dashList = []
        # filter dash
        dashAudioLists = []
        dashVideoLists = []
        if dash:
            # separete audio and video links
            for item in list:
                if 'mp4a' == item['ext']:
                    dashAudioLists.append(item)
                elif item['ext'] in ('mp4v', 'webmv'):
                    dashVideoLists.append(item)
                elif 'mpd' == item['ext']:
                    tmpList = getMPDLinksWithMeta(item['url'], checkExt=False)
                    printDBG(tmpList)
                    for idx in range(len(tmpList)):
                        tmpList[idx]['format'] = "%sx%s" % (tmpList[idx].get(
                            'height', 0), tmpList[idx].get('width', 0))
                        tmpList[idx]['ext'] = "mpd"
                        tmpList[idx]['dash'] = True
                    dashList.extend(tmpList)
            # sort by quality -> format
            def _key(x):
                if x['format'].startswith('>'):
                    int(x['format'][1:-1])
                else:
                    int(ph.search(x['format'], reNum)[0])

            dashAudioLists = sorted(dashAudioLists, key=_key, reverse=True)
            dashVideoLists = sorted(dashVideoLists, key=_key, reverse=True)

        for item in list:
            printDBG(">>>>>>>>>>>>>>>>>>>>>")
            printDBG(item)
            printDBG("<<<<<<<<<<<<<<<<<<<<<")
            if -1 < formats.find(item['ext']):
                if 'yes' == item['m3u8']:
                    format = re.search('([0-9]+?)p$', item['format'])
                    if format != None:
                        item['format'] = format.group(1) + "x"
                        item['ext'] = item['ext'] + "_M3U8"
                        item['url'] = decorateUrl(item['url'],
                                                  {"iptv_proto": "m3u8"})
                        retHLSList.append(item)
                else:
                    format = re.search('([0-9]+?x[0-9]+?$)', item['format'])
                    if format != None:
                        item['format'] = format.group(1)
                        item['url'] = decorateUrl(item['url'])
                        retList.append(item)

        if len(dashAudioLists):
            # use best audio
            for item in dashVideoLists:
                item = dict(item)
                item["url"] = decorateUrl(
                    "merge://audio_url|video_url", {
                        'audio_url': dashAudioLists[0]['url'],
                        'video_url': item['url']
                    })
                dashList.append(item)

        # try to get hls format with alternative method
        if 0 == len(retList):
            try:
                video_id = YoutubeIE()._extract_id(url)
                url = 'http://www.youtube.com/watch?v=%s&gl=US&hl=en&has_verified=1' % video_id
                sts, data = self.cm.getPage(
                    url, {
                        'header': {
                            'User-agent':
                            'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10'
                        }
                    })
                if sts:
                    data = data.replace('\\"', '"').replace('\\\\\\/', '/')
                    hlsUrl = self.cm.ph.getSearchGroups(
                        data, '''"hlsvp"\s*:\s*"(https?://[^"]+?)"''')[0]
                    hlsUrl = json_loads('"%s"' % hlsUrl)
                    if self.cm.isValidUrl(hlsUrl):
                        hlsList = getDirectM3U8Playlist(hlsUrl)
                        if len(hlsList):
                            dashList = []
                            for item in hlsList:
                                item['format'] = "%sx%s" % (item.get(
                                    'with', 0), item.get('heigth', 0))
                                item['ext'] = "m3u8"
                                item['m3u8'] = True
                                retList.append(item)
            except Exception:
                printExc()
            if 0 == len(retList):
                retList = retHLSList

            if dash:
                try:
                    sts, data = self.cm.getPage(
                        url, {
                            'header': {
                                'User-agent':
                                'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
                            }
                        })
                    data = data.replace('\\"',
                                        '"').replace('\\\\\\/',
                                                     '/').replace('\\/', '/')
                    dashUrl = self.cm.ph.getSearchGroups(
                        data, '''"dashmpd"\s*:\s*"(https?://[^"]+?)"''')[0]
                    dashUrl = json_loads('"%s"' % dashUrl)
                    if '?' not in dashUrl: dashUrl += '?mpd_version=5'
                    else: dashUrl += '&mpd_version=5'
                    printDBG("DASH URL >> [%s]" % dashUrl)
                    if self.cm.isValidUrl(dashUrl):
                        dashList = getMPDLinksWithMeta(dashUrl, checkExt=False)
                        printDBG(dashList)
                        for idx in range(len(dashList)):
                            dashList[idx]['format'] = "%sx%s" % (
                                dashList[idx].get('height', 0),
                                dashList[idx].get('width', 0))
                            dashList[idx]['ext'] = "mpd"
                            dashList[idx]['dash'] = True
                except Exception:
                    printExc()

        for idx in range(len(retList)):
            if retList[idx].get('m3u8', False):
                retList[idx]['url'] = strwithmeta(
                    retList[idx]['url'], {'iptv_m3u8_live_start_index': -30})

        if dashSepareteList:
            return retList, dashList
        else:
            retList.extend(dashList)
            return retList
Esempio n. 3
0
    def getDirectLinks(self,
                       url,
                       formats='flv, mp4',
                       dash=True,
                       dashSepareteList=False):
        printDBG('YouTubeParser.getDirectLinks')
        if config.plugins.iptvplayer.ytUseDF.value:  # temporary workaround, similar code should be executed after getDirectLinks
            dash = False
        list = []
        try:
            list = YoutubeIE()._real_extract(url)
        except Exception:
            printExc()
            if dashSepareteList:
                return [], []
            else:
                return []

        retList = []
        # filter dash
        dashAudioLists = []
        dashVideoLists = []
        if dash:
            # separete audio and video links
            for item in list:
                if 'mp4a' == item['ext']:
                    dashAudioLists.append(item)
                elif 'mp4v' == item['ext']:
                    dashVideoLists.append(item)
            # sort by quality -> format
            def _key(x):
                if x['format'].startswith('>'):
                    int(x['format'][1:-1])
                else:
                    int(x['format'][:-1])

            dashAudioLists = sorted(dashAudioLists, key=_key, reverse=True)
            dashVideoLists = sorted(dashVideoLists, key=_key, reverse=True)

        for item in list:
            printDBG(">>>>>>>>>>>>>>>>>>>>>")
            printDBG(item)
            printDBG("<<<<<<<<<<<<<<<<<<<<<")
            if -1 < formats.find(item['ext']):
                if 'yes' == item['m3u8']:
                    format = re.search('([0-9]+?)p$', item['format'])
                    if format != None:
                        item['format'] = format.group(1) + "x"
                        item['ext'] = item['ext'] + "_M3U8"
                        item['url'] = decorateUrl(item['url'],
                                                  {"iptv_proto": "m3u8"})
                        retList.append(item)
                else:
                    format = re.search('([0-9]+?x[0-9]+?$)', item['format'])
                    if format != None:
                        item['format'] = format.group(1)
                        item['url'] = decorateUrl(item['url'])
                        retList.append(item)

        dashList = []
        if len(dashAudioLists):
            # use best audio
            for item in dashVideoLists:
                item = dict(item)
                item["url"] = decorateUrl(
                    "merge://audio_url|video_url", {
                        'audio_url': dashAudioLists[0]['url'],
                        'video_url': item['url'],
                        'prefered_merger': 'MP4box'
                    })
                dashList.append(item)

        # try to get hls format with alternative method
        if 0 == len(retList):
            try:
                video_id = YoutubeIE()._extract_id(url)
                url = 'http://www.youtube.com/watch?v=%s&gl=US&hl=en&has_verified=1' % video_id
                sts, data = self.cm.getPage(
                    url, {
                        'header': {
                            'User-agent':
                            'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10'
                        }
                    })
                if sts:
                    data = data.replace('\\"', '"').replace('\\\\\\/', '/')
                    hlsUrl = self.cm.ph.getSearchGroups(
                        data, '''"hlsvp"\s*:\s*"(http[^"]+?)"''')[0]
                    if '' != hlsUrl:
                        hlsList = getDirectM3U8Playlist(hlsUrl)
                        if len(hlsList):
                            dashList = []
                            for item in hlsList:
                                item['format'] = "%sx%s" % (item.get(
                                    'heigth', 0), item.get('with', 0))
                                item['ext'] = "m3u8"
                                retList.append(item)
            except Exception:
                printExc()

        if dashSepareteList:
            return retList, dashList
        else:
            retList.extend(dashList)
            return retList