前言:
使用python下载ts文件很顺利,如果用python合并保存ts文件经常会报错。
于是,从网上找到ffmpeg工具,对ts文件进行合并。
完整实例:
# coding:utf-8import requestsimport timefrom multiprocessing.dummy import Poolimport redef get_content(url): print("正在获取:", url) # get方法是一个阻塞的方法 resp = requests.get(url, headers=headers) if resp.status_code == 200: name = url.split("/")[-1] with open(f"D:\m3u8\{name}.ts", "wb") as f: f.write(resp.content) print(name + "下载成功")headers = { "User-Agent": "Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KABUL, like Gecko) " "Chrome/86.0.4240.198Safari/537.36 " }url = "https://new.qqaku.com/20221023/1DhUmOUv/1100kb/hls/index.m3u8"resp = requests.get(url=url, headers=headers).textindex_list = re.findall(r'https(.*?).ts', resp)ts_urls = []for i in index_list: title = i.split("/")[-1] ts_url = "https" + i + ".ts" ts_urls.append(ts_url)start_time = time.time()# 实例化一个线程池对象pool = Pool(30)# 将列表中的每一个列表元素传递给get_content中进行处理。content = pool.map(get_content, ts_urls)end_time = time.time()print('%d second'% (end_time-start_time))pool.close()pool.join()
ffmpeg合并ts文件:
1.新建ts.txt文档
2.执行ffmpeg命令
ffmpeg.exe -f concat -safe 0 -i ts.txt -c copy out.mp4