Python解析并批量下载m3u8播放列表后自动合并

import os, sys, requests


def show_progress(processed,total):
    """
    简易进度条
    """
    percent=(int(processed)/int(total))*100

    bar_length=50
    hashes = '#' * int(percent/100.0 * bar_length)
    spaces = ' ' * (bar_length - len(hashes))
    sys.stdout.write('\rPercent: [%s] %d%%[%s/%s]'%(hashes+spaces,percent,processed,total))


lines = []

# 解析并获取所有需要下载的ts分段
with open('file.m3u8','r+') as f:
    for line in f.readlines():
        line=line.strip()
        if (line.endswith('ts')):
            url=line
            lines.append(url)

i=0
total=len(lines)

# 开始下载
for line in lines:
    filepath = line
    i=i+1
    if (not os.path.isfile(filepath)):
        url = 'URLPREFIX/{}'.format(line)
        data = requests.get(url)
        with open(filepath,'wb') as code:
            code.write(data.content)

    show_progress(i,total)

# 调用ffmpeg进行合并
os.system('ffmpeg -i file.m3u8 -c copy file.mkv')
去打赏

您的支持将鼓励我们继续创作!

[微信] 扫描二维码打赏

正在跳转到PayPal...

发表评论

电子邮件地址不会被公开。