VietCat commited on
Commit
a11e902
·
1 Parent(s): d407ef7

init project

Browse files
Files changed (5) hide show
  1. .gitignore +7 -0
  2. Dockerfile +19 -0
  3. app/main.py +40 -0
  4. app/utils.py +16 -0
  5. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ *.pyo
4
+ *.pyd
5
+ *.log
6
+ *.mp4
7
+ /tmp/*
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # Cài đặt hệ thống và yt-dlp
4
+ RUN apt-get update && apt-get install -y wget ffmpeg && \
5
+ pip install yt-dlp
6
+
7
+ # Tạo thư mục và sao chép mã nguồn
8
+ WORKDIR /app
9
+ COPY app /app
10
+ COPY requirements.txt .
11
+
12
+ # Cài đặt thư viện Python
13
+ RUN pip install -r requirements.txt
14
+
15
+ # Mở cổng API
16
+ EXPOSE 7860
17
+
18
+ # Chạy FastAPI bằng uvicorn
19
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
app/main.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, BackgroundTasks
2
+ from fastapi.responses import FileResponse, JSONResponse
3
+ from pydantic import BaseModel
4
+ import uuid
5
+ import os
6
+ from app.utils import download_video
7
+
8
+ app = FastAPI()
9
+ VIDEO_DIR = "/tmp" # RAM-based storage (HFS cho phép ghi tại đây)
10
+
11
+ class DownloadRequest(BaseModel):
12
+ url: str
13
+ cookie: str # Nội dung file cookies.txt
14
+
15
+ @app.post("/download")
16
+ async def download(req: DownloadRequest, background_tasks: BackgroundTasks):
17
+ video_id = str(uuid.uuid4())
18
+ filepath = f"{VIDEO_DIR}/{video_id}.mp4"
19
+ cookie_path = f"/tmp/{video_id}_cookies.txt"
20
+
21
+ with open(cookie_path, "w") as f:
22
+ f.write(req.cookie)
23
+
24
+ success = download_video(req.url, cookie_path, filepath)
25
+ if not success:
26
+ return JSONResponse({"error": "Download failed"}, status_code=500)
27
+
28
+ return {
29
+ "id": video_id,
30
+ "download_url": f"/video/{video_id}"
31
+ }
32
+
33
+ @app.get("/video/{video_id}")
34
+ def get_video(video_id: str, background_tasks: BackgroundTasks):
35
+ filepath = f"/tmp/{video_id}.mp4"
36
+ if not os.path.exists(filepath):
37
+ return JSONResponse({"error": "Not found"}, status_code=404)
38
+
39
+ background_tasks.add_task(os.remove, filepath)
40
+ return FileResponse(filepath, filename=f"{video_id}.mp4", media_type="video/mp4")
app/utils.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+
3
+ def download_video(url, cookie_file, output_path):
4
+ try:
5
+ cmd = [
6
+ "yt-dlp",
7
+ "--cookies", cookie_file,
8
+ "--max-filesize", "500M",
9
+ "-f", "bestvideo+bestaudio",
10
+ "-o", output_path,
11
+ url
12
+ ]
13
+ result = subprocess.run(cmd, capture_output=True, text=True)
14
+ return result.returncode == 0
15
+ except Exception:
16
+ return False
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ uvicorn