[Python] 纯文本查看 复制代码 import requests
import m3u8
import re
import base64
from Crypto.Cipher import AES
def parse_m3u8_with_library(m3u8_url):
# 下载M3U8文件内容
response = requests.get(m3u8_url)
if response.status_code != 200:
raise Exception(f"Failed to download M3U8 file from {m3u8_url}")
# 解析M3U8文件
m3u8_obj = m3u8.loads(response.text)
# 获取媒体片段URL
media_urls = [str(segment.uri) for segment in m3u8_obj.segments]
return media_urls
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',
'Origin': 'https://www.zhiwuyakaoyan.com',
'Referer': 'https://www.zhiwuyakaoyan.com/',
}
params = {
'pid': '1728299822653X1054806',
'device': 'desktop',
}
m3u8_url = "https://hls.videocc.net/acf54bc238/7/acf54bc2384d54119ab27cbea261b3c7_3.m3u8"
data = requests.post('https://www.zhiwuyakaoyan.com/japi/chapters/getFreeTrialPlay', params={'course_id': '1332'},
headers=headers).json()['data']
print(data)
response = requests.get(m3u8_url, params=params, headers=headers)
m3u8_content = response.text
# print(m3u8_content)
# 解析m3u8文件内容,查找EXT-X-KEY标签
ext_x_key_pattern = re.compile(r'#EXT-X-KEY:(?:METHOD=([^,]+),)?URI="([^"]+)",?IV=(.*)')
matches = ext_x_key_pattern.findall(m3u8_content)
print(matches)
for method, uri, iv in matches:
# 构建完整的密钥文件URL
key_url = uri
# 发送请求获取密钥文件内容
key_response = requests.get(key_url, params={'token': data['token']}, headers=headers)
key_content = key_response.content
print(key_content)
# 如果是AES-128加密,密钥文件内容就是base64编码的密钥
if method == 'AES-128':
key = base64.b64encode(key_content).decode()
print(f"密钥: {key}")
# IV通常是第一个TS片段的第一个16字节,但这取决于具体的实现
# 这里假设IV是固定的,实际上你可能需要解析第一个TS片段来获取IV
iv = iv.split('x')[1]
print(f"初始化向量: {iv}")
media_urls = parse_m3u8_with_library(m3u8_url)
for url in media_urls:
with open(f"video{media_urls.index(url)}.ts", "wb") as f:
response = requests.get(url, headers=headers)
f.write(AES.new(key_content, AES.MODE_CBC, key_content).decrypt(response.content))
break
|