Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import re
|
3 |
+
import json
|
4 |
+
|
5 |
+
def get_response(html_url):
|
6 |
+
headers = {
|
7 |
+
"referer": "https://www.bilibili.com/",
|
8 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
|
9 |
+
}
|
10 |
+
response = requests.get(html_url, headers=headers)
|
11 |
+
return response
|
12 |
+
|
13 |
+
def get_video_info(html_url):
|
14 |
+
response = get_response(html_url)
|
15 |
+
html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0]
|
16 |
+
json_data = json.loads(html_data)
|
17 |
+
if json_data['data']['dash']['audio'][0]['backupUrl']!=None:
|
18 |
+
audio_url = json_data['data']['dash']['audio'][0]['backupUrl'][0]
|
19 |
+
else:
|
20 |
+
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
|
21 |
+
video_url = json_data['data']['dash']['video'][0]['baseUrl']
|
22 |
+
return audio_url, video_url
|
23 |
+
|
24 |
+
def save_audio(title, html_url):
|
25 |
+
audio_url = get_video_info(html_url)[0]
|
26 |
+
video_url = get_video_info(html_url)[1]
|
27 |
+
|
28 |
+
audio_content = get_response(audio_url).content
|
29 |
+
video_content = get_response(video_url).content
|
30 |
+
|
31 |
+
with open(title + '.mp3', mode='wb') as f:
|
32 |
+
f.write(audio_content)
|
33 |
+
print("音乐内容保存完成")
|
34 |
+
with open(title + '.mp4', mode='wb') as f:
|
35 |
+
f.write(video_content)
|
36 |
+
print("视频内容保存完成")
|
37 |
+
return f"{title}.mp4"
|
38 |
+
|
39 |
+
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
inp1 = gr.Textbox(label="歌曲名", lines=1)
|
42 |
+
inp2 = gr.Textbox(label="B站视频对应的链接", placeholder="https://www.bilibili.com/video/BV1Wj421U7B4/", lines=1)
|
43 |
+
btn = gr.Button("一键下载视频")
|
44 |
+
out1 = gr.Video(label="为您下载的B站视频")
|
45 |
+
btn.click(save_audio, [inp1, inp2], out1)
|
46 |
+
demo.launch(debug=True)
|
47 |
+
|