bilibili_video_download_v3.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @File : t.py
  5. @Time : 2019/07/18 04:54:35
  6. @Author : Liuyuqi
  7. @Version : 1.0
  8. @Contact : liuyuqi.gov@msn.cn
  9. @License : (C)Copyright 2019
  10. @Desc : 项目: B站视频下载 - 多线程下载
  11. 版本1: 加密API版,不需要加入cookie,直接即可下载1080p视频
  12. '''
  13. import requests, time, hashlib, urllib.request, re, json
  14. from moviepy.editor import *
  15. import os, sys, threading
  16. import imageio
  17. imageio.plugins.ffmpeg.download()
  18. # 访问API地址
  19. def get_play_list(start_url, cid, quality):
  20. entropy = 'rbMCKn@KuamXWlPMoJGsKcbiJKUfkPF_8dABscJntvqhRSETg'
  21. appkey, sec = ''.join([chr(ord(i) + 2) for i in entropy[::-1]]).split(':')
  22. params = 'appkey=%s&cid=%s&otype=json&qn=%s&quality=%s&type=' % (appkey, cid, quality, quality)
  23. chksum = hashlib.md5(bytes(params + sec, 'utf8')).hexdigest()
  24. url_api = 'https://interface.bilibili.com/v2/playurl?%s&sign=%s' % (params, chksum)
  25. headers = {
  26. 'Referer': start_url, # 注意加上referer
  27. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
  28. }
  29. # print(url_api)
  30. html = requests.get(url_api, headers=headers).json()
  31. # print(json.dumps(html))
  32. video_list = [html['durl'][0]['url']]
  33. # print(video_list)
  34. return video_list
  35. # 下载视频
  36. '''
  37. urllib.urlretrieve 的回调函数:
  38. def callbackfunc(blocknum, blocksize, totalsize):
  39. @blocknum: 已经下载的数据块
  40. @blocksize: 数据块的大小
  41. @totalsize: 远程文件的大小
  42. '''
  43. def Schedule_cmd(blocknum, blocksize, totalsize):
  44. speed = (blocknum * blocksize) / (time.time() - start_time)
  45. # speed_str = " Speed: %.2f" % speed
  46. speed_str = " Speed: %s" % format_size(speed)
  47. recv_size = blocknum * blocksize
  48. # 设置下载进度条
  49. f = sys.stdout
  50. pervent = recv_size / totalsize
  51. percent_str = "%.2f%%" % (pervent * 100)
  52. n = round(pervent * 50)
  53. s = ('#' * n).ljust(50, '-')
  54. f.write(percent_str.ljust(8, ' ') + '[' + s + ']' + speed_str)
  55. f.flush()
  56. # time.sleep(0.1)
  57. f.write('\r')
  58. def Schedule(blocknum, blocksize, totalsize):
  59. speed = (blocknum * blocksize) / (time.time() - start_time)
  60. # speed_str = " Speed: %.2f" % speed
  61. speed_str = " Speed: %s" % format_size(speed)
  62. recv_size = blocknum * blocksize
  63. # 设置下载进度条
  64. f = sys.stdout
  65. pervent = recv_size / totalsize
  66. percent_str = "%.2f%%" % (pervent * 100)
  67. n = round(pervent * 50)
  68. s = ('#' * n).ljust(50, '-')
  69. print(percent_str.ljust(6, ' ') + '-' + speed_str)
  70. f.flush()
  71. time.sleep(2)
  72. # print('\r')
  73. # 字节bytes转化K\M\G
  74. def format_size(bytes):
  75. try:
  76. bytes = float(bytes)
  77. kb = bytes / 1024
  78. except:
  79. print("传入的字节格式不对")
  80. return "Error"
  81. if kb >= 1024:
  82. M = kb / 1024
  83. if M >= 1024:
  84. G = M / 1024
  85. return "%.3fG" % (G)
  86. else:
  87. return "%.3fM" % (M)
  88. else:
  89. return "%.3fK" % (kb)
  90. # 下载视频
  91. def down_video(video_list, title, start_url, page):
  92. num = 1
  93. print('[正在下载P{}段视频,请稍等...]:'.format(page) + title)
  94. currentVideoPath = os.path.join(sys.path[0], 'bilibili_video', title) # 当前目录作为下载目录
  95. for i in video_list:
  96. opener = urllib.request.build_opener()
  97. # 请求头
  98. opener.addheaders = [
  99. # ('Host', 'upos-hz-mirrorks3.acgvideo.com'), #注意修改host,不用也行
  100. ('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0'),
  101. ('Accept', '*/*'),
  102. ('Accept-Language', 'en-US,en;q=0.5'),
  103. ('Accept-Encoding', 'gzip, deflate, br'),
  104. ('Range', 'bytes=0-'), # Range 的值要为 bytes=0- 才能下载完整视频
  105. ('Referer', start_url), # 注意修改referer,必须要加的!
  106. ('Origin', 'https://www.bilibili.com'),
  107. ('Connection', 'keep-alive'),
  108. ]
  109. urllib.request.install_opener(opener)
  110. # 创建文件夹存放下载的视频
  111. if not os.path.exists(currentVideoPath):
  112. os.makedirs(currentVideoPath)
  113. # 开始下载
  114. if len(video_list) > 1:
  115. urllib.request.urlretrieve(url=i, filename=os.path.join(currentVideoPath, r'{}-{}.flv'.format(title, num)),reporthook=Schedule_cmd) # 写成mp4也行 title + '-' + num + '.flv'
  116. else:
  117. urllib.request.urlretrieve(url=i, filename=os.path.join(currentVideoPath, r'{}.flv'.format(title)),reporthook=Schedule_cmd) # 写成mp4也行 title + '-' + num + '.flv'
  118. num += 1
  119. # 合并视频
  120. def combine_video(video_list, title):
  121. currentVideoPath = os.path.join(sys.path[0], 'bilibili_video', title) # 当前目录作为下载目录
  122. if len(video_list) >= 2:
  123. # 视频大于一段才要合并
  124. print('[下载完成,正在合并视频...]:' + title)
  125. # 定义一个数组
  126. L = []
  127. # 访问 video 文件夹 (假设视频都放在这里面)
  128. root_dir = currentVideoPath
  129. # 遍历所有文件
  130. for file in sorted(os.listdir(root_dir), key=lambda x: int(x[x.rindex("-") + 1:x.rindex(".")])):
  131. # 如果后缀名为 .mp4/.flv
  132. if os.path.splitext(file)[1] == '.flv':
  133. # 拼接成完整路径
  134. filePath = os.path.join(root_dir, file)
  135. # 载入视频
  136. video = VideoFileClip(filePath)
  137. # 添加到数组
  138. L.append(video)
  139. # 拼接视频
  140. final_clip = concatenate_videoclips(L)
  141. # 生成目标视频文件
  142. final_clip.to_videofile(os.path.join(root_dir, r'{}.mp4'.format(title)), fps=24, remove_temp=False)
  143. print('[视频合并完成]' + title)
  144. else:
  145. # 视频只有一段则直接打印下载完成
  146. print('[视频合并完成]:' + title)
  147. if __name__ == '__main__':
  148. start_time = time.time()
  149. # 用户输入av号或者视频链接地址
  150. print('*' * 30 + 'B站视频下载小助手' + '*' * 30)
  151. start = input('请输入您要下载的B站av号或者视频链接地址:')
  152. if start.isdigit() == True: # 如果输入的是av号
  153. # 获取cid的api, 传入aid即可
  154. start_url = 'https://api.bilibili.com/x/web-interface/view?aid=' + start
  155. else:
  156. # https://www.bilibili.com/video/av46958874/?spm_id_from=333.334.b_63686965665f7265636f6d6d656e64.16
  157. start_url = 'https://api.bilibili.com/x/web-interface/view?aid=' + re.search(r'/av(\d+)/*', start).group(1)
  158. # 视频质量
  159. # <accept_format><![CDATA[flv,flv720,flv480,flv360]]></accept_format>
  160. # <accept_description><![CDATA[高清 1080P,高清 720P,清晰 480P,流畅 360P]]></accept_description>
  161. # <accept_quality><![CDATA[80,64,32,16]]></accept_quality>
  162. quality = input('请输入您要下载视频的清晰度(1080p:80;720p:64;480p:32;360p:16)(填写80或64或32或16):')
  163. # 获取视频的cid,title
  164. headers = {
  165. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
  166. }
  167. html = requests.get(start_url, headers=headers).json()
  168. data = html['data']
  169. video_title=data["title"].replace(" ","_")
  170. cid_list = []
  171. if '?p=' in start:
  172. # 单独下载分P视频中的一集
  173. p = re.search(r'\?p=(\d+)',start).group(1)
  174. cid_list.append(data['pages'][int(p) - 1])
  175. else:
  176. # 如果p不存在就是全集下载
  177. cid_list = data['pages']
  178. # print(cid_list)
  179. # 创建线程池
  180. threadpool = []
  181. for item in cid_list:
  182. cid = str(item['cid'])
  183. title = item['part']
  184. if not title:
  185. title = video_title
  186. title = re.sub(r'[\/\\:*?"<>|]', '', title) # 替换为空的
  187. print('[下载视频的cid]:' + cid)
  188. print('[下载视频的标题]:' + title)
  189. page = str(item['page'])
  190. start_url = start_url + "/?p=" + page
  191. video_list = get_play_list(start_url, cid, quality)
  192. start_time = time.time()
  193. # down_video(video_list, title, start_url, page)
  194. # 定义线程
  195. th = threading.Thread(target=down_video, args=(video_list, title, start_url, page))
  196. # 将线程加入线程池
  197. threadpool.append(th)
  198. combine_video(video_list, title)
  199. # 开始线程
  200. for th in threadpool:
  201. th.start()
  202. # 等待所有线程运行完毕
  203. for th in threadpool:
  204. th.join()
  205. end_time = time.time() # 结束时间
  206. print('下载总耗时%.2f秒,约%.2f分钟' % (end_time - start_time, int(end_time - start_time) / 60))
  207. # 如果是windows系统,下载完成后打开下载目录
  208. currentVideoPath = os.path.join(sys.path[0], 'bilibili_video') # 当前目录作为下载目录
  209. if (sys.platform.startswith('win')):
  210. os.startfile(currentVideoPath)
  211. # 分P视频下载测试: https://www.bilibili.com/video/av19516333/
  212. # 下载总耗时14.21秒,约0.23分钟