Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
from basic_pitch.inference import predict_and_save
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def predict(ina):
|
| 5 |
predict_and_save(
|
| 6 |
[ina],
|
|
@@ -15,8 +69,8 @@ def predict(ina):
|
|
| 15 |
os.system(f'ffmpeg -i "{ina}.wav" -f mp3 -acodec libmp3lame -y "{ina}.mp3"')
|
| 16 |
|
| 17 |
return f'{ina}.mp3',f'{ina}.mid'
|
| 18 |
-
aud=gr.
|
| 19 |
oa=gr.Audio(label="预览midi")
|
| 20 |
pf=gr.File(label="下载midi")
|
| 21 |
-
demo = gr.Interface(fn=
|
| 22 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os,random
|
| 3 |
from basic_pitch.inference import predict_and_save
|
| 4 |
+
from urllib.parse import quote
|
| 5 |
+
os.system('chmod +x ./pget')
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def downloadaud(query):
|
| 12 |
+
filename=str(random.randint(0,9999999))
|
| 13 |
+
# Step 1: Search for videos with the given query
|
| 14 |
+
search_url = f"https://draw-8fj.begin.app/api/search/{quote(query)}"
|
| 15 |
+
search_response = requests.get(search_url).json()
|
| 16 |
+
print('1=>', search_response)
|
| 17 |
+
# Step 2: Find the first video with duration less than 10 minutes and extract its ID
|
| 18 |
+
video_id = None
|
| 19 |
+
#search_response = sorted(search_response, key=lambda x: x["views"], reverse=True)
|
| 20 |
+
for item in search_response:
|
| 21 |
+
duration = item.get("duration_raw")
|
| 22 |
+
if duration and len(duration.split(':'))< 3 and int(duration.split(':')[0])<10:
|
| 23 |
+
video_id = item.get("id", {}).get("videoId")
|
| 24 |
+
break
|
| 25 |
+
print('1-r',video_id)
|
| 26 |
+
# If no video with duration less than 10 minutes was found, return None
|
| 27 |
+
if not video_id:
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
# Step 3: Get the formats for the video and find the URL for the best audio-only format
|
| 31 |
+
formats_url = f"https://draw-8fj.begin.app/api/info/{video_id}"
|
| 32 |
+
formats = requests.get(formats_url)
|
| 33 |
+
if formats.ok!=True:
|
| 34 |
+
formats_url = f"https://draw-8fj-staging.begin.app/api/info/{video_id}"
|
| 35 |
+
formats = requests.get(formats_url)
|
| 36 |
+
formats_response=formats.json()
|
| 37 |
+
print(formats_response["formats"])
|
| 38 |
+
best_audio_format = None
|
| 39 |
+
for fmt in formats_response.get("formats", []):
|
| 40 |
+
if fmt.get("hasVideo") is False and fmt.get("hasAudio") is True and fmt.get("container") == "mp4":
|
| 41 |
+
if not best_audio_format or fmt.get("audioBitrate") > best_audio_format.get("audioBitrate"):
|
| 42 |
+
best_audio_format = fmt
|
| 43 |
+
print(best_audio_format)
|
| 44 |
+
# If no suitable audio format was found, return None
|
| 45 |
+
if not best_audio_format:
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
aurl=best_audio_format["url"]
|
| 49 |
+
print(aurl)
|
| 50 |
+
command = f'./pget -o {filename}.mp4 -p 4 "{aurl}" '
|
| 51 |
+
os.system(command)
|
| 52 |
+
os.system(f'ffmpeg -i {filename}.mp4 -q:a 0 -map a {filename}.mp3')
|
| 53 |
+
os.system(f'mv {filename}.mp3 /tmp/{filename}.mp3')
|
| 54 |
+
res= predict(f'/tmp/{filename}.mp3')
|
| 55 |
+
return res
|
| 56 |
+
|
| 57 |
+
|
| 58 |
def predict(ina):
|
| 59 |
predict_and_save(
|
| 60 |
[ina],
|
|
|
|
| 69 |
os.system(f'ffmpeg -i "{ina}.wav" -f mp3 -acodec libmp3lame -y "{ina}.mp3"')
|
| 70 |
|
| 71 |
return f'{ina}.mp3',f'{ina}.mid'
|
| 72 |
+
aud=gr.Textbox(label="yt query")
|
| 73 |
oa=gr.Audio(label="预览midi")
|
| 74 |
pf=gr.File(label="下载midi")
|
| 75 |
+
demo = gr.Interface(fn=downloadaud, inputs=[aud], outputs=[oa,pf])
|
| 76 |
demo.launch()
|