在做视频直播的开发时,前端通常会使用后台提供 CDN 厂商的拉流地址,但有时可能会需要前端自己创建本地直播服务器。
创建本地直播服务器也让我们前端开发可以更好地了解直播流,比如 hls 分片格式等。
创建本地直播服务器有这样两种比较简单的方法,分别是用 nginx 和 node 做服务器,建议使用第二种方法。 关于第二种方法,我提供了一个 Demo 放在 Gitee 上,Demo地址。
1、nginx-rtmp-win32
nginx-rtmp-win32 其实是对 nginx-rtmp-module 的封装,但是 nginx-rtmp-module 在 Windows10 上面使用 过于复杂,nginx-rtmp-win32 提供了一个已经嵌入 rtmp 模块的 nginx 版本,设置 nginx 的 conf 之后,启动 nginx 即可使用。
nginx.conf 内容示例
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;
index index.html index.htm;
}
}
}
启动 nginx 后使用 ffmpeg 推流
# rtmp 推流地址 `rtmp://127.0.0.1:1935/live/home`
# hls 推流地址 `rtmp://127.0.0.1:1935/hls/home`
# rtmp 拉流地址 `rtmp://127.0.0.1:1935/live/home`
# hls 拉流地址 `http://localhost:8388/hls/home.m3u8`
# rtmp 推流, -stream_loop -1 代表循环推流
ffmpeg -stream_loop -1 -re -i .\ik.mp4 -c copy -f flv rtmp://127.0.0.1:1935/live/home
# hls 推流
ffmpeg -stream_loop -1 -re -i .\ik.mp4 -c copy -f flv rtmp://127.0.0.1:1935/hls/home
hls 推流与本地分片
使用 hls 推流,我们可以在本地目录看到不断更新的 m3u8 文件,不断生成的 ts 文件,这些文件会被浏览器不断请求,推流结束之后这些本地文件就会被删除。
2、Node-Media-Server
Node-Media-Server 是 nginx-rtmp-win32 同一作者的作品,基于 Node 实现的直播服务器。
安装 node-media-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: '*'
},
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();
启动后推流
准备好本地 MP4 文件,安装好 ffmpeg,使用 ffmpeg 来循环推流,可以同时获得多种协议的拉流地址。
ffmpeg -stream_loop -1 -re -i .\ik.mp4 -c copy -f flv rtmp://localhost/live/home
## 拉流地址,home 可以改为其他字符串
# rtmp://localhost/live/home
# http://localhost:8000/live/home.flv
# http://localhost:8000/live/home/index.m3u8
# http://localhost:8000/live/home/index.mpd
根据以上代码的配置,推流之后,可以同时支持四种拉流地址。此时,就可以在本地同时看到 ts 文件和 m4s 文件分片。
可以通过 VLC 或 xgplayer 验证拉流地址是否正确,如图所示。