File size: 1,799 Bytes
601bd10
 
 
6fa1f12
601bd10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d3a1b1
 
601bd10
9d3a1b1
 
601bd10
3439402
 
 
 
 
 
 
601bd10
 
 
6fa1f12
 
8709daf
6fa1f12
309a0c0
3676c5f
601bd10
 
 
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
import requests
import re
import json
import gradio as gr

def get_response(html_url):
  headers = {
      "referer": "https://www.bilibili.com/",
      "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"
  }
  response = requests.get(html_url, headers=headers)
  return response

def get_video_info(html_url):
  response = get_response(html_url)
  html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0]
  json_data = json.loads(html_data)
  if json_data['data']['dash']['audio'][0]['backupUrl']!=None:
    audio_url = json_data['data']['dash']['audio'][0]['backupUrl'][0]
  else:
    audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
  video_url = json_data['data']['dash']['video'][0]['baseUrl']
  return audio_url, video_url

def save_audio(title, html_url):
  audio_url = get_video_info(html_url)[0]
  #video_url = get_video_info(html_url)[1]

  audio_content = get_response(audio_url).content
  #video_content = get_response(video_url).content

  with open(title + '.mp3', mode='wb') as f:
    f.write(audio_content)
  print("音乐内容保存完成")
  #with open(title + '.mp4', mode='wb') as f:
  #  f.write(video_content)
  #print("视频内容保存完成")
  return f"{title}.mp3"


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            inp1 = gr.Textbox(label="歌曲名", placeholder="上春山", lines=1)
            inp2 = gr.Textbox(label="B站视频对应的链接", placeholder="https://www.bilibili.com/video/BV1Wj421U7B4/", lines=1)
        btn = gr.Button("一键下载视频!", variant="primary")
        out1 = gr.File(label="为您下载的B站视频")
    btn.click(save_audio, [inp1, inp2], out1)
demo.launch(debug=True)