Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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
def parse_file(self):
self.m3u8_obj = m3u8.load(self.output_file)
return self.m3u8_obj
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')
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")
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):
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