Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import os,secrets,requests
|
| 3 |
from basic_pitch.inference import predict_and_save
|
| 4 |
from urllib.parse import quote
|
| 5 |
-
import
|
| 6 |
from deta import Deta # Import Deta
|
| 7 |
|
| 8 |
# Initialize
|
|
@@ -36,40 +36,42 @@ def addrecord(orig,pitmp3,mid,gname,ghint):
|
|
| 36 |
|
| 37 |
|
| 38 |
def downloadaud(video_id,gname,ghint):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
if
|
| 57 |
-
best_audio_format
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
|
| 74 |
def predict(ina):
|
| 75 |
fn=ina
|
|
|
|
| 2 |
import os,secrets,requests
|
| 3 |
from basic_pitch.inference import predict_and_save
|
| 4 |
from urllib.parse import quote
|
| 5 |
+
import traceback
|
| 6 |
from deta import Deta # Import Deta
|
| 7 |
|
| 8 |
# Initialize
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def downloadaud(video_id,gname,ghint):
|
| 39 |
+
try:
|
| 40 |
+
filename=secrets.token_hex()
|
| 41 |
+
# Step 1: Search for videos with the given query
|
| 42 |
+
|
| 43 |
+
if not video_id:
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
# Step 3: Get the formats for the video and find the URL for the best audio-only format
|
| 47 |
+
formats_url = f"https://draw-8fj.begin.app/api/info/{video_id}"
|
| 48 |
+
formats = requests.get(formats_url)
|
| 49 |
+
if formats.ok!=True:
|
| 50 |
+
formats_url = f"https://draw-8fj-staging.begin.app/api/info/{video_id}"
|
| 51 |
+
formats = requests.get(formats_url)
|
| 52 |
+
formats_response=formats.json()
|
| 53 |
+
#print(formats_response["formats"])
|
| 54 |
+
best_audio_format = None
|
| 55 |
+
for fmt in formats_response.get("formats", []):
|
| 56 |
+
if fmt.get("hasVideo") is False and fmt.get("hasAudio") is True and fmt.get("container") == "mp4":
|
| 57 |
+
if not best_audio_format or fmt.get("audioBitrate") > best_audio_format.get("audioBitrate"):
|
| 58 |
+
best_audio_format = fmt
|
| 59 |
+
print(best_audio_format)
|
| 60 |
+
# If no suitable audio format was found, return None
|
| 61 |
+
if not best_audio_format:
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
aurl=best_audio_format["url"]
|
| 65 |
+
print(aurl)
|
| 66 |
+
command = f'./pget -o {filename}.mp4 -p 4 "{aurl}" '
|
| 67 |
+
os.system(command)
|
| 68 |
+
os.system(f'ffmpeg -i {filename}.mp4 -q:a 0 -map a {filename}.mp3')
|
| 69 |
+
os.system(f'mv {filename}.mp3 /tmp/{filename}.mp3')
|
| 70 |
+
o,m,i= predict(f'/tmp/{filename}.mp3')
|
| 71 |
+
res=addrecord(o,m,i,gname,ghint)
|
| 72 |
+
return o,m,i,res
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(traceback(e))
|
| 75 |
|
| 76 |
def predict(ina):
|
| 77 |
fn=ina
|