I am working on a screen recording application where it allows users to record their screen and I use websocket to send their bytes data in real time to my FastAPI Python server. The bytes are sent every 2 second and I use FFmpeg to keep saving the bytes in the output MP4 video file.
Problem:
Everything was working fine when I had the server running on my local machine, however, I just deployed the server to EC2 instance us-ease-1
and when I try to record the videos, the videos are really short, IE, if I record a 30 second video, it only has the video for 3 second. Sometimes it saves 90% of the video and sometimes less. I am not sure what is the problem here.
I have been trying to debug the code for the past few days, but with no success
Here is my code :
FRONTEND
const recorder = new MediaRecorder(stream, {
mimeType: 'video/webm;codecs=H264',
videoBitsPerSecond: 8000000
});
recorder.ondataavailable = (e: BlobEvent) => {
socketRef.socket.send(e.data)
}
And here is my python code :-
@router.websocket("/stream")
async def websocket_endpoint(websocket: WebSocket, token: str = Query(...), videoId: str = Query(...), authorize: AuthJWT = Depends()):
await manager.connect(websocket)
dataNumber = 1
recordingFile = os.path.join(temp_dir, f"recording_{videoId}.mp4")
command = [
'ffmpeg',
'-y',
'-i',
'-',
'-codec:v',
'copy',
'-f', 'mp4',
recordingFile,
]
process = subprocess.Popen(command, stdin=subprocess.PIPE)
try:
while True:
try:
data = await websocket.receive_bytes()
if not data:
break
process.stdin.write(data)
await websocket.send_json({"chunkNumber": dataNumber, "status": 200})
dataNumber = dataNumber + 1
except RuntimeError:
break
except WebSocketDisconnect:
print(f"Client disconnected: {websocket.client.host}")
finally:
manager.disconnect(websocket)
# Close stdin to signal EOF
process.stdin.close()
# Wait for FFmpeg to finish processing
process.wait()
# Ensure that the process is terminated
process.terminate()
I also get this error in the console :-
Editor’s note: Text is auto-extracted by an AI (ie: may not be 100% correct)
libpostproc 57. 3.100/57. 3.100
Input #6, matroska, webm, from 'fd:':
Metadata:
encoder: Chrome
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0(eng): Video: h264 (Constrained Baseline), yuv420p(tv, bt789, progressive), 1920x1080, SAR 1:1 DAR 16:9, 30
.30 fps, 56 tbr, 1k tbn (default)
Output #8, mp4, to /tmp/recording_d367d8d3-60de-483e-bf29-1849841b82f3.mp4':
Metadata:
encoder: Lavf60.16.100
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1920x1080 [
SAR 1:1 DAR 16:9], q=2-31, 38.30 fps, 56 tbr, 16k tbn (default)
Stream mapping:
Stream #0:0> #0:6 (copy)
Client disconnected: 127.0.0.189 bitrate=3374.0kbits/s speed=0.999x
[matroska, webm @ 8x61e88988f3c0]
9493864 (0x98dd68)
[out#0/mp4 @ 0x61e8898cc488] video:9263kB audio:0kB subtitle:0kB other streams: 9kB global headers: 8kB muxing overhead: 0
.143342%
size= 9276kB time=00:00:21.85 bitrate=3476.8kbits/s speed=1.08x
INFO:
connection closed
INFO: 127.0.0.1:52546 "POST /api/process HTTP/1.1" 200 ок
5