赞
踩
部分代码讲解:
url = 'https://www.bilibili.com/video/BV1L841137ST/'
'运行
进入目标网页后,我们只需提取?前的url
- headers = {
- 'Referer': 'https://www.bilibili.com/',
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
- }
'运行
Referer 防盗链:告诉服务器我们请求的url网址是从哪里跳转过来的
- response = requests.get(url=url, headers=headers)
- # print(response.text)
- title = re.findall('<h1 title="(.*?)"', response.text)[0]
- title = re.sub(r'[\/:*?"<>| ]', '', title)
- html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0]
- print(title)
- json_data = json.loads(html_data)
- print(html_data)
其中 print(html_data) 后,可以使用json解析器来分析,分别提取出视频和音频数据:
用 ffmpeg 来合并视频和音频:
- cmd = f"ffmpeg -i {title}.mp4 -i {title}.mp3 -c:v copy -c:a aac -strict experimental {title}output.mp4"
- subprocess.run(cmd, shell=True)
最后将原本分别爬取出的无声视频和音频删去 :
- os.remove(f'{title}.mp4')
- os.remove(f'{title}.mp3')
运行代码后,会看到一堆红色的代码,不要慌,那不是报错,而是 ffmpeg 在运行时产生的数据:
最后,可以在该.py的同级目录下看到合并后的视频:
完整代码:
- import requests
- import re
- import pprint
- import json
- import subprocess
- import os
-
- url = 'https://www.bilibili.com/video/BV1L841137ST/'
- headers = {
- 'Referer': 'https://www.bilibili.com/',
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
- }
- response = requests.get(url=url, headers=headers)
- # print(response.text)
- title = re.findall('<h1 title="(.*?)"', response.text)[0]
- title = re.sub(r'[\/:*?"<>| ]', '', title)
- html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0]
- print(title)
- json_data = json.loads(html_data)
- # print(html_data)
- # print(type(json_data))
-
- video_url = json_data['data']['dash']['video'][0]['baseUrl']
- audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
- # print(video_url)
- # print(audio_url)
-
- video_content = requests.get(url=video_url, headers=headers).content
- audio_content = requests.get(url=audio_url, headers=headers).content
-
- with open(title + '.mp4', mode='wb') as f:
- f.write(video_content)
- with open(title + '.mp3', mode='wb') as f:
- f.write(audio_content)
- print(title, '数据保存成功')
- cmd = f"ffmpeg -i {title}.mp4 -i {title}.mp3 -c:v copy -c:a aac -strict experimental {title}output.mp4"
- subprocess.run(cmd, shell=True)
-
- os.remove(f'{title}.mp4')
- os.remove(f'{title}.mp3')
- print(title, '视频合成完成')

赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。