本帖最后由 小羊要吃狼 于 2023-4-21 11:38 编辑
点击这个获取视频分享链接,到脚本中粘贴URL
回车就可以自动保存mp4和mp3到本地路径
代码写了详细的注释,适合新手入门
[Python] 纯文本查看 复制代码
# 确定请求网址
url = 'https://www.bilibili.com/'
# 添加headers请求头,对于py代码进行伪装
headers = {
'referer': 'https://www.bilibili.com/',
'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36"
}
response = requests.get(url=url, headers=headers)
title = re.findall('<h1 title="(.*?)"', response.text)[0]
re.sub(r'[\/:*?"<>|]', '', title)
html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0]
pprint.pprint(html_data)
print(type(html_data))
json_data = json.loads(html_data)
print(type(json_data))
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
print(audio_url)
vod_url = json_data['data']['dash']['video'][0]['baseUrl']
print(vod_url)
audio_content = requests.get(url=audio_url, headers=headers).content
vod_content = requests.get(url=vod_url, headers=headers).content
with open(title + '.mp3', mode='wb') as f:
f.write(audio_content)
with open(title + '.mp4', mode='wb') as f:
f.write(vod_content)
print(title + ",视频下载成功!")
# 合并
command = f"ffmpeg -i {title}.mp4 -i {title}.mp3 -c:v copy -c:a aac -strict experimental {title}_final.mp4"
subprocess.run(command, shell=True)
command2 = f"rm -rf {title}.mp4"
subprocess.run(command2, shell=True)
command3 = f"rm -rf {title}.mp3"
subprocess.run(command3, shell=True)
print(title + ", 视频合并完成!")
|