How to use the m3u8.load function in m3u8

To help you get started, we’ve selected a few m3u8 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github globocom / m3u8 / tests / test_loader.py View on Github external
def test_load_should_create_object_from_uri():
    obj = m3u8.load(playlists.SIMPLE_PLAYLIST_URI)
    assert isinstance(obj, m3u8.M3U8)
    assert 5220 == obj.target_duration
    assert 'http://media.example.com/entire.ts' == obj.segments[0].uri
github wlerin / showroom / showroom / hls.py View on Github external
def load_m3u8(src, url=None, headers=None):
    # is playlist raw text, a path to a file, or a url?
    if src.upper().startswith('#EXTM3U'):
        m3u8_obj = m3u8.loads(src)
        m3u8_obj.playlist_url = url
        m3u8_obj.base_uri = _parsed_url(url)
    elif os.path.exists(src):
        m3u8_obj = m3u8.load(src)
        m3u8_obj.playlist_url = url
        m3u8_obj.base_uri = _parsed_url(url)
    else:
        m3u8_obj = m3u8.load(src, headers=headers or DEFAULT_HEADERS)
        m3u8_obj.playlist_url = src
    return m3u8_obj
github kedpter / M3u8Downloader / src / m3u8_dl / M3u8Downloader.py View on Github external
def parse_file(self):
        self.m3u8_obj = m3u8.load(self.output_file)
        return self.m3u8_obj
github makelove / Python_Master_Courses / 图像视频处理 / 怎样下载m3u8视频 / download_m3u8.py View on Github external
def main(url):
    m = m3u8.load(url)
    #
    print('请输入序号,你想看哪个清晰度:')
    for i, pl in enumerate(m.playlists):
        print(f"{i}:{pl.stream_info.resolution}")
    indexs: str = input('请输入序号,你想看哪个清晰度(默认720p-2):')
    indexs = '2' if indexs == '' else indexs
    if indexs.isnumeric():
        idx = int(indexs)
        pl = m.playlists[idx]
        m2 = m3u8.load(pl.absolute_uri)
        print('开始下载 ts列表...')
        for sm in m2.segments:
            url2 = sm.absolute_uri
            print(url2)
            urlretrieve(url2, sm.uri)
        print('下载完毕')

        # 合并ts片段,存为与文件夹同名的ts文件
        print('开始合并文件:')
        fn = input('输入文件名:')
        fn = f"{randint(1000, 9999)}" if fn == '' else fn
        fn = fn + '.mp4'

        with open(fn, 'wb') as f:
            for sm in m2.segments:
                # file_path = os.path.join(directory, f'{n}.ts')
github Eyevinn / hls-relay / hlsrelay / __init__.py View on Github external
def parseMaster(self):
        obj = m3u8.load(self.src)
        if not obj.is_variant:
            raise Exception("Source is not a HLS master playlist")
        for p in obj.playlists:
            mediaplaylist = {}
            mediaplaylist['lastUpdated'] = time.ctime()
            mediaplaylist['uri'] = p.uri
            mediaplaylist['relayedsegments'] = {}
            mediaplaylist['state'] = STATE_MEDIA_PLAYLIST_INIT
            if not re.match('^http', mediaplaylist['uri']):
                mediaplaylist['base'] = obj.base_uri
                mediaplaylist['uri'] = obj.base_uri + p.uri
                mediaplaylist['filename'] = p.uri
                mediaplaylist['ETag'] = ''
            mediaplaylist['m3u8'] = m3u8.load(mediaplaylist['uri'])
            if mediaplaylist['m3u8'].is_endlist:
                raise Exception("VOD not supported")
github arnesongit / plugin.audio.tidal2 / resources / lib / koditidal.py View on Github external
msg = _T(30505)
            try:
                msg = r.reason
                msg = r.json().get('userMessage')
            except:
                pass
            log('HTTP-Error: ' + msg, xbmc.LOGERROR)
            log('Got no HTTP Stream for Video ID %s, using HLS Stream ...' % video_id, xbmc.LOGERROR)
            xbmcgui.Dialog().notification(plugin.name, _T(30510), xbmcgui.NOTIFICATION_WARNING)
        if not media:
            # Using HLS-Stream 
            self.session_id = self.stream_session_id
            media = Session.get_video_url(self, video_id, quality=None)
        if maxVideoHeight <> 9999 and media.url.lower().find('.m3u8') > 0:
            log('Parsing M3U8 Playlist: %s' % media.url)
            m3u8obj = m3u8_load(media.url)
            if m3u8obj.is_variant and not m3u8obj.cookies:
                # Variant Streams with Cookies have to be played without stream selection.
                # You can change the Bandwidth Limit in Kodi Settings to select other streams !
                # Select stream with highest resolution <= maxVideoHeight
                selected_height = 0
                selected_bandwidth = -1
                for playlist in m3u8obj.playlists:
                    try:
                        width, height = playlist.stream_info.resolution
                        bandwidth = playlist.stream_info.average_bandwidth
                        if not bandwidth:
                            bandwidth = playlist.stream_info.bandwidth
                        if not bandwidth:
                            bandwidth = 0
                        if (height > selected_height or (height == selected_height and bandwidth > selected_bandwidth)) and height <= maxVideoHeight:
                            if re.match(r'https?://', playlist.uri):
github ilyalissoboi / twitch_downloader / downloader.py View on Github external
app.pargs.name = output_file_name

        assert video_type == 'v'

        # Get access code
        url = _vod_api_url.format(video_id)
        r = requests.get(url, headers=common_headers)
        data = r.json()

        # Fetch vod index
        url = _index_api_url.format(video_id)
        payload = {'nauth': data['token'], 'nauthsig': data['sig'], 'allow_source': True, 'allow_spectre': False, "player": "twitchweb", "p": int(random() * 999999), "allow_audio_only": True, "type": "any"}
        r = requests.get(url, params=payload, headers=common_headers)
        m = m3u8.loads(r.content)
        index_url = m.playlists[0].uri
        index = m3u8.load(index_url)

        # Get the piece we need
        position = 0
        chunks = []

        for seg in index.segments:
            # Add duration of current segment
            position += seg.duration

            # Check if we have gotten to the start of the clip
            if position < int(app.pargs.start):
                continue

            # Extract clip name and byte range
            p = re.match(_chunk_re, seg.absolute_uri)
            # match for playlists without byte offsets