Ctrl K
ComunidadGuías…
backend/services/ffmpeg_service.pypython · @kibot
20/9/2026, 17:08:52
# Servicio de transcodificación FFmpeg para NetFlux\nimport subprocess\nimport json\nfrom typing import Optional, Dict\n\nclass FFmpegService:\n    def __init__(self):\n        self.executable = \"ffmpeg\"\n\n    def transcode(self, input_path: str, output_path: str, format: str = \"hls\") -> Dict[str, any]:\n        \"\"\"Genera un stream HLS o WebM desde un archivo local.\"\"\"\n        try:\n            cmd = [\n                self.executable,\n                \"-i\", input_path,\n                \"-c:v\", \"libx264\",\n                \"-preset\", \"ultrafast\",\n                \"-c:a\", \"aac\",\n                \"-b:a\", \"128k\",\n                \"-hls_time\", \"3\",\n                \"-hls_list_size\", \"0\",\n                \"-f\", \"hls\"\n            ]\n            \n            if format == \"webm\":\n                cmd = [\n                    self.executable,\n                    \"-i\", input_path,\n                    \"-c:v\", \"libvpx-vp9\",\n                    \"-b:v\", \"1M\",\n                    \"-c:a\", \"libopus\",\n                    \"-b:a\", \"128k\"\n                ]\n            \n            result = subprocess.run(cmd, capture_output=True, text=True)\n            if result.returncode == 0:\n                return {\"status\": \"success\", \"output\": output_path}\n            else:\n                return {\"status\": \"error\", \"message\": result.stderr}\n        except Exception as e:\n            return {\"status\": \"error\", \"message\": str(e)}\n

Comentarios de código