RTSP Fully Implemented
This commit is contained in:
@@ -243,6 +243,12 @@ def generate_transcoded_stream(file_path: pathlib.Path, start_time: float = 0.0)
|
||||
Transcode video to H.264 on-the-fly using FFmpeg.
|
||||
Streams H.264/MP4 that browsers can actually play.
|
||||
"""
|
||||
if not file_path.exists():
|
||||
raise HTTPException(status_code=404, detail="Video file not found")
|
||||
|
||||
if file_path.stat().st_size == 0:
|
||||
raise HTTPException(status_code=500, detail="Video file is empty (0 bytes)")
|
||||
|
||||
# FFmpeg command to transcode to H.264 with web-optimized settings
|
||||
cmd = [
|
||||
"ffmpeg",
|
||||
@@ -272,17 +278,27 @@ def generate_transcoded_stream(file_path: pathlib.Path, start_time: float = 0.0)
|
||||
|
||||
# Stream chunks
|
||||
chunk_size = 8192
|
||||
bytes_yielded = 0
|
||||
while True:
|
||||
chunk = process.stdout.read(chunk_size)
|
||||
if not chunk:
|
||||
break
|
||||
bytes_yielded += len(chunk)
|
||||
yield chunk
|
||||
|
||||
# Check for errors
|
||||
process.wait()
|
||||
if process.returncode != 0:
|
||||
stderr = process.stderr.read().decode('utf-8', errors='ignore')
|
||||
print(f"FFmpeg error (code {process.returncode}): {stderr}")
|
||||
if bytes_yielded == 0:
|
||||
raise HTTPException(status_code=500, detail=f"FFmpeg transcoding failed: {stderr[:200]}")
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"FFmpeg transcoding error: {e}")
|
||||
raise
|
||||
raise HTTPException(status_code=500, detail=f"Transcoding error: {str(e)}")
|
||||
|
||||
|
||||
@app.head("/videos/{file_id:path}/stream-transcoded")
|
||||
|
||||
Reference in New Issue
Block a user