主要是么m3u8解密的问题,先拿到m3u8的url,请求这个url,里面有ts构造网址,iv偏移量,以及key的url.key是个二进制文件.m3u8文件信息基本如下:
# EXTM3U:.m3u8文件的格式定义
# EXT-X-KEY: 密钥的信息
# METHOD: 加密的方法,这里采用的是AES-128的加密方式
# URI: 密钥的地址,需要获取访问得到密钥的信息
# IV: 偏移量,AES加密的方法,通过这个密钥就可以解密,获取正确的视频信息
另外注意还有一小段网址,这个可以用来构造ts文件地址.话不多说,上代码(如有问题,可以扫码我头像加我微信,共同学习,共同进步):
import urllib3
from mitmproxy import ctx
from Crypto.Cipher import AES
import re
import requests
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)
def request(flow):
headers = {
'User-Agent': 'Mozilla/5.0 (Linux; U; Android 9; zh-cn; MI 8 Build/PKQ1.180729.001) AppleWebKit/533.1 (KHTML, like Gecko) Mobile Safari/533.1'}
request = flow.request
request_url = request.url
patten = re.compile(
'(https://1252524126.vod2.myqcloud.com/[a-z0-9]{29}/[a-z0-9]{27}/drm/)voddrm\.token\..*?m3u8\?time=\d{13}')
if re.match(patten, request_url):
result = re.match(patten, request_url)
m3u8_url = result.group()
video_parse = result.group(1)
res = requests.get(m3u8_url, headers, verify=False)
p_URI = re.compile('https://app\.xiaoe-tech\.com/get_video_key\.php\?edk=.*token=.{86}')
URI = re.search(p_URI, res.text).group()
p_IV = re.compile('IV=(.{34})')
IV = re.search(p_IV, res.text).group()
p_urlParse = re.compile('v.*type=mpegts')
urlParses = re.findall(p_urlParse, res.text)
filename = video_parse.split('/')[4]
vt = IV.replace("0x", "")[:16].encode()
for urlParse in urlParses:
ts_url = video_parse + urlParse
resp_tsdata = requests.get(ts_url, headers, verify=False).content
resp_keydata = requests.get(URI, headers, verify=False).content
cipher = AES.new(resp_keydata, AES.MODE_CBC, iv=vt)
with open(f'{filename}.mp4', 'ab+') as f:
f.write(cipher.decrypt(resp_tsdata))