准备
安装ffmpeg
GitHub上下载windows版本即可
解压
添加环境变量
- 将上面的bin路径添加到系统变量
查看安装
- CMD下输入
ffmpeg -version
2. Demo
- 下载ts文件
- 下载index.m3u8
- 下载加密文件key.key(一般视频会加密,我这里下载下来就是16进制的)
2.1 下载TS
import requests import re url = "访问的网址index.m3u8" res = requests.get(url).text # print(res) # 获取ts文件,正则表达式切割 ts_url_list = re.findall(r"(?<=/hls/)[0-9,a-z,A-Z.]+", res, flags=re.S) # 去掉了key.key ts_url_list.pop(0) # print(ts) # ts的下载前缀 url_prifix = "ts的下载前缀路径" # 写的txt文件 f = open('./filename.txt', mode="w") for ts_url in ts_url_list: u = url_prifix+ts_url # print(ts_url) f.write("file '" + ts_url+"'\n") # r = requests.get(url=u, headers=headers).content r = requests.get(url=u).content print(ts_url, u) # 二进制写入到本地 with open('./ts/'+ts_url, mode="wb") as file: file.write(r) f.close()
2.2 key.key文件
2.3 TS文件
2.4 下载index.m3u8
- 将下面修改对应的路径
D:/Cumtb_Code/ts1/key.key
2.5 ffmpeg合并
ffmpeg -f concat -safe 0 -i filename.txt -c copy output.mp4 ffmpeg -allowed_extensions ALL -i index.m3u8 -c copy a_new.mp4
3. 多线程下载
''' Description: henggao_note version: v1.0.0 Date: 2022-08-05 18:09:11 LastEditors: henggao LastEditTime: 2022-08-05 20:26:56 ''' import requests import threading import datetime import re count =0; def Handler(start, end, url, filename): #headers = {'Range': 'bytes=%d-%d' % (start, end-1)} #r = requests.get(url, headers=headers, stream=True) url_prifix = "https://ll.bdzybf9.com/20220217/kugexC1F/2000kb/hls/" for i in filename[start:end]: global count u = url_prifix + i r = requests.get(url=u,verify=False).content # 二进制写入到本地 with open('./ts1/'+i, mode="wb") as file: file.write(r) count =count+1 print("下载进度:%.2f" % (count/len(filename))) def download_file(url, num_thread = 100): f = requests.get(url,verify=False).text # 获取ts文件 text_list = re.findall(r"(?<=/hls/)[0-9,a-z,A-Z.]+", f, flags=re.S) # ts_url_list.pop(0) s_list = [] for i in text_list: if i.find('#EX')==-1: s_list.append(i) file_size = len(s_list) # 启动多线程写文件 part = file_size // num_thread # 如果不能整除,最后一块应该多几个字节 for i in range(num_thread): start = part * i if i == num_thread - 1: # 最后一块 end = file_size else: end = start + part t = threading.Thread(target=Handler, kwargs={'start': start, 'end': end, 'url': url, 'filename': s_list}) t.setDaemon(True) t.start() # 等待所有线程下载完成 main_thread = threading.current_thread() for t in threading.enumerate(): if t is main_thread: continue t.join() #print('%s 下载完成' % file_name) if __name__ == '__main__': url ="https://ll.bdzybf9.com/20220217/kugexC1F/2000kb/hls/index.m3u8"; start = datetime.datetime.now().replace(microsecond=0) download_file(url) end = datetime.datetime.now().replace(microsecond=0) print("用时: ", end='') print(end-start)