W
W
Wasya UK2019-01-30 18:14:59
Node.js
Wasya UK, 2019-01-30 18:14:59

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()

On the client I accept:
<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>

But I get just a picture from the beginning of the stream, and in poor quality.
What's the right way to do things like this? I was looking for modules, but I didn’t find one that could simply connect to rtmp and use express server. Where can I read about how to do such things correctly? Are there any modules to control the broadcast, for example: turn on the capture to the RAM, then the broadcast, and then connect through the server, and transmit it through socket.io as it arrives?
Is it possible to make structures like this?
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 question

Ask a Question

731 491 924 answers to any question