使用python和ffmpeg下载并合并加密m3u8列表里的ts文件
最近看到某有色网站的视频不错,可是这个网站的网速不太理想,就想下载看,偏偏视频又是加密的,遂破解之。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | # -*- coding: utf-8 -*- """ Created on 2022-03-21 0021 21:45 @Software : PyCharm @author: Administrator """ import requests, os, pyperclip, re from tqdm import tqdm path = r 'E:\pojie' headers = { 'Connection' : 'keep-alive' , '其他header' : '信息隐藏' , } proxy = { 'http' : None , 'https' : None } def get_res(url, file_path): """ 下载ts视频流 :param url: ts流链接 :param file_path: 临时文件路径 :param key: 加密密钥 """ try : response = requests.get(url = url, timeout = 120 , headers = headers) with open (file_path, 'wb+' ) as f: f.write(response.content) except Exception as e: print (e) def get_list(url): response = requests.get(url, headers = headers, proxies = proxy) return response def get_keys(url): """ 获取加密密钥 :param url: 密钥网址 :return: """ response = requests.get(url, proxies = proxy) return response def write_keys(response, filename): """ 写入密钥文件 :param response: 返回的密钥响应 :param filename: 密钥文件名 :return: """ file_path = os.path.join(path, filename) # print(file_path) with open (file_path, 'wb+' ) as fp: fp.write(response.content) def get_links_from_file(path): """ 从文件中批量读取网址 :param path: 文件所在位置 :return: """ with open (path, 'r' , encoding = 'utf-8' ) as fp: st = fp.readlines() return st def write_list(list_path1, list_path2, keystring): """ 重写本地m3u8文件,用于ffmpeg合成用 :param list_path1: :param list_path2: :param keystring: 记录密钥信息的那一行的字符串, 例如''#EXT-X-KEY:METHOD=AES-128,URI="E:/pojie/videokey",IV=0x00000000000000000000000000000000\n' :return: 不返回信息 """ fp1 = open (list_path1, 'r' , encoding = 'utf-8' ) fp2 = open (list_path2, 'w+' , encoding = 'utf-8' ) st_list = fp1.readlines() fp1.close() index = 0 for st in st_list: if st[: 10 ] = = '#EXT-X-KEY' : if index < 1 : fp2.write(keystring) continue elif st[: 1 ] = = "#" : fp2.write(st) else : if index < 9 : filename = f '00{index + 1}.ts' elif index < 99 : filename = f '0{index + 1}.ts' else : filename = f '{index + 1}.ts' fp2.write(filename + '\n' ) index + = 1 fp2.close() def get_key_url(path): """ 从下载好的m3u8文件里读取密钥网址 :param path: :return: """ with open (path, 'r' ) as f: st = f.read() ur = re.findall( '#EXT-X-KEY:METHOD=AES-128,URI="(.*?)"' , st) url = ur[ 0 ] # print(url) return url def write_video(response, filename): """ 下载视频 :param response: response响应 :param filename: 视频保存的文件名 :return: """ video_path = os.path.join(path, filename) if not os.path.exists(path): os.mkdir(path) with open (video_path, 'wb+' ) as fp: fp.write(response.content) def join_video(listname, newfilename): """ 利用ffmpeg合并加密的ts文件 :param listname: 本地m3u8文件路径 :param newfilename: 待合成的文件名称 :return: """ command = f 'ffmpeg -allowed_extensions ALL -i {listname} -c copy {newfilename}' command1 = 'cd /d E:/pojie' print (command) os.system(command1) os.system(command) def d_k(path): """ 下载密钥 :return: """ filename = 'videokey' url = get_key_url(path) res = get_keys(url) write_keys(res, filename) return res def d_l(url): """ 下载m3u8列表 :return: """ filename = '01.m3u8' res = get_list(url) write_video(res, filename) def d_v(url_left, list_path, key): """ 下载视频 :return: """ url_list = get_links_from_file(list_path) index = 0 for url in url_list: if url[: 1 ] = = 'v' : if index < 9 : filename = f '00{index + 1}.ts' elif index < 99 : filename = f '0{index + 1}.ts' else : filename = f '{index + 1}.ts' new_url = url_left + url new_url = new_url.strip() file_path = os.path.join(path, filename) downloadFile(new_url, file_path) # time.sleep(2) index + = 1 else : continue def get_url_left(url): """ 读取m3u8的网址前缀 :param url: :return: """ url_list = url.split( '/' ) url_list[ 1 ] = 'https:/' new_url = '/' .join(url_list[ 1 : - 1 ]) new_url = new_url + '/' return new_url def rename(): """ 重命名合成后的文件 :return: """ txt_path = r 'C:\Users\rabbit\PycharmProjects\utils\video\links.txt' fp = open (txt_path, 'r' , encoding = 'utf-8' ) st_list = fp.readlines() fp.close() path1 = r 'E:\pojie' old_path = r "E:\pojie\03.mp4" filename = st_list[ 0 ].strip() + '.mp4' new_path = os.path.join(path1, filename) os.rename(old_path, new_path) fo = open (txt_path, 'w' , encoding = 'utf-8' ) for index, line in enumerate (st_list): if index = = 0 : continue else : fo.write(line) fo.close() return new_path def delete_TS(): """ 永久删除下载的ts视频流分段 :return: """ path = r 'E:\pojie' file_list = os.listdir(path) for file in file_list: if file [ - 2 :] = = 'ts' : print (f '{file}已删除' ) file_path = os.path.join(path, file ) os.remove(file_path) def downloadFile(url, name): """ 下载实时显示速度 :param url: :param name: :return: """ # print(url) # 用流stream的方式获取url的数据 resp = requests.get(url, stream = True , headers = headers, proxies = proxy) # 拿到文件的长度,并把total初始化为0 total = int (resp.headers.get( 'content-length' , 0 )) # 打开当前目录的fname文件(名字你来传入) # 初始化tqdm,传入总数,文件名等数据,接着就是写入,更新等操作了 with open (name, 'wb+' ) as file , tqdm( desc = name, total = total, unit = 'iB' , unit_scale = True , unit_divisor = 1024 , ) as bar: for data in resp.iter_content(chunk_size = 1024 ): size = file .write(data) bar.update(size) def mainone(): """ 主程序入口 :return: """ # 读取剪贴板中的m3u8链接 url = pyperclip.paste() if not os.path.exists(path): os.mkdir(path) path1 = "E:\\pojie\\01.m3u8" path2 = "E:\\pojie\\02.m3u8" keystring = '#EXT-X-KEY:METHOD=AES-128,URI="E:/pojie/videokey",IV=0x00000000000000000000000000000000\n' # 获取m3u8链接前缀,用于拼接ts网址 url_left = get_url_left(url) # 下载m3u8列表 d_l(url) # 下载密钥,保存到此文件中,E:/pojie/videokey res = d_k(path1) # 下载TS分段 d_v(url_left, path1, res.content) # 下载完分段后,重写本地的m3u8文件,方便后续使用ffmpeg合成 write_list(path1, path2, keystring) filename = 'E:\\pojie\\03.mp4' # 合并文件 join_video(path2, filename) # 读取目录文件并重命名 np = rename() # 删除TS分段 delete_TS() # 输出已下载好的文件名 print (np) if __name__ = = '__main__' : mainone() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库