Answer the question
In order to leave comments, you need to log in
How to work with rtmp in Node.js?
I record the desktop with ffmpeg: ffmpeg -f gdigrab -framerate 25 -i desktop out.mpg
Then I stream the file: ffmpeg -re -i output.flv -c copy -f flv rtmp://localhost/live/STREAM_NAMEffmpeg -re -i output.flv -c:v libx264 -preset superfast -tune zerolatency -c:a aac -ar 44100 -f flv rtmp://localhost/live/STREAM_NAME
And through the server I send to the page:
const { NodeMediaServer } = require('node-media-server')
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 60,
ping_timeout: 30
},
http: {
port: PORT,
allow_origin: '*'
}
}
var nmcs = new NodeMediaServer(config)
nmcs.run()
<script src="https://cdn.bootcss.com/flv.js/1.5.0/flv.min.js"></script>
<video id="videoElement" allow="autoplay"></video>
<script>
if (flvjs.isSupported()) {
var videoElement = document.getElementById('videoElement');
var flvPlayer = flvjs.createPlayer({
type: 'flv',
url: 'ws://localhost:3443/live/STREAM_NAME.flv'
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
flvPlayer.play();
}
</script>
const express = require('express')
const http = require('http')
const socketIo = require('socket.io')
const ffmpeg = require('fluent-ffmpeg')
const app = express()
const server = http.createServer(app)
const io = socketIo(server, {})
const PORT = process.env.PORT || 3443
const write_command = ffmpeg.startRecord()
const stream_command = ffmpeg.startStreaming()
rtmp.connect('localhost/live/STREAM_NAME')
io.on('connection', socket => {
rtmp.on('data', data => {
socket.broadcast.emit('stream', data)
})
})
server.listen(PORT, () => {
console.log(`Server work on port: ${PORT}`)
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question