B站视频下载源码

    技术2024-12-16  16

    最初源码

    # -*- coding: utf-8 -*- """ @author: tanderick """ import requests import re import os out_ffmpeg = os.getcwd() out_fliepath = os.getcwd()+'/download' av_name = input('视频av号:') base_url = 'https://www.bilibili.com/video/'+av_name if not os.path.exists(out_fliepath): os.mkdir(out_fliepath) base_headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q = 0.9' } download_headers = { 'referer': base_url, 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0' } base_response = requests.get(base_url,headers=base_headers) html = base_response.text video_name =re.sub(' ','',re.findall('<span class=".*?">(.*?)</span>',html,re.S)[0]) if not os.path.exists(out_fliepath+'/'+av_name): os.mkdir(out_fliepath+'/'+av_name) vid =[80,64,32,16] for i in range(4): download_url = re.findall('"id":{0},"baseUrl":".*?","base_url":"(.*?)","backupUrl"'.format(vid[i]),html,re.S) print(download_url,vid[i],i) if len(download_url) == 0: download_url = 0 else: break video_url = download_url[0] audio_url = re.findall('"id":30280,"baseUrl":".*?","base_url":"(.*?)","backupUrl"'.format(vid[i]),html,re.S)[0] vid = requests.get(video_url, headers=download_headers, stream=True, verify=False) aud = requests.get(audio_url, headers=download_headers, stream=True, verify=False) vid_path = out_fliepath+'/'+av_name+'/1.mp4' aud_path = out_fliepath+'/'+av_name+'/2.mp3' with open(vid_path, 'wb') as f: f.write(vid.content) with open(aud_path, 'wb') as f: f.write(aud.content) cmd1 =str('cd /d '+out_fliepath+'/'+av_name) cmd2 = str(out_ffmpeg+'/ffmpeg -i 1.mp4 -i 2.mp3 -codec copy '+video_name+'.mp4') os.system(cmd1+'&&'+cmd2) os.remove(vid_path) os.remove(aud_path)

    第一版改动

    # -*- coding: utf-8 -*- """ @author: tanderick """ import requests import re import os #import shutil out_ffmpeg = os.getcwd() out_fliepath = os.getcwd()+'/download' av_name = input('视频av号:') for x in range(1,int(input('总视频集数:'))+1): x = str(x) base_url = 'https://www.bilibili.com/video/'+av_name+'?p='+x if not os.path.exists(out_fliepath): os.mkdir(out_fliepath) base_headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q = 0.9' } download_headers = { 'referer': base_url, 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0' } base_response = requests.get(base_url,headers=base_headers) html = base_response.text video_name =re.sub(' ','',re.findall('<span class=".*?">(.*?)</span>',html,re.S)[0]) if not os.path.exists(out_fliepath+'/'+av_name+'-'+x): os.mkdir(out_fliepath+'/'+av_name+'-'+x) vid =[80,64,32,16] for i in range(4): download_url = re.findall('"id":{0},"baseUrl":".*?","base_url":"(.*?)","backupUrl"'.format(vid[i]),html,re.S) print(download_url,vid[i],i) if len(download_url) == 0: download_url = 0 else: break video_url = download_url[0] audio_url = re.findall('"id":30280,"baseUrl":".*?","base_url":"(.*?)","backupUrl"'.format(vid[i]),html,re.S)[0] vid = requests.get(video_url, headers=download_headers, stream=True, verify=False) aud = requests.get(audio_url, headers=download_headers, stream=True, verify=False) vid_path = out_fliepath+'/'+av_name+'-'+x+'/1.mp4' aud_path = out_fliepath+'/'+av_name+'-'+x+'/2.mp3' with open(vid_path, 'wb') as f: f.write(vid.content) with open(aud_path, 'wb') as f: f.write(aud.content) #shutil.copy2('ffmpeg.exe', out_fliepath+'/'+av_name+x+'/ffmpeg.exe') cmd1 =str('cd /d '+out_fliepath+'/'+av_name+'-'+x) cmd2 = str(out_ffmpeg+'ffmpeg -i 1.mp4 -i 2.mp3 -codec copy '+video_name+'-'+x+'p.mp4') os.system(cmd1+'&&'+cmd2) os.remove(vid_path) os.remove(aud_path)

    第二版改动

    # -*- coding: utf-8 -*- """ @author: tanderick """ import requests import re import os out_ffmpeg = os.getcwd() out_fliepath = os.getcwd()+'\download' with open(out_ffmpeg+'\download.txt','r',encoding = 'utf-8') as f: content = f.read() avname_list = content.split(',') for av_name in avname_list: base_url = 'https://www.bilibili.com/video/'+av_name if not os.path.exists(out_fliepath): os.mkdir(out_fliepath) base_headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q = 0.9' } download_headers = { 'referer': base_url, 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0' } base_response = requests.get(base_url,headers=base_headers) html = base_response.text video_name =re.sub(' ','',re.findall('<span class=".*?">(.*?)</span>',html,re.S)[0]) if not os.path.exists(out_fliepath+'/'+av_name): os.mkdir(out_fliepath+'/'+av_name) vid =[80,64,32,16] for i in range(4): download_url = re.findall('"id":{0},"baseUrl":".*?","base_url":"(.*?)","backupUrl"'.format(vid[i]),html,re.S) print(download_url,vid[i],i) if len(download_url) == 0: download_url = 0 else: break video_url = download_url[0] audio_url = re.findall('"id":30280,"baseUrl":".*?","base_url":"(.*?)","backupUrl"'.format(vid[i]),html,re.S)[0] vid = requests.get(video_url, headers=download_headers, stream=True, verify=False) aud = requests.get(audio_url, headers=download_headers, stream=True, verify=False) vid_path = out_fliepath+'/'+av_name+'/1.mp4' aud_path = out_fliepath+'/'+av_name+'/2.mp3' with open(vid_path, 'wb') as f: f.write(vid.content) with open(aud_path, 'wb') as f: f.write(aud.content) cmd1 =str('cd /d '+out_fliepath+'/'+av_name) cmd2 = str(out_ffmpeg+'/ffmpeg -i 1.mp4 -i 2.mp3 -codec copy '+video_name+'.mp4') os.system(cmd1+'&&'+cmd2) os.remove(vid_path) os.remove(aud_path)
    Processed: 0.023, SQL: 9