def getHLSPlaylist(url, bFindBest=True): ''' given a URL, determine if it's an HTTP live streaming playlist and find the best segments to use (only if we dont trust ffmpeg) ''' # make sure it's a playlist at all, safe to rsplit urlPath = url.rsplit('?')[0] if False == urlPath.endswith('m3u8'): return url isHls = True xbmc.log("getHLSPlaylist url=" + url, xbmc.LOGDEBUG) # obtain the playlist and save any cookie that might be set. urlllib will join Set-Cookie headers based on RFC (one of them :) req = urllib2.Request(url) response = urllib2.urlopen(req) playlistStr = response.read() #hls_cookie = cleanCookie(response.info().getheader('Set-Cookie')) response.close() # parse m3u8 to find the best bitrate segments. if not variant, return original URL urlPath = url if bFindBest == True: variant_m3u8 = m3u8.loads(playlistStr) if True == variant_m3u8.is_variant: maxBW = 0 maxIdx = 0 for i, playlist in enumerate(variant_m3u8.playlists): bw = int(playlist.stream_info.bandwidth) if bw > maxBW: maxBW = bw maxIdx = i playlist = variant_m3u8.playlists[maxIdx] # build segments URL # TODO: Pass this to real url parsing engine, since it might be relative and might be absalute #urlPath = urlPath.rsplit('/', 1)[0] # removes the filename #urlPath = urlPath + '/' + playlist.uri return playlist.uri return urlPath
def processHLSPlaylist(self, bFindBest=False): ''' given a URL, determine if it's an HTTP live streaming playlist and find the best segments to use (only if we dont trust ffmpeg) ''' # make sure it's a playlist at all, safe to rsplit urlPath = self.__stream.rsplit('?')[0] if False == urlPath.endswith('m3u8'): return self.__stream self.__isHls = True # obtain the playlist and save any cookie that might be set. urlllib will join Set-Cookie headers based on RFC (one of them :) req = urllib2.Request(self.__stream) response = urllib2.urlopen(req) playlistStr = response.read() self.__hls_cookie = self.cleanCookie( response.info().getheader('Set-Cookie')) response.close() # parse m3u8 to find the best bitrate segments. if not variant, return original URL urlPath = self.__stream if bFindBest == True: variant_m3u8 = m3u8.loads(playlistStr) if True == variant_m3u8.is_variant: maxBW = 0 maxIdx = 0 for i, playlist in enumerate(variant_m3u8.playlists): bw = int(playlist.stream_info.bandwidth) if bw > maxBW: maxBW = bw maxIdx = i playlist = variant_m3u8.playlists[maxIdx] # build segments URL urlPath = urlPath.rsplit('/', 1)[0] # removes the filename urlPath = urlPath + '/' + playlist.uri return urlPath
def processHLSPlaylist(self, bFindBest=False): ''' given a URL, determine if it's an HTTP live streaming playlist and find the best segments to use (only if we dont trust ffmpeg) ''' # make sure it's a playlist at all, safe to rsplit urlPath = self.__stream.rsplit('?')[0] if False == urlPath.endswith('m3u8'): return self.__stream self.__isHls = True # obtain the playlist and save any cookie that might be set. urlllib will join Set-Cookie headers based on RFC (one of them :) req = urllib2.Request(self.__stream) response = urllib2.urlopen(req) playlistStr = response.read() self.__hls_cookie = self.cleanCookie(response.info().getheader('Set-Cookie')) response.close() # parse m3u8 to find the best bitrate segments. if not variant, return original URL urlPath = self.__stream if bFindBest == True: variant_m3u8 = m3u8.loads(playlistStr) if True == variant_m3u8.is_variant: maxBW=0 maxIdx=0 for i, playlist in enumerate(variant_m3u8.playlists): bw = int(playlist.stream_info.bandwidth) if bw > maxBW: maxBW = bw maxIdx = i playlist = variant_m3u8.playlists[maxIdx] # build segments URL urlPath = urlPath.rsplit('/', 1)[0] # removes the filename urlPath = urlPath + '/' + playlist.uri return urlPath