Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import os
|
| 3 |
import io
|
| 4 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 5 |
-
from fastapi.responses import StreamingResponse
|
| 6 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
-
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
-
allow_origins=["*"],
|
| 13 |
allow_credentials=True,
|
| 14 |
allow_methods=["*"],
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
| 18 |
-
def process_video(input_bytes
|
| 19 |
"""
|
| 20 |
-
Process video to change playback speed and compress
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
video_speed = 1 / speed
|
| 26 |
audio_speed = speed
|
| 27 |
|
| 28 |
cmd = [
|
| 29 |
-
"ffmpeg",
|
| 30 |
-
"-threads", str(os.cpu_count()),
|
| 31 |
-
"-i", "pipe:0", # read input from stdin
|
| 32 |
"-filter_complex", f"[0:v]setpts={video_speed}*PTS[v];[0:a]atempo={audio_speed}[a]",
|
| 33 |
"-map", "[v]", "-map", "[a]",
|
| 34 |
-
"-vcodec", "libx264",
|
| 35 |
-
"-crf", "30", # lower quality for speed
|
| 36 |
-
"-preset", "ultrafast", # fastest preset
|
| 37 |
"-movflags", "+faststart",
|
| 38 |
-
"-
|
| 39 |
-
"pipe:1" # write output to stdout
|
| 40 |
]
|
| 41 |
|
| 42 |
-
process = subprocess.Popen(cmd,
|
| 43 |
-
|
| 44 |
|
| 45 |
if process.returncode != 0:
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
return output_data
|
| 49 |
|
|
|
|
| 50 |
@app.post("/process_video")
|
| 51 |
async def upload_video(file: UploadFile = File(...), speed: float = 1.5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
video_bytes = await file.read()
|
| 53 |
processed_video = process_video(video_bytes, speed)
|
| 54 |
|
|
@@ -56,4 +69,4 @@ async def upload_video(file: UploadFile = File(...), speed: float = 1.5):
|
|
| 56 |
io.BytesIO(processed_video),
|
| 57 |
media_type="video/mp4",
|
| 58 |
headers={"Content-Disposition": "attachment; filename=processed_video.mp4"}
|
| 59 |
-
)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from fastapi.responses import StreamingResponse
|
| 3 |
+
import tempfile
|
| 4 |
import subprocess
|
| 5 |
import os
|
| 6 |
import io
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"], # or specific origin(s)
|
| 13 |
allow_credentials=True,
|
| 14 |
allow_methods=["*"],
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
| 18 |
+
def process_video(input_bytes, speed=1.5):
|
| 19 |
"""
|
| 20 |
+
Process video to change playback speed and compress.
|
| 21 |
"""
|
| 22 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_input:
|
| 23 |
+
tmp_input.write(input_bytes)
|
| 24 |
+
input_path = tmp_input.name
|
| 25 |
+
|
| 26 |
+
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 27 |
|
| 28 |
video_speed = 1 / speed
|
| 29 |
audio_speed = speed
|
| 30 |
|
| 31 |
cmd = [
|
| 32 |
+
"ffmpeg", "-i", input_path,
|
|
|
|
|
|
|
| 33 |
"-filter_complex", f"[0:v]setpts={video_speed}*PTS[v];[0:a]atempo={audio_speed}[a]",
|
| 34 |
"-map", "[v]", "-map", "[a]",
|
| 35 |
+
"-vcodec", "libx264", "-crf", "28", "-preset", "fast",
|
|
|
|
|
|
|
| 36 |
"-movflags", "+faststart",
|
| 37 |
+
"-y", output_path
|
|
|
|
| 38 |
]
|
| 39 |
|
| 40 |
+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
| 41 |
+
process.wait()
|
| 42 |
|
| 43 |
if process.returncode != 0:
|
| 44 |
+
os.remove(input_path)
|
| 45 |
+
os.remove(output_path)
|
| 46 |
+
raise HTTPException(status_code=500, detail="Video processing failed.")
|
| 47 |
+
|
| 48 |
+
with open(output_path, "rb") as f:
|
| 49 |
+
output_data = f.read()
|
| 50 |
+
|
| 51 |
+
os.remove(input_path)
|
| 52 |
+
os.remove(output_path)
|
| 53 |
|
| 54 |
return output_data
|
| 55 |
|
| 56 |
+
|
| 57 |
@app.post("/process_video")
|
| 58 |
async def upload_video(file: UploadFile = File(...), speed: float = 1.5):
|
| 59 |
+
"""
|
| 60 |
+
Upload a video file and return processed video.
|
| 61 |
+
"""
|
| 62 |
+
if not (0.25 <= speed <= 4.0):
|
| 63 |
+
raise HTTPException(status_code=400, detail="Speed must be between 0.25x and 4x.")
|
| 64 |
+
|
| 65 |
video_bytes = await file.read()
|
| 66 |
processed_video = process_video(video_bytes, speed)
|
| 67 |
|
|
|
|
| 69 |
io.BytesIO(processed_video),
|
| 70 |
media_type="video/mp4",
|
| 71 |
headers={"Content-Disposition": "attachment; filename=processed_video.mp4"}
|
| 72 |
+
)
|