Spaces:
Paused
Paused
Delete app3.py
Browse files
app3.py
DELETED
|
@@ -1,110 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
import subprocess
|
| 4 |
-
import base64
|
| 5 |
-
import os
|
| 6 |
-
import uuid
|
| 7 |
-
import shutil
|
| 8 |
-
import yt_dlp
|
| 9 |
-
|
| 10 |
-
app = FastAPI()
|
| 11 |
-
|
| 12 |
-
@app.get("/")
|
| 13 |
-
def greet_json():
|
| 14 |
-
return {"Hello": "Viral Cat Media Server Running!"}
|
| 15 |
-
|
| 16 |
-
class VideoRequest(BaseModel):
|
| 17 |
-
url: str
|
| 18 |
-
platform: str
|
| 19 |
-
is_pro: bool = False
|
| 20 |
-
fps: int = 2
|
| 21 |
-
|
| 22 |
-
def file_to_base64(filepath):
|
| 23 |
-
with open(filepath, "rb") as f:
|
| 24 |
-
return base64.b64encode(f.read()).decode('utf-8')
|
| 25 |
-
|
| 26 |
-
@app.post("/process-video")
|
| 27 |
-
def process_video(req: VideoRequest):
|
| 28 |
-
job_id = str(uuid.uuid4())
|
| 29 |
-
work_dir = f"/tmp/viralcat_{job_id}"
|
| 30 |
-
os.makedirs(work_dir, exist_ok=True)
|
| 31 |
-
|
| 32 |
-
video_path = f"{work_dir}/video.mp4"
|
| 33 |
-
audio_path = f"{work_dir}/audio.mp3"
|
| 34 |
-
|
| 35 |
-
try:
|
| 36 |
-
# 1. Download Video
|
| 37 |
-
# 🚨 FIX: Removed the broken 0.0.0.0 binding.
|
| 38 |
-
# 🚨 ADDED: 'extractor_args' to bypass YouTube blocking cloud IPs.
|
| 39 |
-
ydl_opts = {
|
| 40 |
-
'format': 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]/best',
|
| 41 |
-
'outtmpl': video_path,
|
| 42 |
-
'quiet': True,
|
| 43 |
-
'no_warnings': True,
|
| 44 |
-
'nocheckcertificate': True,
|
| 45 |
-
'extractor_args': {'youtube': ['player_client=android']} # Disguises the server as a mobile phone
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 49 |
-
ydl.download([req.url])
|
| 50 |
-
|
| 51 |
-
# Verify video downloaded successfully
|
| 52 |
-
if not os.path.exists(video_path):
|
| 53 |
-
files = os.listdir(work_dir)
|
| 54 |
-
if files:
|
| 55 |
-
video_path = os.path.join(work_dir, files[0])
|
| 56 |
-
else:
|
| 57 |
-
raise ValueError("Failed to download video file.")
|
| 58 |
-
|
| 59 |
-
# 2. Check Duration (Limit to 2 minutes / 120 seconds if not Pro)
|
| 60 |
-
probe = subprocess.run([
|
| 61 |
-
"ffprobe", "-v", "error", "-show_entries",
|
| 62 |
-
"format=duration", "-of", "default=noprint_wrappers=1:nokey=1",
|
| 63 |
-
video_path
|
| 64 |
-
], capture_output=True, text=True, check=True)
|
| 65 |
-
|
| 66 |
-
duration = float(probe.stdout.strip() or 0)
|
| 67 |
-
|
| 68 |
-
if duration > 120 and not req.is_pro:
|
| 69 |
-
raise ValueError(f"Video is {duration:.1f}s long. Free users are limited to 120 seconds.")
|
| 70 |
-
|
| 71 |
-
# 3. Dynamic Frame Extraction (e.g., 2 frames per second)
|
| 72 |
-
subprocess.run([
|
| 73 |
-
"ffmpeg", "-y", "-i", video_path,
|
| 74 |
-
"-vf", f"fps={req.fps}",
|
| 75 |
-
"-q:v", "2", f"{work_dir}/frame_%04d.jpg"
|
| 76 |
-
], check=True, capture_output=True)
|
| 77 |
-
|
| 78 |
-
# 4. Extract Uncut Audio
|
| 79 |
-
subprocess.run([
|
| 80 |
-
"ffmpeg", "-y", "-i", video_path,
|
| 81 |
-
"-q:a", "0", "-map", "a", "-ac", "1", "-b:a", "64k", audio_path
|
| 82 |
-
], check=True, capture_output=True)
|
| 83 |
-
|
| 84 |
-
# 5. Gather and convert to Base64
|
| 85 |
-
frame_files = sorted([f for f in os.listdir(work_dir) if f.startswith("frame_") and f.endswith(".jpg")])
|
| 86 |
-
frames_b64 =[file_to_base64(os.path.join(work_dir, f)) for f in frame_files]
|
| 87 |
-
audio_b64 = file_to_base64(audio_path) if os.path.exists(audio_path) else None
|
| 88 |
-
|
| 89 |
-
return {
|
| 90 |
-
"success": True,
|
| 91 |
-
"duration": duration,
|
| 92 |
-
"total_frames_extracted": len(frames_b64),
|
| 93 |
-
"frames": frames_b64,
|
| 94 |
-
"audio": audio_b64,
|
| 95 |
-
"thumbnail_url": "https://placehold.co/400x600/png"
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
except ValueError as ve:
|
| 99 |
-
raise HTTPException(status_code=400, detail=str(ve))
|
| 100 |
-
except Exception as e:
|
| 101 |
-
error_detail = str(e)
|
| 102 |
-
if hasattr(e, 'stderr') and getattr(e, 'stderr'):
|
| 103 |
-
error_detail += f" | {e.stderr.decode()}"
|
| 104 |
-
raise HTTPException(status_code=500, detail=error_detail)
|
| 105 |
-
|
| 106 |
-
finally:
|
| 107 |
-
# 🚨 CLEANUP: Prevent server bloat
|
| 108 |
-
if os.path.exists(work_dir):
|
| 109 |
-
shutil.rmtree(work_dir, ignore_errors=True)
|
| 110 |
-
print(f"[CLEANUP] Deleted temporary folder: {work_dir}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|