申请会员ID:amumu0406
1、申 请 I D:amumu04062、个人邮箱:55654624@qq.com
3、原创技术文章:B站视频下载简单版 python源码
B站视频下载 python源码
一、准备工作
安装 ffmpeg 记住安装的路径
把ffmpeg路径添加到path中
二、源码
```
from pprint import pprint
import requests
import re
import json
import os
import time
class BiliDown:
def __init__(self, bili_url):
self.bili_url = bili_url
self.title = str()
self.file_name = int(time.time())
def get_title(self):
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67',
'Referer': 'https://www.bilibili.com/'
}
r = requests.get(self.bili_url, headers=head)
self.title = re.findall('<title data-vue-meta="true">(.*?)</title>', r.text)
s = ['\n', ',', '。', ' ', '—', '”', '?', '“', '(', ')', '、', '|']
for i in s:
if i == '(' or i == ')':
self.title = self.title.replace(i, '_')
self.title = self.title.replace(i, '')
print(f'视频标题:"{self.title}"')
def audio_video_data(self):
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67',
'Referer': 'https://www.bilibili.com/'
}
r = requests.get(self.bili_url, headers=head)
# # 通过re获取包含视频和音频地址的json字段
json_data = re.findall('<script>window.__playinfo__=(.*?)</script>', r.text)
json_data = json.loads(json_data)
audio_url = json_data['data']['dash']['audio']['backupUrl']
print(f'已提取到音频地址:audio_url="{audio_url}"')
video_url = json_data['data']['dash']['video']['backupUrl']
video_url = json_data['data']['dash']['video']['baseUrl']
print(f'已提取到视频地址:video_url="{video_url}"')
r_audio = requests.get(url=audio_url, headers=head)
audio_data = r_audio.content
with open(f"{self.file_name}.mp3", mode='wb') as f:
f.write(audio_data)
print('音频下载成功')
r_video = requests.get(url=video_url, headers=head)
video_data = r_video.content
with open(f"{self.file_name}" + '.mp4', mode='wb') as f:
f.write(video_data)
print('视频下载完成')
def audio_video_add(self):
print('开始合成')
cmd = f' ffmpeg-i {self.file_name}.mp4 -i {self.file_name}.mp3 -acodec copy -vcodec copy C:\\Users\\Administrator\\Videos\\bili_{self.title}.mp4'#修改为自己的保存路径
# 调用cmd执行ffmpeg程序来合并音频和视频
os.system(cmd)
# 删除原来的音频和视频
os.remove(str(self.file_name) + '.mp4')
os.remove(str(self.file_name) + '.mp3')
print('合成结束')
if __name__ == "__main__":
while True:
value = input('输入该视频的有效链接地址(输入quit退出):')
if value == 'quit':
break
else:
url = value
bilidown = BiliDown(url)
bilidown.get_title()
bilidown.audio_video_data()
bilidown.audio_video_add()
```
抱歉,未能达到申请要求,申请不通过,可以关注论坛官方微信(吾爱破解论坛),等待开放注册通知。
页:
[1]