python 根据m3u8,下载ts,聚合成mp4

# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@contact: [email protected]
@time: 2023/2/11 17:41 $
@desc:

"""
import os
import re
import requests
from Crypto.Cipher import AES


save_path = os.path.join(os.getcwd(),"res")
os.makedirs(save_path,exist_ok=True)
HEADER_URL = "https://p1.ocs.hjfile.cn"

url_in = "https://tq.cctalk.cc/hujiang_api?lessonid=1001011265529785227&t=0.2741343593335621"
lessonid = re.findall("lessonid=(.*?)&t",url_in)[0]
# 根据提供的网页链接获取m3u8链接
response = requests.get(url_in)
# print(response.text)
m3u8_url = HEADER_URL+response.json()["data"]["m3u8s"][0]["url"]
# print(m3u8_url)
# 从m3u8链接里获取key链接,并请求key链接获取密钥,并且提取ts链接
response_m3u8 = requests.get(m3u8_url)
# print(response_m3u8.text)
key_url = HEADER_URL+re.findall('URI="(.*?)",IV',response_m3u8.text)[0]
response_key = requests.get(key_url)
# 密钥
secret = response_key.content
# 提取ts链接
ts_url_list = re.finditer("/class_ocs(.*?)\.ts",response_m3u8.text)
for ts_url in ts_url_list:
    ts_url_real = HEADER_URL+ts_url.group()
    # 加载密钥
    cryptor = AES.new(secret, AES.MODE_CBC, secret)
    ts_content = requests.get(ts_url_real).content
    ts_name = ts_url_real.split("/")[-1]
    save_file = os.path.join(save_path,ts_name)
    with open(save_file,"wb")as fp:
        # 解密保存ts
        fp.write(cryptor.decrypt(ts_content))
# ts聚合成MP4
cmd = "copy /b"+" {}\*.ts {}\\1.mp4".format(save_path,os.getcwd()).replace("/","\\")
print(cmd)
os.system(cmd)


猜你喜欢

转载自blog.csdn.net/CXY00000/article/details/130036709