N
N
Nikita Kornilov2021-11-20 23:34:30
JavaScript
Nikita Kornilov, 2021-11-20 23:34:30

How to send a Media Stream over the network and save to a file on the server?

I record video in the browser to a stream using the MediaStream and MediaRecorder classes, after which I send it to the Node.js server via WebSockets. The code looks something like this:

const stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: { facingMode: "user" } });
const socket = new WebSocket("ws://127.0.0.1:3000/ws");

mediaRecorder = new MediaRecorder(stream, { mimeType: "video/mp4" });
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.start(500);

function handleDataAvailable(event) {
  if (event.data.size > 0) socket.send(event.data);
}

setTimeout(() => mediaRecorder.stop(), 5000);

The server receives messages with objects of the form <Buffer 00 00 01 ...>. The question is how to combine all this data and save it into a single video file in .mp4 format?

I'm trying to figure out how to do this, but I'm clearly missing something. Tried through fs.writeFile(), but through it it is impossible to combine and save all buffers. Also tried through fs.createWriteStream(), but the final file does not open.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question