File size: 925 Bytes
923cd30
 
 
16c970a
923cd30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import subprocess
from fastapi import HTTPException

def compress_video(input_path: str, output_path: str, resolution: str = "256x144"):
    """Compress video using FFmpeg for maximum reduction, with audio removed."""
    command = [
        "ffmpeg",
        "-y",  # Force overwrite without asking
        "-i", input_path,
        "-vf", f"scale={resolution}",
        "-an",  # Remove audio
        "-c:v", "libx265",
        "-preset", "medium",  # Changed from veryslow for better speed/compression balance
        "-crf", "32",
        "-maxrate", "150k",
        "-bufsize", "200k",
        "-r", "10",
        output_path
    ]
    
    try:
        subprocess.run(command, check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        print(f"Compression error: {e.stderr}")
        raise HTTPException(status_code=500, detail="Video compression failed")