优化
该版本兼容3种模式:
1.无需解密ts
2.需解密,但需要手动输入key_url
3.需解密,但不需要手动输入key_url
执行过程
def main():
if not os.path.isdir('video'):
os.mkdir('video')
title = input('请输入电影名称:').strip()
# 1-1 是否需要手动输入key_url
manual = input('是否需要手动输入key_url(Y/N):').strip().upper()
lst, lst_n = get_lst()
thread_download(lst, lst_n)
# 1-2 如果需要手动输入,输入key_url,解码
if manual == 'Y':
key_url = input('请输入key_url:').strip()
thread_decode(lst, lst_n, key_url)
# 1-3 如果不需要手动输入,m3u8文件中key不为空,自动获取key_url,若为空,直接运行合并
else:
key = judgment()
if key is not None:
key_url = key.group(1)
thread_decode(lst, lst_n, key_url)
merge(len(lst), title)
完整代码
import os
from concurrent.futures import ThreadPoolExecutor
import re
import requests
from Crypto.Cipher import AES
from tqdm import tqdm
def get_lst():
task = []
num = []
with open('1.m3u8', 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line.startswith('#'):
continue
line = line
task.append(line)
for i in range(len(task)):
num.append(i)
return task, num
def download(url, name):
resp = requests.get(url, timeout=10)
with open(f'video/{name}', 'wb') as f1:
f1.write(resp.content)
print(f'{name}下载完成')
def thread_download(lst, lst_n):
with ThreadPoolExecutor(50) as t:
for i in lst_n:
t.submit(download, name=i, url=lst[i])
check(lst, lst_n)
def check(lst, lst_n):
lst_ns = []
for i in lst_n:
if not os.path.isfile(f'video/{i}'):
lst_ns.append(i)
if lst_ns:
print(lst_ns)
print('开始递归==================================================')
thread_download(lst, lst_ns)
else:
print('全部下载完成')
def merge(num, title):
with open(f'video/{title}.mp4', 'wb') as f:
for i in tqdm(range(num)):
if os.path.isfile(f'video/{i}_1'):
with open(f'video/{i}_1', 'rb') as f1:
f.write(f1.read())
else:
with open(f'video/{i}', 'rb') as f1:
f.write(f1.read())
print('合并完成')
delete_ts(num)
def decode_ts(name, url):
resp = requests.get(url)
key = resp.content
iv = b'0000000000000000'
aes = AES.new(key, AES.MODE_CBC, iv)
with open(f'video/{name}', 'rb') as f:
de_f = aes.decrypt(f.read())
with open(f'video/{name}_1', 'wb') as f1:
f1.write(de_f)
print(f'{name}解码完成')
def thread_decode(lst, lst_n, key_url):
with ThreadPoolExecutor(50) as t:
for i in lst_n:
t.submit(decode_ts, name=i, url=key_url)
check_de_ts(lst, lst_n, key_url)
def check_de_ts(lst, lst_n, key_url):
lst_ns = []
for i in lst_n:
if not os.path.isfile(f'video/{i}_1'):
lst_ns.append(i)
if lst_ns:
print(lst_ns)
print('开始递归==================================================')
thread_download(lst, lst_ns)
thread_decode(lst, lst_ns, key_url)
else:
print('全部ts解密完成')
def delete_ts(num):
for i in range(num):
os.remove(f'video/{i}')
if os.path.isfile(f'video/{i}_1'):
os.remove(f'video/{i}_1')
def judgment():
with open('1.m3u8', 'r', encoding='utf-8') as f:
obj = re.compile(r'URI="(.*?)"')
key = obj.search(f.read())
return key
def main():
if not os.path.isdir('video'):
os.mkdir('video')
title = input('请输入电影名称:').strip()
manual = input('是否需要手动输入key_url(Y/N):').strip().upper()
lst, lst_n = get_lst()
thread_download(lst, lst_n)
if manual == 'Y':
key_url = input('请输入key_url:').strip()
thread_decode(lst, lst_n, key_url)
else:
key = judgment()
if key is not None:
key_url = key.group(1)
thread_decode(lst, lst_n, key_url)
merge(len(lst), title)
if __name__ == '__main__':
main()