I’am using NodeJS server to display a html page which has webcam option. Once user visited to my NodeJS server, it will serve html page. User can allow webcam option and see webcam view on the page. In the backend, I send webcam stream (byte array) using socket.io. I receive byte array successfully in backend with the help of socket.io. BUT MY PROBLEM IS, I can’t pipe this byte array to the ffmpeg spawn process. I don’t know how to properly pipe this data to the ffmpeg. Once it done, all my problem will be solved. On the other side, I have node-media-server as RTMP server to publish this stream to VLC player and other devices. Kindly help me to complete this task. I will attach all my code to this question. Kindly run this in your environment and answer the question.
MY HTML PAGE
<!DOCTYPE html>
<html>
<head>
<title>Camera</title>
<style>
#container {
margin: 0px auto;
width: 500px;
height: 375px;
border: 10px #333 solid;
}
#video {
width: 500px;
height: 375px;
background-color: #666;
}
</style>
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"
integrity="sha384-2huaZvOR9iDzHqslqwpR87isEmrfxqyWOF7hr7BY6KG0+hVKLoEXMPUJw3ynWuhO"
crossorigin="anonymous"></script>
</head>
<body>
<div id="container">
<video height="1080" width="1024" autoplay="true" id="video">
</video>
<script>
const socket = io('http://localhost:8080/');
var video = document.getElementById("video");
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true, audio:true })
.then(function (stream) {
const recorder = new MediaRecorder(stream);
recorder.ondataavailable = event => {
socket.emit('VideoStream', event.data);
};
recorder.start(1000);
video.srcObject = stream;
}).catch(function (error) {
console.log("Something went wrong!");
});
}
</script>
</body>
</html>
FFMPEG IMPLEMENTATION
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const path = require('node:path');
const { spawn } = require('node:child_process');
let cmd = spawn('ffmpeg.exe', [
'-c:v', 'copy', '-preset', 'ultrafast', '-tune', 'zerolatency',
'-c:a', 'aac', '-strict', '-2', '-ar', '44100', '-b:a', '64k',
'-y',
'-use_wallclock_as_timestamps', '1',
'-async', '1',
'-flush_packets', '1',
'-rtbufsize', '1000',
'-bufsize', '1000',
'-f', 'flv',
'-i','-',
'rtmp://localhost:1935',
]);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + 'index.html'));
});
io.on('connection', (socket) => {
socket.on("VideoStream", (data) =>{
cmd.stdin.write(data);
});
});
server.listen(8080, () => {
console.log('listening on *:8080');
});
```
**NODE MEDIA SERVER IMPLEMENTATION**
```
const NodeMediaServer = require('node-media-server');
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60
},
http: {
port: 8000,
allow_origin: '*'
}
};
var nms = new NodeMediaServer(config)
nms.run();
```
Thanesh Prabaghan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.