python 을 이용 하여 bilibili 비디오 를 다운로드 합 니 다.

실행 효과:

전체 코드

# !/usr/bin/python
# -*- coding:utf-8 -*-
# time: 2019/07/21--20:12
__author__ = 'Henry'


'''
  : B     (bangumi)  
  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    !!
'''

import requests, time, urllib.request, re
from moviepy.editor import *
import os, sys, threading, json

import imageio

#   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=13bd2abb%2C1619949439%2C2815d*b1', #   B      cookie  SESSDATA  ,   1  
    'Host': 'api.bilibili.com'
  }
  html = requests.get(url_api, headers=headers).json()
  # print(html)
  #         ,  cookie          SESSDATA     : {'code': -404, 'message': '    ', 'ttl': 1, 'data': None}
  if html['code'] != 0:
    print('  !     B      ,    ,Cookie        SESSDATA')
    return 'NoVIP'
  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('[     {}   ,   ...]:'.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)


if __name__ == '__main__':
  start_time = time.time()
  #             
  # 1. https://www.bilibili.com/bangumi/play/ep267692 (  ep  )
  # 2. https://www.bilibili.com/bangumi/play/ss26878 (     ss  ,epinfo aid   '-1')
  print('*' * 30 + 'B          ' + '*' * 30)
  print('[  ]: 1.      720P60,1080p+,1080p60     ,  35     SESSDATA            SESSDATA,     SESSDATA      1080p   ')
  print('    2.           720p  ,  35     SESSDATA         SESSDATA(      ),    SESSDATA     480p   ')

  start = input('        B          (  :https://www.bilibili.com/bangumi/play/ep267692):')
  ep_url = start
  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(ep_url,headers=headers).text
  ep_info = re.search(r'INITIAL_STATE__=(.*?"]});', html).group(1)
  # print(ep_info)
  ep_info = json.loads(ep_info)
  print(ep_info)
  # print('          :' + ep_info['mediaInfo']['title']) #          
  y = input('   1 2 - 1.        2.        :')
  # 1.       ep
  id_list = []
  if y == '1':
    try:
      id_list.append([ep_info['videoData']['aid'], ep_info['videoData']['cid'],
              ep_info['videoData']['title'] + ' ' + ep_info['videoData']['title']])
    except:
      id_list.append([ep_info['videoData']['aid'], ep_info['videoData']['cid'],
              ' ' + str(ep_info['videoData']['index']) + '  ' + ep_info['videoData']['index_title']])
  # 2.       ep
  else:
    for i in ep_info['epList']:
      # if i['badge'] == '': #  badge   '  ' ,    404
      try:
        id_list.append([i['aid'], i['cid'],
                i['titleFormat'] + ' ' + i['title']])
      except:
        id_list.append([i['aid'], i['cid'],' ' + str(i['index']) + '  ' + i['index_title']])

  # qn         
  #    :
  # 116:   1080P60 (        cookie  SESSDATA  ,     SESSDATA      1080p   ,   SESSDATA     480p )
  # 112:   1080P+ (hdflv2) (     )
  # 80:   1080P (flv)
  # 74:   720P60 (     )
  # 64:   720P (flv720)
  # 32:   480P (flv480)
  # 16:   360P (flv360)
  print('             (1080p60:116;1080p+:112;1080p:80;720p60:74;720p:64;480p:32;360p:16; **  :1080p+,1080p60,720p60         cookie  SESSDATA  ,     SESSDATA      1080p   ):')
  quality = input('   116 112 80 74 64 32 16:')
  threadpool = []
  title_list = []
  page = 1
  print(id_list)
  for item in id_list:
    aid = str(item[0])
    cid = str(item[1])
    title = item[2]
    title = re.sub(r'[\/\\:*?"<>|]', '', title) #      
    print('[      ]:' + title)
    title_list.append(title)
    start_url = ep_url
    video_list = get_play_list(aid, cid, quality)
    start_time = time.time()
    # down_video(video_list, title, start_url, page)
    #     
    if video_list != 'NoVIP':
      th = threading.Thread(target=down_video, args=(video_list, title, start_url, page))
      #         
      threadpool.append(th)
    page += 1

  #     
  for th in threadpool:
    th.start()
  #           
  for th in threadpool:
    th.join()
  
  #       
  print(title_list)
  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)
이상 은 python 을 이용 하여 bilibili 동 영상 을 다운로드 하 는 상세 한 내용 입 니 다.python 에서 bilibili 동 영상 을 다운로드 하 는 데 관 한 자 료 는 저희 의 다른 관련 글 을 주목 해 주 십시오!

좋은 웹페이지 즐겨찾기