一: 运行代码需要的安装的模块:
import aiohttp
import requests
from lxml import etree
import re
import pyperclip
import asyncio
import aiofiles
import os
import os.path
二:运行的代码需要安装的外置工具:
? ? ?(1):cmd命令音频视频合成工具:ffmpeg
? ? ? ? ? 下载网址:?Download FFmpeghttp://www.ffmpeg.org/download.html? ? (2):解压到某个目录,并添加到环境变量中
? ? ?示例:
? ? ? ? ? 解压到D盘根目录:
? ? ? ? ? ? ? ?D:/ffmpeg
? ? ? ? ? 则需要添加环境变量:D:/ffmpeg/bin
三:需要在代码所在目录创建的文件夹:
? ??(1):?bilibili视频
? ? (2):在"bilibi视频"这个文件夹下载创建下面两个文件夹:
? ? ? ? ? ? up视频
? ? ? ? ? ? 电视剧视频
四:代码
import aiohttp
import requests
from lxml import etree
import re
import pyperclip
import asyncio
import aiofiles
import os
import os.path
def conbine(title_name, Class):
title_new = title_name + 'c'
if Class == 3:
os.system(f'ffmpeg -i {title_new}.mp4 -i {title_new}.mp3 -c copy bilibili视频/电视剧视频/{title_name}.mp4')
elif Class == 1 or 2:
os.system(f'ffmpeg -i {title_new}.mp4 -i {title_new}.mp3 -c copy bilibili视频/up视频/{title_name}.mp4')
def delete(title_name):
title_new = title_name + 'c'
os.remove(title_new + '.mp4')
os.remove(title_new + '.mp3')
async def download(i, url, title_name, headers2):
title_new = title_name + 'c'
async with aiohttp.ClientSession(headers=headers2) as session:
async with session.get(url) as resp:
data = await resp.content.read()
if i == 0:
filename = f'./{title_new}.mp4'
elif i == 1:
filename = f'./{title_new}.mp3'
async with aiofiles.open(filename, mode='wb') as f:
await f.write(data)
async def asyncmake(video_url, audio_url, title_name, headers2):
urls = [video_url, audio_url]
tasks = []
for i in range(0, 2):
d = asyncio.create_task(download(i, urls[i], title_name, headers2))
tasks.append(d)
await asyncio.wait(tasks)
if __name__ == "__main__":
url = str(pyperclip.paste())
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
"cookie": "_uuid=1A1B677B-6311-4E06-9BDA-D06081D8740275362infoc; buvid3=6E8BD183-ADF3-4D9B-8714-C157F73620CC34773infoc; sid=ab95r294; fingerprint=8f4236095f9ce06ad4c94e22f8be2c3a; buvid_fp=6E8BD183-ADF3-4D9B-8714-C157F73620CC34773infoc; buvid_fp_plain=1F0F89A8-01C5-4175-8F28-7E203E65FE7334767infoc; DedeUserID=306269459; DedeUserID__ckMd5=a7388eef56c50e08; SESSDATA=2833875d%2C1638683753%2C8d0ed*61; bili_jct=c6f37eedd1ca8195a83acf9076d81243; CURRENT_FNVAL=80; blackside_state=1; rpdid=|(k|)Yk|)|~l0J'uYkJuJu~Yu; CURRENT_QUALITY=80; LIVE_BUVID=AUTO8916234196752032; PVID=3; bp_video_offset_306269459=537685465433229330",
"Referer": "https://www.bilibili.com/video/BV1jV411n7qa?p=4"
}
response = requests.get(url, headers=headers)
data = response.text
html_obj = etree.HTML(data)
print("'1':表示bilibili up主视频\n'2':表示up主选集视频\n'3':表示电视剧类型")
Class = int(input("请输入下载类型:"))
if Class == 1:
title_name = html_obj.xpath('//*[@id="viewbox_report"]/h1/span/text()')[0]
url_str = html_obj.xpath('/html/head/script[5]/text()')[0]
video_url = re.findall(r'video":\[{"id":\d+,"baseUrl":"(.*?)"', url_str)[0]
audio_url = re.findall(r'audio":\[{"id":\d+,"baseUrl":"(.*?)"', url_str)[0]
print(title_name + " 正在下载中!!!")
elif Class == 2:
title_name1 = html_obj.xpath('//*[@id="viewbox_report"]/h1/span/text()')[0]
title_name2 = url.split('?')[1]
title_name = title_name1 + title_name2
url_str = html_obj.xpath('/html/head/script[5]/text()')[0]
video_url = re.findall(r'video":\[{"id":\d+,"baseUrl":"(.*?)"', url_str)[0]
audio_url = re.findall(r'audio":\[{"id":\d+,"baseUrl":"(.*?)"', url_str)[0]
print(title_name + " 正在下载中!!!")
elif Class == 3:
title_name = html_obj.xpath("/html/head/title/text()")[0]
title_name = title_name.split('_')[0]
print(title_name + " 正在下载中!!!")
url_str = html_obj.xpath('/html/body/script[3]/text()')[0]
video_url = \
re.findall(r'video":\[{"start_with_sap":\d+,"bandwidth":\d+,"sar":"\d+:\d+","backupUrl":\["(.*?)"',
url_str)[0]
audio_url = \
re.findall(r'audio":\[{"start_with_sap":\d+,"bandwidth":\d+,"sar":"","backupUrl":\["(.*?)"', url_str)[0]
# 发送请求获取响应
headers2 = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
"Referer": url
}
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncmake(video_url, audio_url, title_name, headers2))
conbine(title_name, Class)
delete(title_name)
print("下载完成!!!")
五:使用方法步骤:
? ? ?(1):打开bilibili想要下载的视频,复制视频连接:
? ? ? ? ? 如下图红框内链接:
? ? ? ? ? ? ? ? ? ? ?
? ? ?(2):运行代码:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? 输入下载类型:
? ? ? ? ?1:表示bilibili up主视频
? ? ? ? ?2:表示up主选集视频,类似于各种教程选集视频
? ? ? ? ?3:表示电视剧,动漫视频,类似名侦探柯南,下载哪集复制哪集的链接 (vip视频暂时下载不? ? ? ? ? ? ? ? 来,有更厉害的大佬能下载的话欢迎留言).
? 六:运行代码,视频下载成功:
? ? ? ? ? ? ? ? ? ??
?
?七:查看视频:
? ? ? ? 视频下载到了创建的up视频文件夹里了:
? ? ? ? ? ? ? ? ? ? ? ??
使用视频播放器进行播放就可以观看了!!!
八:观看视频:?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?有大佬建议欢迎留言哦!!!
?
? ??
|