Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import uuid
|
|
| 7 |
from fastapi import FastAPI, BackgroundTasks
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from starlette.responses import FileResponse, JSONResponse
|
| 10 |
-
from typing import Optional
|
| 11 |
|
| 12 |
# --- Configuration ---
|
| 13 |
app = FastAPI()
|
|
@@ -31,13 +31,13 @@ def init_db():
|
|
| 31 |
task_id TEXT PRIMARY KEY,
|
| 32 |
status TEXT NOT NULL,
|
| 33 |
file_name TEXT,
|
| 34 |
-
error_message TEXT
|
|
|
|
| 35 |
)
|
| 36 |
''')
|
| 37 |
conn.commit()
|
| 38 |
|
| 39 |
# --- Background Re-encoding Task ---
|
| 40 |
-
# β
β
β
FIX: Accept 'cookie' as an argument
|
| 41 |
def process_video_task(url: str, cookie: Optional[str], task_id: str):
|
| 42 |
db = get_db()
|
| 43 |
temp_in_path = os.path.join(DOWNLOAD_DIR, f"{task_id}_in.mp4")
|
|
@@ -48,15 +48,13 @@ def process_video_task(url: str, cookie: Optional[str], task_id: str):
|
|
| 48 |
db.execute("UPDATE jobs SET status = 'downloading' WHERE task_id = ?", (task_id,))
|
| 49 |
db.commit()
|
| 50 |
|
| 51 |
-
# β
β
β
FIX: Add browser headers
|
| 52 |
headers = {
|
| 53 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
|
| 54 |
"Referer": "https://terabox.com/"
|
| 55 |
}
|
| 56 |
if cookie:
|
| 57 |
-
headers["Cookie"] = cookie
|
| 58 |
|
| 59 |
-
# β
β
β
FIX: Use the 'headers' when downloading
|
| 60 |
with httpx.stream("GET", url, headers=headers, follow_redirects=True, timeout=600.0) as r:
|
| 61 |
r.raise_for_status()
|
| 62 |
with open(temp_in_path, 'wb') as f:
|
|
@@ -67,7 +65,6 @@ def process_video_task(url: str, cookie: Optional[str], task_id: str):
|
|
| 67 |
db.execute("UPDATE jobs SET status = 'encoding' WHERE task_id = ?", (task_id,))
|
| 68 |
db.commit()
|
| 69 |
|
| 70 |
-
# β οΈ THIS IS THE 100% CPU RE-ENCODE YOU WANTED β οΈ
|
| 71 |
(
|
| 72 |
ffmpeg
|
| 73 |
.input(temp_in_path)
|
|
@@ -75,14 +72,23 @@ def process_video_task(url: str, cookie: Optional[str], task_id: str):
|
|
| 75 |
.run(capture_stdout=True, capture_stderr=True)
|
| 76 |
)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# 3. Update DB: Complete
|
| 79 |
final_file_name = f"{task_id}_out.mp4"
|
| 80 |
-
db.execute(
|
|
|
|
|
|
|
|
|
|
| 81 |
db.commit()
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
error_msg = str(e)
|
| 85 |
-
if hasattr(e, 'stderr'):
|
| 86 |
error_msg = e.stderr.decode()
|
| 87 |
print(f"Error processing task {task_id}: {error_msg}")
|
| 88 |
db.execute("UPDATE jobs SET status = 'error', error_message = ? WHERE task_id = ?", (error_msg, task_id))
|
|
@@ -95,7 +101,7 @@ def process_video_task(url: str, cookie: Optional[str], task_id: str):
|
|
| 95 |
# --- API Endpoints ---
|
| 96 |
class VideoRequest(BaseModel):
|
| 97 |
url: str
|
| 98 |
-
cookie: Optional[str] = None
|
| 99 |
|
| 100 |
@app.post("/process")
|
| 101 |
async def start_processing(request: VideoRequest, background_tasks: BackgroundTasks):
|
|
@@ -106,7 +112,6 @@ async def start_processing(request: VideoRequest, background_tasks: BackgroundTa
|
|
| 106 |
db.execute("INSERT INTO jobs (task_id, status) VALUES (?, 'queued')", (task_id,))
|
| 107 |
db.commit()
|
| 108 |
|
| 109 |
-
# β
β
β
FIX: Pass the cookie to the background task
|
| 110 |
background_tasks.add_task(process_video_task, request.url, request.cookie, task_id)
|
| 111 |
return {"status": "queued", "task_id": task_id}
|
| 112 |
|
|
@@ -119,7 +124,7 @@ async def get_status(task_id: str):
|
|
| 119 |
if not job:
|
| 120 |
return JSONResponse(status_code=404, content={"status": "not_found"})
|
| 121 |
|
| 122 |
-
return dict(job) #
|
| 123 |
|
| 124 |
@app.get("/download/{file_name}")
|
| 125 |
async def download_file(file_name: str):
|
|
@@ -132,7 +137,7 @@ async def download_file(file_name: str):
|
|
| 132 |
|
| 133 |
@app.get("/")
|
| 134 |
async def health_check():
|
| 135 |
-
""" Health check endpoint
|
| 136 |
return {"status": "ok"}
|
| 137 |
|
| 138 |
|
|
|
|
| 7 |
from fastapi import FastAPI, BackgroundTasks
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from starlette.responses import FileResponse, JSONResponse
|
| 10 |
+
from typing import Optional
|
| 11 |
|
| 12 |
# --- Configuration ---
|
| 13 |
app = FastAPI()
|
|
|
|
| 31 |
task_id TEXT PRIMARY KEY,
|
| 32 |
status TEXT NOT NULL,
|
| 33 |
file_name TEXT,
|
| 34 |
+
error_message TEXT,
|
| 35 |
+
duration INTEGER
|
| 36 |
)
|
| 37 |
''')
|
| 38 |
conn.commit()
|
| 39 |
|
| 40 |
# --- Background Re-encoding Task ---
|
|
|
|
| 41 |
def process_video_task(url: str, cookie: Optional[str], task_id: str):
|
| 42 |
db = get_db()
|
| 43 |
temp_in_path = os.path.join(DOWNLOAD_DIR, f"{task_id}_in.mp4")
|
|
|
|
| 48 |
db.execute("UPDATE jobs SET status = 'downloading' WHERE task_id = ?", (task_id,))
|
| 49 |
db.commit()
|
| 50 |
|
|
|
|
| 51 |
headers = {
|
| 52 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
|
| 53 |
"Referer": "https://terabox.com/"
|
| 54 |
}
|
| 55 |
if cookie:
|
| 56 |
+
headers["Cookie"] = cookie
|
| 57 |
|
|
|
|
| 58 |
with httpx.stream("GET", url, headers=headers, follow_redirects=True, timeout=600.0) as r:
|
| 59 |
r.raise_for_status()
|
| 60 |
with open(temp_in_path, 'wb') as f:
|
|
|
|
| 65 |
db.execute("UPDATE jobs SET status = 'encoding' WHERE task_id = ?", (task_id,))
|
| 66 |
db.commit()
|
| 67 |
|
|
|
|
| 68 |
(
|
| 69 |
ffmpeg
|
| 70 |
.input(temp_in_path)
|
|
|
|
| 72 |
.run(capture_stdout=True, capture_stderr=True)
|
| 73 |
)
|
| 74 |
|
| 75 |
+
#
|
| 76 |
+
# β
β
β
FIX: Get duration using ffprobe β
β
β
|
| 77 |
+
#
|
| 78 |
+
probe = ffmpeg.probe(temp_out_path)
|
| 79 |
+
duration = int(float(probe['format']['duration'])) # Get duration in seconds
|
| 80 |
+
|
| 81 |
# 3. Update DB: Complete
|
| 82 |
final_file_name = f"{task_id}_out.mp4"
|
| 83 |
+
db.execute(
|
| 84 |
+
"UPDATE jobs SET status = 'complete', file_name = ?, duration = ? WHERE task_id = ?",
|
| 85 |
+
(final_file_name, duration, task_id)
|
| 86 |
+
)
|
| 87 |
db.commit()
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
error_msg = str(e)
|
| 91 |
+
if hasattr(e, 'stderr'):
|
| 92 |
error_msg = e.stderr.decode()
|
| 93 |
print(f"Error processing task {task_id}: {error_msg}")
|
| 94 |
db.execute("UPDATE jobs SET status = 'error', error_message = ? WHERE task_id = ?", (error_msg, task_id))
|
|
|
|
| 101 |
# --- API Endpoints ---
|
| 102 |
class VideoRequest(BaseModel):
|
| 103 |
url: str
|
| 104 |
+
cookie: Optional[str] = None
|
| 105 |
|
| 106 |
@app.post("/process")
|
| 107 |
async def start_processing(request: VideoRequest, background_tasks: BackgroundTasks):
|
|
|
|
| 112 |
db.execute("INSERT INTO jobs (task_id, status) VALUES (?, 'queued')", (task_id,))
|
| 113 |
db.commit()
|
| 114 |
|
|
|
|
| 115 |
background_tasks.add_task(process_video_task, request.url, request.cookie, task_id)
|
| 116 |
return {"status": "queued", "task_id": task_id}
|
| 117 |
|
|
|
|
| 124 |
if not job:
|
| 125 |
return JSONResponse(status_code=404, content={"status": "not_found"})
|
| 126 |
|
| 127 |
+
return dict(job) # This will now include 'duration'
|
| 128 |
|
| 129 |
@app.get("/download/{file_name}")
|
| 130 |
async def download_file(file_name: str):
|
|
|
|
| 137 |
|
| 138 |
@app.get("/")
|
| 139 |
async def health_check():
|
| 140 |
+
""" Health check endpoint. """
|
| 141 |
return {"status": "ok"}
|
| 142 |
|
| 143 |
|