在上文已经获取到m3u8地址, 直接上代码
import requests import os #需要安装 from Crypto.Cipher import AES import uuid def m3u8(url): base_url = url[:url.rfind('/')+1]#如果需要拼接url,则启用 , +1 把 / 加上 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'} rs = requests.get(url,headers=headers).text list_content = rs.split('\n') player_list = [] #如果没有merge文件夹则新建merge文件夹,用于存放ts文件 if not os.path.exists('merge'): os.system('mkdir merge') key = '' for index,line in enumerate(list_content): # 判断视频是否经过AES-128加密 if "#EXT-X-KEY" in line: method_pos = line.find("METHOD") comma_pos = line.find(",") method = line[method_pos:comma_pos].split('=')[1]#获取加密方式 print("Decode Method:", method) uri_pos = line.find("URI") quotation_mark_pos = line.rfind('"') key_path = line[uri_pos:quotation_mark_pos].split('"')[1] key_url = key_path res = requests.get(key_url) key = res.content #获取加密密钥 print("key:", key) #以下拼接方式可能会根据自己的需求进行改动 if '#EXTINF' in line: # href = '' # 如果加密,直接提取每一级的.ts文件链接地址 if 'http' in list_content[index + 1]: href = list_content[index + 1] player_list.append(href) # 如果没有加密,构造出url链接 elif('ad0.ts' not in list_content[index + 1]): href = base_url + list_content[index+1] player_list.append(href) if(len(key)): print('此视频经过加密') print(player_list)#打印ts地址列表 for i,j in enumerate(player_list): cryptor = AES.new(key, AES.MODE_CBC, key) res = requests.get(j,headers=headers) with open('merge/' + str(i+1) + '.ts','wb') as file: file.write(cryptor.decrypt(res.content))#将解密后的视频写入文件 print('正在写入第{}个文件'.format(i+1)) else: print('此视频未加密') print(player_list)#打印ts地址列表 #生成随机名 ret =uuid.uuid1() path = os.getcwd() + '\merge' # 获取视频存放路径 #合成并.ts文件,自定义 new_file = u"{}\{}.ts".format(path,ret) #合并成一个文件 with open(new_file,'wb+') as file: for i,j in enumerate(player_list): res = requests.get(url=j, headers=headers) file.write(res.content) print('正在写入第{}个文件'.format(i + 1)) print('下载完成') if __name__ == '__main__': #地址为上文获取的m3u8地址 url ="https://apd-708650dfbd236a3bf3dd058958fa0147.v.smtcdns.com/sportsts.tc.qq.com/AY2Ug9CnIkEJD7lnyT5NTyKh33kOsMDch1jipcvMvjZ4/uwMROfz2r5zAoaQXGdGnC2df644E7D3uP8M8pmtgwsRK9nEL/a779mIeJvMkCYxLB2cTj1Q1MVoScAoewQwoeGxZgG02L9Mg0jDHnbT2nrvP9dQoSVhMCzrz4RzrvTBhIq0oWgGF6dBF13OmdUziwra6awnjrCfz93qCZ4x-24ZUnuG5ssRkl_SULo1ji4o8Z9473KEGTZSGhDF-Yi_eFu1GsNmg/f0034qpo5mh.321002.ts.m3u8?ver=4" # 下载视频 m3u8(url)