nginx+ffmpeg+rtmp实现rtmp点播/直播和hls直播
目录
环境:
# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
创建文件夹:mkdir -p /data/software
安装工具包:yum install -y wget git bzip2
安装基础依赖包:yum -y install gcc pcre-devel openssl openssl-devel make
关闭防火墙:systemctl stop firewalld
VLC工具下载地址:https://www.videolan.org/
一、ffmpeg安装:
1. 安装nasm依赖
yum装的版本太低,所以手动安装,装yasm依赖也行,这里装的是nasm
cd /data/software/ wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.gz tar -zxvf nasm-2.14.02.tar.gz cd nasm-2.14.02 ./configure make && make install
2. 安装h264编码支持
要不然推流的时候会报错 Unknown encoder 'libx264'
cd /data/software/ git clone git://git.videolan.org/x264.git cd x264/ ./configure --enable-static --enable-shared make && make install
将 libx264 库的地址添加到 /etc/ld.so.conf 文件中,要不然在推流使用h264编码时可能会报 ffmpeg: error while loading shared libraries: libx264.so.157: cannot open shared object file: No such file or directory :
vim /etc/ld.so.conf
添加 /usr/local/lib/ 到 文件末尾。
然后执行 ldconfig 生效。
3. 源码安装ffmpeg:
cd /data/software/ wget https://ffmpeg.org/releases/ffmpeg-4.1.3.tar.bz2 tar -jxvf ffmpeg-4.1.3.tar.bz2 cd ffmpeg-4.1.3 ./configure --enable-libx264 --enable-gpl make && make install
二、nginx+rmtp模块安装
cd /data/software/
下载安装包:
nginx源码包:wget http://nginx.org/download/nginx-1.14.0.tar.gz
rtmp模块:git clone https://github.com/arut/nginx-rtmp-module.git
开始安装:
tar -zxvf nginx-1.14.0.tar.gz cd nginx-1.14.0 ./configure --prefix=/data/app/nginx --add-module=/data/software/nginx-rtmp-module/ make && make install
二、配置服务
1. 配置rtmp点播服务:
vim /data/app/nginx/conf/nginx.conf
在与http同级的地方添加下面内容:
rtmp {
server {
listen 8888; # 监听端口
chunk_size 4096; # 数据传输块大小
application vod {
play /data/resources/videos; # 视频文件存储位置
}
}
}
保存退出。
启动nginx:/data/app/nginx/sbin/nginx
新建目录 /data/resources/videos 并放一个测试的视频到/data/resources/videos 目录,我这里是 /data/resources/videos/test.mp4
访问地址:rtmp://192.168.0.109:8888/vod/test.mp4
2. 配置直播服务
添加一个直播应用节点在nginx配置文件中,添加好后如下:
rtmp {
server {
listen 8888; # 监听端口
chunk_size 4096; # 数据传输块大小
application vod {
play /data/resources/videos; # 视频文件存储位置
}
application hls {
live on;
hls on;
hls_path /data/resources/hls; # hls文件存放位置
}
}
}
重新加载配置文件生效:/data/app/nginx/sbin/nginx -s reload
使用ffmpeg进行推流:
ffmpeg -re -i /data/resources/videos/test.mp4 -f flv rtmp://192.168.0.109:8888/hls/test
播放地址为:rtmp://192.168.0.109:8888/hls/test
3. 配置http播放
vim /data/app/nginx/conf/nginx.conf
在server同级的地方加入新的server节点,内容如下:
server {
listen 9999;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /data/resources/hls;
add_header Cache-Control no-cache;
}
}
重新加载配置文件生效:/data/app/nginx/sbin/nginx -s reload
使用ffmpeg进行推流:
ffmpeg -re -i /data/resources/videos/test.mp4 -vcodec h264 -acodec copy -f flv rtmp://192.168.0.109:8888/hls/test
rtmp播放地址为:rtmp://192.168.0.109:8888/hls/test
hls播放地址为: http://192.168.0.109:9999/hls/test.m3u8
现在环境已经搭好了,我们也可以通过此方式将一个只支持 rtmp的地址转为hls方式,这样就可以使得在手机上也能够播放,如下:
宜宾电视台新闻综合频道:rtmp://218.89.69.211/live/ch1
使用ffmpeg转换为hls推流:
ffmpeg -re -i rtmp://218.89.69.211/live/ch1 -vcodec libx264 -acodec copy -f flv rtmp://192.168.0.109:8888/hls/test
转换后的播放地址仍然为如下:
rtmp播放地址为:rtmp://192.168.0.109:8888/hls/test
hls播放地址为: http://192.168.0.109:9999/hls/test.m3u8
4. 创建网页测试在不同设备上播放
将附件下载解压到/data/resources/目录下:web.tar
将index.html文件里的 hls.loadSource('http://192.168.0.109:9999/hls/test.m3u8'); 地址修改为你的地址。
index.html 页面内容如下:
<!DOCTYPE html>
<html>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<head>
<title>直播页面</title>
</head>
<body>
<p>正在直播...</p>
<p>适用于PC端</p>
<script src="hls.min.js"></script>
<video id="video"></video>
<script>
if(Hls.isSupported()) {
var video = document.getElementById('video');
var hls = new Hls();
hls.loadSource('http://192.168.0.109:9999/hls/test.m3u8');
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
video.play();
});
}
</script>
<p>适用于移动端</p>
<video class="vjs-tech"
controls="controls"
autoplay="autoplay"
x-webkit-airplay="true"
x5-video-player-fullscreen="true"
preload="auto" playsinline="true"
webkit-playsinline x5-video-player-typ="h5">
<source type="application/x-mpegURL" src="http://192.168.0.109:9999/hls/test.m3u8">
</video>
</body>
</html>
配置nginx:
添加一个location:
location /play {
alias /data/resources/web;
index index.html;
}
重新加载配置文件生效:/data/app/nginx/sbin/nginx -s reload
网页播放地址为:http://192.168.0.109:9999/play/
nginx.conf 配置文件最终版:
worker_processes auto;
events {
use epoll;
worker_connections 65535;
}
rtmp {
server {
listen 8888; # 监听端口
chunk_size 4096; # 数据传输块大小
application vod {
play /data/resources/videos; # 视频文件存储位置
}
application hls {
live on;
hls on;
hls_path /data/resources/hls; # hls文件存放位置
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 9999;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /data/resources/hls;
add_header Cache-Control no-cache;
}
location /play {
alias /data/resources/web;
index index.html;
}
}
}
共有 0 条评论