多线程下载加密电视剧电影
代码部分
import os
import queue
import re
import subprocess
import threading
import time
import urllib.parse
import requests
from Cryptodome.Cipher import AES
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 safari/537.36'}
video_url = 'http://www.edugansu.com/dongzuopian/93guojiliechedajieanmosikexingdong/2-1.html'
res = requests.get(video_url, headers=headers)
soup = BeautifulSoup(res.text, 'lxml')
file_name = soup.find('title').text.split('抢先')[0]
print(file_name)
'''
<script>
var zanpiancms_player =
{"player":"\/public\/","url":"https:\/\/hnzy.bfvvs.com\/play\/Yer2EgBe\/index.m3u8",
"next":"http:\/\/m.hnxzdq.com\/vodplay\/64139-2-2.html","name":"hnm3u8","apiurl":null,"adtime":"0",
"adurl":"\/\/cdn.97bike.com\/loading.html","copyright":0,"danmu":{"status":0}};
</script>
'''
m3u8_url = 'https://hnzy.bfvvs.com/play/penkGqWa/index.m3u8'
print(m3u8_url)
ts_file_urls = requests.get(m3u8_url, headers=headers)
ts_file_urls = re.sub('#.*\n', '', ts_file_urls.text).replace('\n', ',').split(',')
ts_file_new_list = [i for i in ts_file_urls if i != '']
print(r'ts文件列表地址', ts_file_new_list)
print(len(ts_file_new_list))
def download_ts(urlQueue, aes, headers):
while True:
try:
temp = urlQueue.get_nowait()
url = temp[0]
n = temp[1]
except Exception as e:
break
response = requests.get(url, stream=True, headers=headers)
ts_path = "./ts_cache/%06d.ts" % n
with open(ts_path, "wb+") as file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
after = aes.decrypt(chunk)
file.write(after)
print("视频流ts文件:%06d.ts 下载完成!" % n)
current_dir = os.getcwd()
print(current_dir)
def make_folder(folder_name):
folder_name = folder_name.replace('/', '_').replace(':', '_').replace('*', '_').replace('?', '_').replace('"', '_') \
.replace('<', '_').replace('>', '_').replace('|', '_')
folder_name = folder_name.strip().replace(' ', '_')
new_folder_name = folder_name
new_folder_path = os.path.join(current_dir, new_folder_name)
if not os.path.exists(new_folder_path):
os.makedirs(new_folder_path)
print(f'文件夹名称为→{file_name}←的文件夹已成功创建!')
else:
print(f'文件夹名称为→{file_name}←的文件夹已存在!')
if __name__ == '__main__':
r = requests.get(m3u8_url, headers=headers)
urlQueue = queue.Queue()
for i in r.text.split('\n'):
if i.endswith('.ts'):
urlQueue.put([urllib.parse.urljoin(m3u8_url, i), urlQueue.qsize()])
elif 'URI' in i:
URI = urllib.parse.urljoin(m3u8_url, re.findall('URI="(.*?)"', i)[0])
print(URI)
enc_key = requests.get(URI, headers=headers).content
aes = AES.new(enc_key, AES.MODE_CBC, enc_key)
startTime = time.time()
threads = []
threadNum = 1
for i in range(threadNum):
t = threading.Thread(target=download_ts, args=(urlQueue, aes, headers))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
endTime = time.time()
print('完成ts下载, 所用时间: %s ' % (endTime - startTime), '秒')
make_folder(file_name)
ts_file_path = fr'{current_dir}\ts_cache\*.ts'
video_file_path = fr'{current_dir}\{file_name}\{file_name}.ts'
command = fr'copy/b {ts_file_path} {video_file_path}'
output = subprocess.getoutput(command)
print(fr'{file_name}.ts 已完成合并!')
file_list = []
for root, dirs, files in os.walk('D:/Demo/python_demo/video_test/ts_cache'):
for fn in files:
p = str(root + '/' + fn)
file_list.append(p)
for i in file_list:
os.remove(i)
print(r'已删除ts视频流文件!')
下载完成效果图