1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import requests import re import json import time from moviepy.editor import * import PySimpleGUI as sg
def get_file_info(url): headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36", "referer": "https://www.bilibili.com" }
resp = requests.get(url, headers=headers) palyinfo = re.findall(r'<script>window.__playinfo__=(.*?)</script>', resp.text)[0] palyinfo_data = json.loads(palyinfo)
title = re.findall(r'<h1 title="(.*?)" class="video-title tit">', resp.text)[0]
video_url = palyinfo_data['data']['dash']['video'][2]['base_url'] audio_url = palyinfo_data['data']['dash']['audio'][2]['base_url']
return title, video_url, audio_url
def down_file(title, file_url, file_type): headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36", "referer": "https://www.bilibili.com" } resp = requests.get(url=file_url, headers=headers) print(resp.status_code)
print(f'文件名称:{title}') chunk_size = 1024 file_size = int(resp.headers['content-length']) done_size = 0 file_size_MB = file_size / 1024 / 1024 print(f'文件大小:{file_size_MB:0.2f} MB') start_time = time.time() with open(title + '.' + file_type, mode='wb') as f: for chunk in resp.iter_content(chunk_size=chunk_size): f.write(chunk) done_size += len(chunk) print(f'\r下载进度:{done_size / file_size * 100:0.2f}%', end='') end_time = time.time() cost_time = end_time - start_time print(f'累计耗时:{cost_time:0.2f} 秒') print(f'下载速度:{file_size_MB / cost_time:0.2f}M/s')
def merge(title): video_path = title + '.mp4' audio_path = title + '.mp3' audio = AudioFileClip(audio_path) video = VideoFileClip(video_path) video = video.set_audio(audio) video.write_videofile(f"{title}(含音频).mp4")
sg.theme('SystemDefaultForReal')
layout = [[sg.Text('选择B站视频地址:', font=("微软雅黑", 12)), sg.InputText(key='url', size=(50, 1), font=("微软雅黑", 10), enable_events=True)], [sg.Button('开始下载', font=("微软雅黑", 10), button_color='Orange'), sg.Button('关闭程序', font=("微软雅黑", 10), button_color='red'), ] ]
window = sg.Window('Ray', layout, font=("微软雅黑", 12), default_element_size=(50, 1))
while True: event, values = window.read() if event in (None, '关闭程序'): break if event == '开始下载': url = values['url'] print('获取视频信息') title, video_url, audio_url = get_file_info(url) print('下载视频资源') down_file(title, video_url, 'mp4') print('下载音频资源') down_file(title, audio_url, 'mp3') print('合并视频与音频') merge(title) print('处理完成!!!') window.close()
|