1 Star 2 Fork 1

无极/Bilibili_video_download

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bilibili_video_download_v2.py 8.77 KB
一键复制 编辑 原始数据 按行查看 历史
Henryhaohao 提交于 2019-04-22 10:03 . Bilibili_video_download:star:
# !/usr/bin/python
# -*- coding:utf-8 -*-
# time: 2019/04/16--17:12
__author__ = 'Henry'
'''
项目: B站视频下载
版本2: 无加密API版,但是需要加入登录后cookie中的SESSDATA字段,才可下载720p及以上视频
API:
1.获取cid的api为 https://api.bilibili.com/x/web-interface/view?aid=47476691 aid后面为av号
2.下载链接api为 https://api.bilibili.com/x/player/playurl?avid=44743619&cid=78328965&qn=32 cid为上面获取到的 avid为输入的av号 qn为视频质量
注意:
但是此接口headers需要加上登录后'Cookie': 'SESSDATA=3c5d20cf%2C1556704080%2C7dcd8c41' (30天的有效期)(因为现在只有登录后才能看到720P以上视频了)
不然下载之后都是最低清晰度,哪怕选择了80也是只有480p的分辨率!!
20190422 - 增加多P视频单独下载其中一集的功能
'''
import requests, time, urllib.request, re
from moviepy.editor import *
import os, sys
# 访问API地址
def get_play_list(aid, cid, quality):
url_api = 'https://api.bilibili.com/x/player/playurl?cid={}&avid={}&qn={}'.format(cid, aid, quality)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'Cookie': 'SESSDATA=b693f4f2%2C1558063359%2Cfc41fa41', # 登录B站后复制一下cookie中的SESSDATA字段,有效期1个月
'Host': 'api.bilibili.com'
}
html = requests.get(url_api, headers=headers).json()
video_list = []
for i in html['data']['durl']:
video_list.append(i['url'])
print(video_list)
return video_list
# 下载视频
'''
urllib.urlretrieve 的回调函数:
def callbackfunc(blocknum, blocksize, totalsize):
@blocknum: 已经下载的数据块
@blocksize: 数据块的大小
@totalsize: 远程文件的大小
'''
def Schedule_cmd(blocknum, blocksize, totalsize):
speed = (blocknum * blocksize) / (time.time() - start_time)
# speed_str = " Speed: %.2f" % speed
speed_str = " Speed: %s" % format_size(speed)
recv_size = blocknum * blocksize
# 设置下载进度条
f = sys.stdout
pervent = recv_size / totalsize
percent_str = "%.2f%%" % (pervent * 100)
n = round(pervent * 50)
s = ('#' * n).ljust(50, '-')
f.write(percent_str.ljust(8, ' ') + '[' + s + ']' + speed_str)
f.flush()
# time.sleep(0.1)
f.write('\r')
def Schedule(blocknum, blocksize, totalsize):
speed = (blocknum * blocksize) / (time.time() - start_time)
# speed_str = " Speed: %.2f" % speed
speed_str = " Speed: %s" % format_size(speed)
recv_size = blocknum * blocksize
# 设置下载进度条
f = sys.stdout
pervent = recv_size / totalsize
percent_str = "%.2f%%" % (pervent * 100)
n = round(pervent * 50)
s = ('#' * n).ljust(50, '-')
print(percent_str.ljust(6, ' ') + '-' + speed_str)
f.flush()
time.sleep(2)
# print('\r')
# 字节bytes转化K\M\G
def format_size(bytes):
try:
bytes = float(bytes)
kb = bytes / 1024
except:
print("传入的字节格式不对")
return "Error"
if kb >= 1024:
M = kb / 1024
if M >= 1024:
G = M / 1024
return "%.3fG" % (G)
else:
return "%.3fM" % (M)
else:
return "%.3fK" % (kb)
# 下载视频
def down_video(video_list, title, start_url, page):
num = 1
print('[正在下载P{}段视频,请稍等...]:'.format(page) + title)
currentVideoPath = os.path.join(sys.path[0], 'bilibili_video', title) # 当前目录作为下载目录
for i in video_list:
opener = urllib.request.build_opener()
# 请求头
opener.addheaders = [
# ('Host', 'upos-hz-mirrorks3.acgvideo.com'), #注意修改host,不用也行
('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0'),
('Accept', '*/*'),
('Accept-Language', 'en-US,en;q=0.5'),
('Accept-Encoding', 'gzip, deflate, br'),
('Range', 'bytes=0-'), # Range 的值要为 bytes=0- 才能下载完整视频
('Referer', start_url), # 注意修改referer,必须要加的!
('Origin', 'https://www.bilibili.com'),
('Connection', 'keep-alive'),
]
urllib.request.install_opener(opener)
# 创建文件夹存放下载的视频
if not os.path.exists(currentVideoPath):
os.makedirs(currentVideoPath)
# 开始下载
if len(video_list) > 1:
urllib.request.urlretrieve(url=i, filename=os.path.join(currentVideoPath, r'{}-{}.flv'.format(title, num)),
reporthook=Schedule_cmd) # 写成mp4也行 title + '-' + num + '.flv'
else:
urllib.request.urlretrieve(url=i, filename=os.path.join(currentVideoPath, r'{}.flv'.format(title)),
reporthook=Schedule_cmd) # 写成mp4也行 title + '-' + num + '.flv'
num += 1
# 合并视频
def combine_video(video_list, title):
currentVideoPath = os.path.join(sys.path[0], 'bilibili_video', title) # 当前目录作为下载目录
if len(video_list) >= 2:
# 视频大于一段才要合并
print('[下载完成,正在合并视频...]:' + title)
# 定义一个数组
L = []
# 访问 video 文件夹 (假设视频都放在这里面)
root_dir = currentVideoPath
# 遍历所有文件
for file in sorted(os.listdir(root_dir), key=lambda x: int(x[x.rindex("-") + 1:x.rindex(".")])):
# 如果后缀名为 .mp4/.flv
if os.path.splitext(file)[1] == '.flv':
# 拼接成完整路径
filePath = os.path.join(root_dir, file)
# 载入视频
video = VideoFileClip(filePath)
# 添加到数组
L.append(video)
# 拼接视频
final_clip = concatenate_videoclips(L)
# 生成目标视频文件
final_clip.to_videofile(os.path.join(root_dir, r'{}.mp4'.format(title)), fps=24, remove_temp=False)
print('[视频合并完成]' + title)
else:
# 视频只有一段则直接打印下载完成
print('[视频合并完成]:' + title)
if __name__ == '__main__':
# 用户输入av号或者视频链接地址
print('*' * 30 + 'B站视频下载小助手' + '*' * 30)
start = input('请输入您要下载的B站av号或者视频链接地址:')
if start.isdigit() == True:
# 如果输入的是av号
# 获取cid的api, 传入aid即可
aid = start
start_url = 'https://api.bilibili.com/x/web-interface/view?aid=' + aid
else:
# 如果输入的是url (eg: https://www.bilibili.com/video/av46958874/)
aid = re.search(r'/av(\d+)/*', start).group(1)
start_url = 'https://api.bilibili.com/x/web-interface/view?aid=' + aid
# qn参数就是视频清晰度
# 可选值:
# 112: 高清1080P+ (hdflv2) (需要带入大会员的cookie中的SESSDATA才行,普通用户的SESSDATA最多只能下载1080p的视频)
# 80: 高清1080P (flv)
# 64: 高清720P (flv720)
# 32: 清晰480P (flv480)
# 16: 流畅360P (flv360)
print('请输入您要下载视频的清晰度(1080p+:112;1080p:80;720p:64;480p:32;360p:16; **注意:1080p+需要带入大会员的cookie中的SESSDATA才行,普通用户的SESSDATA最多只能下载1080p的视频):')
quality = input('请填写112或80或64或32或16:')
# 获取视频的cid,title
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}
html = requests.get(start_url, headers=headers).json()
data = html['data']
cid_list = []
if '?p=' in start:
# 单独下载分P视频中的一集
p = re.search(r'\?p=(\d+)', start).group(1)
cid_list.append(data['pages'][int(p) - 1])
else:
# 如果p不存在就是全集下载
cid_list = data['pages']
# print(cid_list)
for item in cid_list:
cid = str(item['cid'])
title = item['part']
title = re.sub(r'[\/\\:*?"<>|]', '', title) # 替换为空的
print('[下载视频的cid]:' + cid)
print('[下载视频的标题]:' + title)
page = str(item['page'])
start_url = start_url + "/?p=" + page
video_list = get_play_list(aid, cid, quality)
start_time = time.time()
down_video(video_list, title, start_url, page)
combine_video(video_list, title)
# 如果是windows系统,下载完成后打开下载目录
currentVideoPath = os.path.join(sys.path[0], 'bilibili_video') # 当前目录作为下载目录
if (sys.platform.startswith('win')):
os.startfile(currentVideoPath)
# 分P视频下载测试: https://www.bilibili.com/video/av19516333/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/WuJizhu/Bilibili_video_download.git
git@gitee.com:WuJizhu/Bilibili_video_download.git
WuJizhu
Bilibili_video_download
Bilibili_video_download
master

搜索帮助