Pepguy commited on
Commit
44f2b5d
·
verified ·
1 Parent(s): da922cb

Delete app2.py

Browse files
Files changed (1) hide show
  1. app2.py +0 -85
app2.py DELETED
@@ -1,85 +0,0 @@
1
- from fastapi import FastAPI, HTTPException, UploadFile, File, Form
2
- import subprocess
3
- import base64
4
- import os
5
- import uuid
6
- import shutil
7
-
8
- app = FastAPI()
9
-
10
- @app.get("/")
11
- def greet_json():
12
- return {"status": "online", "engine": "Viral Cat Local Processor"}
13
-
14
- def file_to_base64(filepath):
15
- if not os.path.exists(filepath): return None
16
- with open(filepath, "rb") as f:
17
- return base64.b64encode(f.read()).decode('utf-8')
18
-
19
- @app.post("/process-video")
20
- async def process_video(
21
- file: UploadFile = File(...),
22
- fps: int = Form(2),
23
- is_pro: bool = Form(False)
24
- ):
25
- job_id = str(uuid.uuid4())
26
- work_dir = f"/tmp/viralcat_{job_id}"
27
- os.makedirs(work_dir, exist_ok=True)
28
-
29
- video_path = os.path.join(work_dir, "video.mp4")
30
- audio_path = os.path.join(work_dir, "audio.mp3")
31
-
32
- try:
33
- # 1. Save the uploaded file locally
34
- with open(video_path, "wb") as buffer:
35
- shutil.copyfileobj(file.file, buffer)
36
-
37
- # 2. Check Duration (120s limit for free)
38
- probe = subprocess.run([
39
- "ffprobe", "-v", "error", "-show_entries",
40
- "format=duration", "-of", "default=noprint_wrappers=1:nokey=1",
41
- video_path
42
- ], capture_output=True, text=True, check=True)
43
-
44
- duration = float(probe.stdout.strip() or 0)
45
- if duration > 120 and not is_pro:
46
- raise ValueError(f"Video ({duration:.1f}s) exceeds 120s limit.")
47
-
48
- # 3. Extract Frames (2 per second)
49
- subprocess.run([
50
- "ffmpeg", "-y", "-i", video_path,
51
- "-vf", f"fps={fps}",
52
- "-q:v", "4", f"{work_dir}/frame_%04d.jpg"
53
- ], check=True, capture_output=True)
54
-
55
- # 4. Extract Audio
56
- subprocess.run([
57
- "ffmpeg", "-y", "-i", video_path,
58
- "-q:a", "0", "-map", "a", "-ac", "1", "-b:a", "64k", audio_path
59
- ], check=True, capture_output=True)
60
-
61
- # 5. Base64 Conversion
62
- frame_files = sorted([f for f in os.listdir(work_dir) if f.startswith("frame_") and f.endswith(".jpg")])
63
- if len(frame_files) > 80: frame_files = frame_files[:80] # Safety cap
64
-
65
- frames_b64 =[file_to_base64(os.path.join(work_dir, f)) for f in frame_files]
66
- audio_b64 = file_to_base64(audio_path)
67
-
68
- print(f"Done with video, {len(frames_b64)}")
69
-
70
- return {
71
- "success": True,
72
- "total_frames": len(frames_b64),
73
- "duration": duration,
74
- "frames": frames_b64,
75
- "audio": audio_b64
76
- }
77
-
78
- except Exception as e:
79
- print(f"ERROR: {str(e)}")
80
- return {"success": False, "error": str(e)}
81
-
82
- finally:
83
- # CLEANUP
84
- if os.path.exists(work_dir):
85
- shutil.rmtree(work_dir, ignore_errors=True)