阿里云CentOS7下nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器
nginx安装rtmp模块参考上篇文章内容:https://www.r1989.com/linux-1097.html
nginx通过rtmp模块提供rtmp服务, ffmpeg推送一个rtmp流到nginx, 客户端通过访问nginx来收看实时视频流。HLS也是差不多的原理,只是最终客户端是通过HTTP协议来访问的,但是ffmpeg推送流仍然是rtmp的。
配置nginx.conf
添加rtmp配置,针对hls需添加location配置
location /hls
{
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /data;
add_header Cache-Control no-cache;
}
#.....
rtmp {
server {
listen 1935;
chunk_size 4000;
application rtmplive {
live on;
max_connections 1024;
}
application hls {
live on;
hls on;
hls_path /data/hls;
hls_fragment 5s;
}
}
}
保存重启nginx,netstat -ltn
检查1935端口是否启用
推流
- 推送到rtmplive
ffmpeg -re -i /data/test.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/rtmplive/rtmp
- 推送到hls
ffmpeg -re -i /data/test.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://ip:1935/hls/stream
客户端播放
使用VLC播放器测试
- rtmp流播放:
rtmp://ip:1935/rtmplive/rtmp
- hls流播放:
http://ip:8080/hls/stream.m3u8
网页播放
<link href="http://vjs.zencdn.net/5.19/video-js.min.css" rel="stylesheet">
<script src="http://vjs.zencdn.net/5.19/video.min.js"></script>
</head>
<body>
<video
id="my-player"
class="video-js"
controls
preload="auto"
poster="//vjs.zencdn.net/v/oceans.png"
data-setup='{}'>
<source src='rtmp://ip:1935/rtmplive/rtmp' type='rtmp/flv'/>
</p>
</video>
<script type="text/javascript">
var player = videojs('my-player');
var options = {};
var player = videojs('my-player', options, function onPlayerReady() {
videojs.log('Your player is ready!');
// In this context, `this` is the player that was created by Video.js.
this.play();
// How about an event listener?
this.on('ended', function() {
videojs.log('Awww...over so soon?!');
});
});
</script>
obs rtmp推流设置(以rtmp流播放:rtmp://ip:1935/rtmplive/rtmp为例):
URL:rtmp://ip:1935/rtmplive
流名称:rtmp
OBS Studio 免费,开源软件,用于实时流媒体直播和录制。
支持的功能包括:
编码使用H264(X264)和AAC。
支持英特尔快速同步视频(QSV)和NVENC。
无限数量的场景和来源。
直播 RTMP 流媒体 to Twitch, YouTube, DailyMotion, Hitbox 和更多。
MP4或者FLV文件输出。
基于GPU的高性能游戏流媒体游戏捕获。
支持DirectShow捕获设备(摄像头、采集卡、等)。
Windows 8 高速监控捕捉支持。
双线性或lanczos3重采样。