m3u8 文件处理:media_ 替换问题及代码优化
在处理以 '#YUMING|https' 开头的 m3u8 文件时,需要将其中的 'media_' 部分替换为新的 URL。代码中关于 'media_' 替换的代码部分存在问题,需要进行调整。
'media_' 并非一个 URL,而只是一段文字,因此直接将其替换为新的 URL 即可。此外,代码中的顺序也是正确的,先替换再删除。
以下是优化后的代码示例:
if '#YUMING|https' in lines[0]:
url = lines[0][lines[0].index('https'):lines[0].index('/-/')+3]
response = requests.post(url + 'playlist.m3u8', headers=headers)
if response.status_code == 200:
print('成功:' + str(response.status_code))
new_url = response.url
new_url = new_url[:new_url.index('playlist.m3u8')]
# Replace all occurrences of 'media_' with the new URL
new_lines = []
for line in lines:
if 'media_' in line:
new_lines.append(line.replace('media_', new_url))
elif not line.startswith('#YUMING|https'):
new_lines.append(line)
# Write the new lines back to the original file (except for the first line if it starts with '#YUMING|https')
with open(file_path, 'w') as f:
for line in new_lines:
f.write(line)
else:
print('失效:' + str(response.status_code))
在替换 'media_' 时,直接将其替换为新的 URL,而不是加上新 URL 再加上 'media_'。
原文地址: https://www.cveoy.top/t/topic/nEnq 著作权归作者所有。请勿转载和采集!