Use node-media-server to build a simple streaming media server (windows environment)

Record some processes of using node-media-server. The environment of this article is windows. This article is for beginners.

Things used: nodeJs, ffmpeg, node-media-server .

Here's a little bit (please point out if I'm wrong):

node-media-server is used as a streaming media server, you can understand it as a transfer station, which is used to convert the stream format or do some operations on the video stream and push the stream address to the outside.

ffmpeg is used as a streaming tool to push the video or video stream you need to convert to the streaming server.

Pulling streams means pulling video streams from streaming media servers, and viewers can watch them by pulling the streaming addresses published by streaming media servers. If you play it with a video player, you are pulling the stream.

Install

node-media-server is developed based on node.Js, so it needs to be installed using npm first.

npm install node-media-server

Write and run node-media-server

New app.js. And write the following code, then run app.js

const NodeMediaServer= require('node-media-server');
const config = {
    rtmp: {
        port: 1935,
        chunk_size: 60000,
        gop_cache: true,
        ping: 60,
        ping_timeout: 30
    },
    http: {
        port: 8000,
        allow_origin: '*',
    }
};

var nms = new NodeMediaServer(config)
nms.run();

You can visit the localhost:8000/admin address to access the management interface of node-media-server. Screenshot below:

Push the address using FFmpeg commands.

push video file

When executing the following code, you need to start node-media-server.

ffmpeg -re -i ./video.mp4 -c copy -f flv rtmp://localhost:1935/live/STREAM_NAME

The above command will generate two stream addresses after passing through node-media-server. A kind of rtmp. A kind of flv. The former can be played on a computer, and the latter can be played on a mobile phone and a computer. The rtmp address is the command address in FFmpeg

The flv address is: 

http://localhost:8000/live/STREAM_NAME.flv

push rtsp stream

To push rtsp (camera video stream), just click the above ./video.mp4. During the test, the blogger found that pushing the rtsp stream will either cause a green screen or packet loss, especially when combined with hls, the packet loss rate is greater, so it is not recommended to use the command to push the rtsp stream.

Convert to hls stream format

One thing needs to be paid attention to when transferring hls streams. You need to specify the mediaroot parameter. Although node-media-server has a default value inside, it is recommended to set it once. Then use the following configuration.

const NodeMediaServer= require('node-media-server');
const ff = require('ffmpeg');
const config = {
    rtmp: {
        port: 1935,
        chunk_size: 60000,
        gop_cache: true,
        ping: 60,
        ping_timeout: 30
    },
    http: {
        port: 8979,
        mediaroot: './media/', // 建议写
        allow_origin: '*',
    },
    trans: { // 这里参数是trans参数,不是relay参数,relay参数中配置hls无效
        ffmpeg: './bin/ffmpeg.exe',//指明FFmpeg位置
        tasks: [
            {
                app: 'live',
                ac: 'acc',
                vc: 'libx264',
                hls: true,
                hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]',
                dash: true,
                dashFlags: '[f=dash:window_size=3:extra_window_size=5]'
            }
        ]
    }
};

var nms = new NodeMediaServer(config)
nms.run();

After starting the above code, use FFmpeg to push the stream. After a while, you will find that a live/STREAM_NAME folder is generated under the directory pointed to by mediaroot, which stores m3u8 files. Since the m3u8 file needs to be generated first, if it is a streaming camera, there will be a relatively large delay.

The m3u8 address is:

http://localhost:8000/live/STREAM_NAME/index.m3u8

If you find that there is a problem with m3u8 playback, try removing the ac and vc parameters. When the landlord is actually using it, these two parameters are not used.

Use code to stream to rtsp

For some users, it may be necessary to push the rtsp camera video stream for cross-end preview. The blogger here recommends using this method. This method does not need to use cmd to execute FFmpeg commands, and the delay has been tested by the blogger as 3s (intranet, since there is no external network address, the external network is not very clear). There is less latency.

code show as below:

const NodeMediaServer= require('node-media-server');
const config = {
    rtmp: {
        port: 1935,
        chunk_size: 60000,
        gop_cache: true,
        ping: 60,
        ping_timeout: 30
    },
    http: {
        port: 8979,
        mediaroot: './media/',
        allow_origin: '*',
    },
   relay: {
        ffmpeg: './bin/ffmpeg.exe',
        tasks: [
            {
                app: 'live',
                mode: 'static',
                edge: 'rtsp://admin:****@192.168.4.167:554/Streaming/Channels/101',//rtsp
                name: 'technology',
                rtsp_transport : 'tcp', //['udp', 'tcp', 'udp_multicast', 'http']
            }
        ]
    },
};

var nms = new NodeMediaServer(config)
nms.run();

This method can generate two video streams, one rtmp and one flv.

 

Summarize

Because the blogger builds the streaming media server mainly to transfer the camera in the project, but the JAVA colleagues have dealt with it before and found that it will consume a lot of hardware resources, but the blogger used node-media-server but did not find that it consumes much resources. In the end, you still need to test it in detail. And for camera transfer, in order to avoid unnecessary performance consumption, the host intends to let the server start the streaming media transfer function only when the observer initiates a preview (use the code to transfer rtsp streaming), and when the observer closes the preview, the streaming media function will be turned off immediately. This avoids unnecessary consumption of performance. After all, it can be operated directly through the code, which is more convenient and can reduce the pressure on the server.

Guess you like

Origin blog.csdn.net/QiZi_Zpl/article/details/107259935