In the development of live video, the front-end usually uses the background to provide the pull stream address of CDN manufacturers, but sometimes the front-end may need to create a local live video server by itself.
Creating a local live server also allows our front-end development to better understand live streams, such as HLS sharding format.
There are two simple methods to create a local broadcast server, namely using Nginx and Node as servers. The second method is recommended. For the second method, I provided a Demo to put on Gitee, the Demo address.
- nginx-rtmp-win32
- Node-Media-Server
1, nginx – RTMP – win32
Nginx-rtmp-win32 is a package of nginx-rtmp-module, but nginx-rtmp-module is too complicated to use in Windows10. Nginx-rtmp-win32 provides a version of nginx embedded in the RTMP module. After setting the Nginx conf, you can start nginx.
Example of nginx.conf content
worker_processes 2;
#pid logs/nginx.pid;
events {
worker_connections 8192;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
}
application hls{
live on;
hls on;
hls_path E:/nginx-rtmp-win32-1.2.1/temp/hls;
hls_fragment 5s; }}}http {
#include /nginx/conf/naxsi_core.rules;
include mime.types;
default_type application/octet-stream;
sendfile off;
#tcp_nopush on;
server_names_hash_bucket_size 128;
## Start: Timeouts ##
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 30;
send_timeout 10;
keepalive_requests 10;
## End: Timeouts ##
#gzip on;
server {
listen 8388;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias E:/nginx-rtmp-win32-1.2.1/temp/hls;
expires -1;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
location / {
root html;
indexindex.html index.htm; }}}Copy the code
After starting nginx, use FFmPEG to push the stream
#RTMP push flow address ` RTMP: / / 127.0.0.1:1935 / live/home `
#Push HLS flow address ` RTMP: / / 127.0.0.1:1935 / HLS/home `
#RTMP pull flow address ` RTMP: / / 127.0.0.1:1935 / live/home `
#HLS flow address ` http://localhost:8388/hls/home.m3u8 `
#RTMP push stream, -stream_loop-1 stands for loop push streamFfmpeg stream_loop - 1 - re - i. \ ik FLV mp4 - c copy - f RTMP: / / 127.0.0.1:1935 / live/home
#Push HLS flowFfmpeg stream_loop - 1 - re - i. \ ik FLV mp4 - c copy - f RTMP: / / 127.0.0.1:1935 / HLS/homeCopy the code
HLS streams and local fragments
With the HLS push stream, we can see the m3U8 files that are constantly updated in the local directory, the TS files that are constantly generated, these files are constantly requested by the browser, and these local files are deleted when the push stream ends.
2, the Node – Media – Server
Node-media-server is a live broadcast Server based on the same author of nginx-rtmp-win32.
After installing Node-media-Server, execute the following code to start the live server.
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.mediaroot: './media'.allow_origin: The '*'
},
trans: {
ffmpeg: 'E:/Program Files/FFMpeg/bin/ffmpeg.exe'.tasks: [{app: 'live'.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();
Copy the code
Start after push flow
Prepare local MP4 files, install FFMPEG, use FFMPEG to cycle push stream, you can get a variety of protocols at the same time pull stream address.
ffmpeg -stream_loop -1 -re -i .\ik.mp4 -c copy -f flv rtmp://localhost/live/home
## pull stream address, home can be changed to other string
# rtmp://localhost/live/home
# http://localhost:8000/live/home.flv
# http://localhost:8000/live/home/index.m3u8
# http://localhost:8000/live/home/index.mpd
Copy the code
According to the configuration of the above code, after pushing the stream, four pull stream addresses can be supported simultaneously. In this case, you can see both the TS file and the M4S file shards locally.
You can verify that the pull stream address is correct by using VLC or XgPlayer, as shown in the figure.