import json
import os.path
import pprint
import re
import json
import threading
import requests
from queue import Queue
from threading import Thread
q = Queue()
url = ''
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
def get_links(url):
#获取视频页的网页源代码
result = requests.get(url,headers=headers)
info = re.findall('window.pageInfo = window.videoInfo = (.*?)window.videoResource', result.text,re.DOTALL)[0].strip()[:-1]
m3u8_url = json.loads(json.loads(info)['currentVideoInfo']['ksPlayJson'])['adaptationSet'][0]['representation'][1]['url']
title = json.loads(info)['title']
m3u8_list = requests.get(m3u8_url,headers=headers).text
ts_files = re.sub('#.*', '', m3u8_list).split()
#https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/
for index,ts in enumerate(ts_files):
ts_url = 'https://ali-safety-video.acfun.cn/mediacloud/acfun/acfun_video/'+ts
q.put([ts_url,index])
# print(ts_files)
return title,len(ts_files)
#提取m3u8地址
def download(title):
while not q.empty():
url, index = q.get()
video_cantent = requests.get(url,headers=headers).content
with open(f'{title}_{index}.ts','wb') as f:
f.write(video_cantent)
print(f'{threading.current_thread().name}已下载...第{index}个视频')
def combine(title,length):
with open(f'{title}.mp4','ab') as fp:
for i in range(length):
if os.path.exists(f'{title}_{i}.ts'):
with open(f'{title}_{i}.ts','rb') as f:
content = f.read()
fp.write(content)
print(f'已合并第{i}个片段')
os.remove(f'{title}_{i}.ts')
print(f'已删除第{i}个片段')
def main():
indexurl = 'https://www.acfun.cn/v/ac40380599'
title,length = get_links(indexurl)
tasks = []
for i in range(3):
th = Thread(target=download,args=(title,), name=f'线程{i}')
th.start()
tasks.append(th)
for t in tasks:
t.join()
combine(title,length)
if __name__ == '__main__':
main()
acfun.cn视频根据观看地址下载视频
最新推荐文章于 2024-10-15 12:45:15 发布
336

被折叠的 条评论
为什么被折叠?



