import requests
import re
from urllib.parse import urljoin
import os
import time
# 获取所有ts的url
def one_movie_get_ts(url):
s = requests.Session()
result = s.get(url)
pp = re.search(r'var main = "(.*?)";',result.text,)
full_url = urljoin(url,pp.group(1))
m3u8_url = s.get(full_url)
# 拼接 m3u8 完整 url
full_url2 = full_url[:full_url.index('index')] + m3u8_url.text.splitlines()[-1]
m3u8_text = s.get(full_url2)
all_ts = [i for i in re.findall(r'\w*\.ts',m3u8_text.text)]
return s,all_ts,full_url2
# 判断文件夹是否存在
def judge_path(path):
result = os.path.exists(path)
if result == True:
print('文件夹已存在')
else:
print('创建文件夹')
os.makedirs(path)
# 下载一个视频的 ts 文件
def get_ts(url,path):
s ,all_ts_url,full_url = one_movie_get_ts(url)
print(f'一共有{len(all_ts_url)}个ts文件数据需要下载')
print(full_url)
judge_path(path)
for index,i in enumerate(all_ts_url):
one_ts_res = s.get(urljoin(full_url,i))
with open('./{}/{}'.format(path,i),'wb') as f:
f.write(one_ts_res.content)
print(f'第{index+1}个下载: ',i,'下载完成')
# 合成视频
def join_ts(path):
all_files = os.listdir(f'./{path}')
# print(all_files)
with open(f'{path}.mp4','wb+') as f:
for i in all_files:
with open(f'{path}/{i}','rb') as one:
f.write(one.read())
print(i,'写入完成')
def get_page(url):
html = requests.get(url)
html.encoding = html.apparent_encoding
jishu = re.search('"zyp","playurls":(.*?)},',html.text,re.S)
# 标题
title = (re.search(r'《(.*?)》',html.text,re.S)).group(1)
print(title)
lists = eval(jishu.group(1))
dict_page = {i[0]:i[1].replace('\\','') for i in lists}
for i in dict_page.items():
print(i)
page = input('输入第几集:\n')
one_movie_url = dict_page[page.strip()]
save_path = title+'-'+page.strip()
print('*'*80)
print(save_path)
print('开始下载')
start = time.time()
get_ts(one_movie_url,save_path)
print('开始合成视频')
time.sleep(5)
join_ts(save_path)
print('用时: ',time.time()-start)
print('*'*80)
index_url = 'https://www.zhandi.cc/Gc/155082/play.html?155082-1-19'
get_page(index_url)
# url = 'https://cdn-yong.bejingyongjiu.com/share/7ffe08e7623a46843e73565ae1618f5b'