Pepguy commited on
Commit
df0bd72
·
verified ·
1 Parent(s): 3a2ae67

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py (FastAPI)
2
+ from fastapi import FastAPI, HTTPException, UploadFile, File, Form
3
+ import subprocess
4
+ import base64
5
+ import os
6
+ import uuid
7
+ import shutil
8
+
9
+ app = FastAPI()
10
+
11
+ @app.post("/process-video")
12
+ async def process_video(file: UploadFile = File(...), is_pro: bool = Form(False)):
13
+ job_id = str(uuid.uuid4())
14
+ work_dir = f"/tmp/viralcat_{job_id}"
15
+ os.makedirs(work_dir, exist_ok=True)
16
+ video_path = os.path.join(work_dir, "video.mp4")
17
+
18
+ try:
19
+ # 1. Save uploaded file
20
+ with open(video_path, "wb") as buffer:
21
+ shutil.copyfileobj(file.file, buffer)
22
+
23
+ # 2. Check Duration
24
+ probe = subprocess.run([
25
+ "ffprobe", "-v", "error", "-show_entries",
26
+ "format=duration", "-of", "default=noprint_wrappers=1:nokey=1",
27
+ video_path
28
+ ], capture_output=True, text=True, check=True)
29
+
30
+ duration = float(probe.stdout.strip() or 0)
31
+ if duration > 120 and not is_pro:
32
+ raise ValueError(f"Video is {duration:.1f}s. Free users are limited to 120s.")
33
+
34
+ # 3. Convert to Base64
35
+ with open(video_path, "rb") as f:
36
+ video_b64 = base64.b64encode(f.read()).decode('utf-8')
37
+
38
+ return {
39
+ "success": True,
40
+ "duration": duration,
41
+ "video_base64": video_base64
42
+ }
43
+
44
+ except Exception as e:
45
+ return {"success": False, "error": str(e)}
46
+ finally:
47
+ if os.path.exists(work_dir):
48
+ shutil.rmtree(work_dir, ignore_errors=True)