Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import shutil
|
| 4 |
+
import subprocess
|
| 5 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 6 |
+
from fastapi.responses import FileResponse
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Add CORS middleware to allow requests from your frontend
|
| 13 |
+
app.add_middleware(
|
| 14 |
+
CORSMiddleware,
|
| 15 |
+
allow_origins=["*"], # Set this to your frontend URL in production
|
| 16 |
+
allow_credentials=True,
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Create a directory to store temporary files
|
| 22 |
+
TEMP_DIR = "temp_files"
|
| 23 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 24 |
+
|
| 25 |
+
@app.get("/")
|
| 26 |
+
def read_root():
|
| 27 |
+
return {"message": "Video Subtitle Server is running"}
|
| 28 |
+
|
| 29 |
+
@app.post("/burn-subtitles")
|
| 30 |
+
async def burn_subtitles(
|
| 31 |
+
video: UploadFile = File(...),
|
| 32 |
+
subtitle: UploadFile = File(...),
|
| 33 |
+
font_size: str = Form("20"),
|
| 34 |
+
font_color: str = Form("white"),
|
| 35 |
+
output_format: str = Form("mp4")
|
| 36 |
+
):
|
| 37 |
+
# Create unique ID for this job
|
| 38 |
+
job_id = str(uuid.uuid4())
|
| 39 |
+
job_dir = os.path.join(TEMP_DIR, job_id)
|
| 40 |
+
os.makedirs(job_dir, exist_ok=True)
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Save uploaded files
|
| 44 |
+
video_path = os.path.join(job_dir, f"input{os.path.splitext(video.filename)[1]}")
|
| 45 |
+
subtitle_path = os.path.join(job_dir, "subtitles.srt")
|
| 46 |
+
output_path = os.path.join(job_dir, f"output.{output_format}")
|
| 47 |
+
|
| 48 |
+
# Write the files to disk
|
| 49 |
+
with open(video_path, "wb") as video_file:
|
| 50 |
+
shutil.copyfileobj(video.file, video_file)
|
| 51 |
+
|
| 52 |
+
with open(subtitle_path, "wb") as subtitle_file:
|
| 53 |
+
shutil.copyfileobj(subtitle.file, subtitle_file)
|
| 54 |
+
|
| 55 |
+
# Ensure font color is in the correct format for FFmpeg
|
| 56 |
+
if font_color.startswith('#'):
|
| 57 |
+
# Convert hex to FFmpeg compatible format
|
| 58 |
+
font_color = font_color.lstrip('#')
|
| 59 |
+
# Convert RGB to BGR which is what FFmpeg expects
|
| 60 |
+
if len(font_color) == 6:
|
| 61 |
+
r, g, b = font_color[0:2], font_color[2:4], font_color[4:6]
|
| 62 |
+
font_color = f"&H{b}{g}{r}&"
|
| 63 |
+
|
| 64 |
+
# Run FFmpeg command to burn subtitles
|
| 65 |
+
subtitle_style = f"FontSize={font_size},PrimaryColour={font_color}"
|
| 66 |
+
command = [
|
| 67 |
+
"ffmpeg",
|
| 68 |
+
"-i", video_path,
|
| 69 |
+
"-vf", f"subtitles={subtitle_path}:force_style='{subtitle_style}'",
|
| 70 |
+
"-c:a", "copy",
|
| 71 |
+
output_path
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
# Execute FFmpeg command
|
| 75 |
+
process = subprocess.run(command, capture_output=True, text=True)
|
| 76 |
+
|
| 77 |
+
if process.returncode != 0:
|
| 78 |
+
raise HTTPException(status_code=500, detail=f"FFmpeg Error: {process.stderr}")
|
| 79 |
+
|
| 80 |
+
# Return the processed video
|
| 81 |
+
return FileResponse(
|
| 82 |
+
output_path,
|
| 83 |
+
media_type=f"video/{output_format}",
|
| 84 |
+
filename=f"processed_video.{output_format}"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
# Clean up in case of errors
|
| 89 |
+
if os.path.exists(job_dir):
|
| 90 |
+
shutil.rmtree(job_dir)
|
| 91 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 92 |
+
|
| 93 |
+
finally:
|
| 94 |
+
# Clean up temporary files (you might want to do this after a delay in production)
|
| 95 |
+
if os.path.exists(job_dir):
|
| 96 |
+
shutil.rmtree(job_dir)
|
| 97 |
+
|
| 98 |
+
@app.post("/convert-srt")
|
| 99 |
+
async def convert_srt(
|
| 100 |
+
subtitle: UploadFile = File(...),
|
| 101 |
+
):
|
| 102 |
+
# Create unique ID for this job
|
| 103 |
+
job_id = str(uuid.uuid4())
|
| 104 |
+
job_dir = os.path.join(TEMP_DIR, job_id)
|
| 105 |
+
os.makedirs(job_dir, exist_ok=True)
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
# Save uploaded file
|
| 109 |
+
subtitle_path = os.path.join(job_dir, "subtitles.srt")
|
| 110 |
+
|
| 111 |
+
# Write the file to disk
|
| 112 |
+
with open(subtitle_path, "wb") as subtitle_file:
|
| 113 |
+
shutil.copyfileobj(subtitle.file, subtitle_file)
|
| 114 |
+
|
| 115 |
+
# Return the SRT file
|
| 116 |
+
return FileResponse(
|
| 117 |
+
subtitle_path,
|
| 118 |
+
media_type="text/plain",
|
| 119 |
+
filename="subtitles.srt"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 124 |
+
|
| 125 |
+
finally:
|
| 126 |
+
# Clean up temporary files
|
| 127 |
+
if os.path.exists(job_dir):
|
| 128 |
+
shutil.rmtree(job_dir)
|
| 129 |
+
|
| 130 |
+
# Add a health check endpoint
|
| 131 |
+
@app.get("/health")
|
| 132 |
+
def health_check():
|
| 133 |
+
return {"status": "healthy"}
|
| 134 |
+
|