talktalkai-tool / app.py
kevinwang676's picture
Update app.py
6fa1f12 verified
raw
history blame
1.76 kB
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}.mp4"
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
inp1 = gr.Textbox(label="歌曲名", lines=1)
inp2 = gr.Textbox(label="B站视频对应的链接", placeholder="https://www.bilibili.com/video/BV1Wj421U7B4/", lines=1)
btn = gr.Button("一键下载视频!")
out1 = gr.Video(label="为您下载的B站视频")
btn.click(save_audio, [inp1, inp2], out1)
demo.launch(debug=True)