zl程序教程

您现在的位置是:首页 >  其他

当前栏目

python - 多线程下载m3u8

2023-04-18 15:51:25 时间
import requests
import m3u8
import os
from multiprocessing.dummy import Pool
from tqdm import tqdm
from retry import retry
from urllib.parse import urljoin

@retry(tries=3, delay=2) # 重试3次, 间隔2s
def download(i, url):
    res = requests.get(url,stream=True)
    with open(f"./{i}.ts", "wb") as f:
        f.write(res.content)

def merge(total):
    with open("./test.mp4", "ab") as f:
        for i in range(total):
            try:
                path = f"./{i}.ts"
                content = open(path, "rb").read()
                f.write(content)
                os.remove(path)
            except Exception as e:
                print(e)

url = "https://vip.lz-cdn14.com/20220812/7879_a40a495a/1200k/hls/index.m3u8"
m3u8_data = m3u8.load(url).data
ts_list = []
for i in range(len(m3u8_data["segments"])):
    ts_list.append(urljoin(url, m3u8_data["segments"][i]["uri"]))
total = len(ts_list)

pbar = tqdm(total=total)
pbar.set_description("进度条")
update = lambda *args: pbar.update()

if __name__ == "__main__":
    pool = Pool(64)
    for i in range(total):
        if not os.path.exists(f"./{i}.ts"):
            pool.apply_async(download, args=(i, ts_list[i],), callback=update)
    pool.close()
    pool.join()  # 阻塞主线程
    
    merge(total)

效果如下
image
image