Add live video streaming for in-progress renders

This commit is contained in:
giles
2026-02-03 00:27:19 +00:00
parent 9302283e86
commit a57be27907
2 changed files with 46 additions and 2 deletions

View File

@@ -955,3 +955,40 @@ async def purge_failed_runs(
logger.info(f"Purged {len(deleted)} failed runs")
return {"purged": len(deleted), "run_ids": deleted}
@router.get("/{run_id}/stream")
async def stream_run_output(
run_id: str,
request: Request,
):
"""Stream the video output of a running render.
Returns the partial video file as it's being written,
allowing live preview of the render progress.
"""
from fastapi.responses import StreamingResponse, FileResponse
from pathlib import Path
import os
# Check for the streaming output file in the shared cache
cache_dir = os.environ.get("CACHE_DIR", "/data/cache")
stream_path = Path(cache_dir) / "streaming" / run_id / "output.mp4"
if not stream_path.exists():
raise HTTPException(404, "Stream not available yet")
file_size = stream_path.stat().st_size
if file_size == 0:
raise HTTPException(404, "Stream not ready")
# Return the file with headers that allow streaming of growing file
return FileResponse(
path=str(stream_path),
media_type="video/mp4",
headers={
"Accept-Ranges": "bytes",
"Cache-Control": "no-cache, no-store, must-revalidate",
"X-Content-Size": str(file_size),
}
)