欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 批量下载bilibili视频的gui程序

程序员文章站 2022-04-01 18:59:58
运行效果:完整代码:# !/usr/bin/python# -*- coding:utf-8 -*-# time: 2019/07/02--08:12__author__ = 'henry''''项目...

运行效果:

python 批量下载bilibili视频的gui程序

完整代码:

# !/usr/bin/python
# -*- coding:utf-8 -*-
# time: 2019/07/02--08:12
__author__ = 'henry'


'''
项目: b站视频下载 - gui版本
版本1: 加密api版,不需要加入cookie,直接即可下载1080p视频
20190422 - 增加多p视频单独下载其中一集的功能
20190702 - 增加视频多线程下载 速度大幅提升
20190711 - 增加gui版本,可视化界面,操作更加友好
'''

import requests, time, hashlib, urllib.request, re, json
import imageio
imageio.plugins.ffmpeg.download()
from moviepy.editor import *
import os, sys, threading



from tkinter import *
from tkinter import ttk
from tkinter import stringvar
root=tk()
start_time = time.time()

# 将输出重定向到表格
def print(thetext):
  msgbox.insert(end,thetext+'\n')


# 访问api地址
def get_play_list(start_url, cid, quality):
  entropy = 'rbmckn@kuamxwlpmojgskcbijkufkpf_8dabscjntvqhrsetg'
  appkey, sec = ''.join([chr(ord(i) + 2) for i in entropy[::-1]]).split(':')
  params = 'appkey=%s&cid=%s&otype=json&qn=%s&quality=%s&type=' % (appkey, cid, quality, quality)
  chksum = hashlib.md5(bytes(params + sec, 'utf8')).hexdigest()
  url_api = 'https://interface.bilibili.com/v2/playurl?%s&sign=%s' % (params, chksum)
  headers = {
    'referer': start_url, # 注意加上referer
    'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/55.0.2883.87 safari/537.36'
  }
  # print(url_api)
  html = requests.get(url_api, headers=headers).json()
  # print(json.dumps(html))
  video_list = []
  for i in html['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

  # 设置下载进度条
  pervent = recv_size / totalsize
  percent_str = "%.2f%%" % (pervent * 100)
  download.coords(fill_line1,(0,0,pervent*465,23))
  root.update()
  pct.set(percent_str)



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

# 合并视频(20190802新版)
def combine_video(title_list):
  video_path = os.path.join(sys.path[0], 'bilibili_video') # 下载目录
  for title in title_list:
    current_video_path = os.path.join(video_path ,title)
    if len(os.listdir(current_video_path)) >= 2:
      # 视频大于一段才要合并
      print('[下载完成,正在合并视频...]:' + title)
      # 定义一个数组
      l = []
      # 遍历所有文件
      for file in sorted(os.listdir(current_video_path), key=lambda x: int(x[x.rindex("-") + 1:x.rindex(".")])):
        # 如果后缀名为 .mp4/.flv
        if os.path.splitext(file)[1] == '.flv':
          # 拼接成完整路径
          filepath = os.path.join(current_video_path, file)
          # 载入视频
          video = videofileclip(filepath)
          # 添加到数组
          l.append(video)
      # 拼接视频
      final_clip = concatenate_videoclips(l)
      # 生成目标视频文件
      final_clip.to_videofile(os.path.join(current_video_path, r'{}.mp4'.format(title)), fps=24, remove_temp=false)
      print('[视频合并完成]' + title)
    else:
      # 视频只有一段则直接打印下载完成
      print('[视频合并完成]:' + title)

def do_prepare(inputstart,inputquality):
  # 清空进度条
  download.coords(fill_line1,(0,0,0,23))
  pct.set('0.00%')
  root.update()
  # 清空文本栏
  msgbox.delete('1.0','end')
  start_time = time.time()
  # 用户输入av号或者视频链接地址
  print('*' * 30 + 'b站视频下载小助手' + '*' * 30)
  start = inputstart
  if start.isdigit() == true: # 如果输入的是av号
    # 获取cid的api, 传入aid即可
    start_url = 'https://api.bilibili.com/x/web-interface/view?aid=' + start
  else:
    # https://www.bilibili.com/video/av46958874/?spm_id_from=333.334.b_63686965665f7265636f6d6d656e64.16
    start_url = 'https://api.bilibili.com/x/web-interface/view?aid=' + re.search(r'/av(\d+)/*', start).group(1)

  # 视频质量
  # <accept_format><![cdata[flv,flv720,flv480,flv360]]></accept_format>
  # <accept_description><![cdata[高清 1080p,高清 720p,清晰 480p,流畅 360p]]></accept_description>
  # <accept_quality><![cdata[80,64,32,16]]></accept_quality>
  quality = inputquality
  # 获取视频的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)
  # 创建线程池
  threadpool = []
  title_list = []
  for item in cid_list:
    cid = str(item['cid'])
    title = item['part']
    title = re.sub(r'[\/\\:*?"<>|]', '', title) # 替换为空的
    print('[下载视频的cid]:' + cid)
    print('[下载视频的标题]:' + title)
    title_list.append(title)
    page = str(item['page'])
    start_url = start_url + "/?p=" + page
    video_list = get_play_list(start_url, cid, quality)
    start_time = time.time()
    # down_video(video_list, title, start_url, page)
    # 定义线程
    th = threading.thread(target=down_video, args=(video_list, title, start_url, page))
    # 将线程加入线程池
    threadpool.append(th)

  # 开始线程
  for th in threadpool:
    th.start()
  # 等待所有线程运行完毕
  for th in threadpool:
    th.join()
  
  # 最后合并视频
  combine_video(title_list)

  end_time = time.time() # 结束时间
  print('下载总耗时%.2f秒,约%.2f分钟' % (end_time - start_time, int(end_time - start_time) / 60))

  # 如果是windows系统,下载完成后打开下载目录
  currentvideopath = os.path.join(sys.path[0], 'bilibili_video') # 当前目录作为下载目录
  if (sys.platform.startswith('win')):
    os.startfile(currentvideopath)



def thread_it(func, *args):
  '''将函数打包进线程'''
  # 创建
  t = threading.thread(target=func, args=args) 
  # 守护 !!!
  t.setdaemon(true) 
  # 启动
  t.start()


if __name__ == "__main__":
  # 设置标题
  root.title('b站视频下载小助手-gui')
  # 设置ico
  root.iconbitmap('./pic/favicon.ico')
  # 设置logo
  photo = photoimage(file='./pic/logo.png')
  logo = label(root,image=photo)
  logo.pack()
  # 各项输入栏和选择框
  inputstart = entry(root,bd=4,width=600)
  labelstart=label(root,text="请输入您要下载的b站av号或者视频链接地址:") # 地址输入
  labelstart.pack(anchor="w")
  inputstart.pack()
  labelqual = label(root,text="请选择您要下载视频的清晰度") # 清晰度选择
  labelqual.pack(anchor="w")
  inputqual = ttk.combobox(root,state="readonly")
  # 可供选择的表
  inputqual['value']=('1080p','720p','480p','360p')
  # 对应的转换字典
  keytrans=dict()
  keytrans['1080p']='80'
  keytrans['720p']='64'
  keytrans['480p']='32'
  keytrans['360p']='16'
  # 初始值为720p
  inputqual.current(1)
  inputqual.pack()
  confirm = button(root,text="开始下载",command=lambda:thread_it(do_prepare,inputstart.get(), keytrans[inputqual.get()] ))
  msgbox = text(root)
  msgbox.insert('1.0',"对于单p视频:直接传入b站av号或者视频链接地址\n(eg: 49842011或者https://www.bilibili.com/video/av49842011)\n对于多p视频:\n1.下载全集:直接传入b站av号或者视频链接地址\n(eg: 49842011或者https://www.bilibili.com/video/av49842011)\n2.下载其中一集:传入那一集的视频链接地址\n(eg: https://www.bilibili.com/video/av19516333/?p=2)")
  msgbox.pack()
  download=canvas(root,width=465,height=23,bg="white")
  # 进度条的设置
  labeldownload=label(root,text="下载进度")
  labeldownload.pack(anchor="w")
  download.pack()
  fill_line1 = download.create_rectangle(0, 0, 0, 23, width=0, fill="green")
  pct=stringvar()
  pct.set('0.0%')
  pctlabel = label(root,textvariable=pct)
  pctlabel.pack()
  root.geometry("600x800")
  confirm.pack()
  # gui主循环
  root.mainloop()
  

以上就是python 批量下载bilibili视频的gui程序的详细内容,更多关于python 批量下载bilibili视频的资料请关注其它相关文章!