Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,52 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import os
|
6 |
-
import json
|
7 |
-
import shutil
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
@app.get("/info")
|
20 |
-
async def get_video_info(url: str = Query(...)):
|
21 |
try:
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
if result.returncode != 0:
|
31 |
-
raise Exception(result.stderr)
|
32 |
-
data = json.loads(result.stdout)
|
33 |
-
return {
|
34 |
-
"title": data.get("title"),
|
35 |
-
"thumbnail": data.get("thumbnail"),
|
36 |
-
"formats": [
|
37 |
-
{"id": f["format_id"], "ext": f["ext"], "note": f.get("format_note"), "filesize": f.get("filesize")}
|
38 |
-
for f in data.get("formats", [])
|
39 |
-
if f.get("vcodec") != "none" or f.get("acodec") != "none"
|
40 |
-
]
|
41 |
-
}
|
42 |
except Exception as e:
|
43 |
-
raise HTTPException(status_code=
|
44 |
-
|
45 |
-
@app.post("/
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
cmd += ["--recode-video", request.convert_to]
|
66 |
-
|
67 |
-
tasks[task_id]["status"] = "downloading"
|
68 |
-
subprocess.run(cmd, capture_output=True, text=True)
|
69 |
-
tasks[task_id]["status"] = "completed"
|
70 |
-
tasks[task_id]["file"] = f"/tmp/{output_file}"
|
71 |
-
except Exception as e:
|
72 |
-
tasks[task_id]["status"] = "error"
|
73 |
-
tasks[task_id]["error"] = str(e)
|
74 |
-
|
75 |
-
background_tasks.add_task(_do_download)
|
76 |
-
return {"task_id": task_id}
|
77 |
-
|
78 |
-
@app.get("/progress/{task_id}")
|
79 |
-
def check_progress(task_id: str):
|
80 |
-
if task_id not in tasks:
|
81 |
-
raise HTTPException(status_code=404, detail="Task ID not found")
|
82 |
-
return tasks[task_id]
|
83 |
-
|
84 |
-
@app.get("/cancel/{task_id}")
|
85 |
-
def cancel_task(task_id: str):
|
86 |
-
# Basic cancellation support stub (real cancellation needs async task mgmt)
|
87 |
-
if task_id in tasks and tasks[task_id]["status"] not in ["completed", "error"]:
|
88 |
-
tasks[task_id]["status"] = "cancelled"
|
89 |
-
return {"detail": "Task cancelled"}
|
90 |
-
raise HTTPException(status_code=404, detail="Task not found or already done")
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
+
import yt_dlp
|
4 |
+
import base64
|
|
|
|
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
class VideoRequest(BaseModel):
|
9 |
+
video_id: str
|
10 |
|
11 |
+
@app.post("/audio/")
|
12 |
+
def get_audio_link(req: VideoRequest):
|
13 |
+
url = f"https://www.youtube.com/watch?v={req.video_id}"
|
14 |
+
ydl_opts = {
|
15 |
+
'quiet': True,
|
16 |
+
'format': 'bestaudio/best',
|
17 |
+
'skip_download': True,
|
18 |
+
'forceurl': True,
|
19 |
+
}
|
20 |
|
|
|
|
|
21 |
try:
|
22 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
23 |
+
info = ydl.extract_info(url, download=False)
|
24 |
+
for f in info['formats']:
|
25 |
+
if f.get('acodec') != 'none' and f.get('vcodec') == 'none':
|
26 |
+
audio_url = f['url']
|
27 |
+
b64 = base64.b64encode(audio_url.encode()).decode()
|
28 |
+
return {"status": "success", "audio_url": b64}
|
29 |
+
raise Exception("No audio found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
except Exception as e:
|
31 |
+
raise HTTPException(status_code=400, detail=str(e))
|
32 |
+
|
33 |
+
@app.post("/video/")
|
34 |
+
def get_video_link(req: VideoRequest):
|
35 |
+
url = f"https://www.youtube.com/watch?v={req.video_id}"
|
36 |
+
ydl_opts = {
|
37 |
+
'quiet': True,
|
38 |
+
'format': 'best[height<=720][width<=1280]',
|
39 |
+
'skip_download': True,
|
40 |
+
'forceurl': True,
|
41 |
+
}
|
42 |
+
try:
|
43 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
44 |
+
info = ydl.extract_info(url, download=False)
|
45 |
+
for f in info['formats']:
|
46 |
+
if f.get('vcodec') != 'none':
|
47 |
+
video_url = f['url']
|
48 |
+
b64 = base64.b64encode(video_url.encode()).decode()
|
49 |
+
return {"status": "success", "video_url": b64}
|
50 |
+
raise Exception("No video found")
|
51 |
+
except Exception as e:
|
52 |
+
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|