安装带rtmp的nginx
前提:需要yum update、wget、tar、gcc、pcre、pcre-devel、openssl
1.下载软件:
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.17.8.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
2.解压软件:
tar -zxvf nginx-1.17.8.tar.gz
unzip master.zip
cd nginx-1.17.8
3.安装nginx:
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
make && make install
4.启动nginx:
service nginx start
5.设置开机启动(可不要)
wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
chmod +x /etc/init.d/nginx
update-rc.d nginx defaults
配置nginx使用RTMP
vi /usr/local/nginx/conf/nginx.conf
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
}
}
}
安装ffmpeg
1.安装yasm:http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
2.安装nasm2.13.0:https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/nasm-2.13.03.tar.gz
3.yum install bzip2
4.安装x264:https://code.videolan.org/videolan/x264/-/archive/master/x264-master.tar.bz2
./configure --enable-static --enable-shared
5.安装SDL2: http://www.libsdl.org/release/SDL2-2.0.10.tar.gz
6.安装FFmpeg: http://www.ffmpeg.org/releases/ffmpeg-4.1.5.tar.gz
./configure --enable-shared --enable-ffplay --enable-gpl --enable-libx264
make && make install
配置hsl
vi /usr/local/nginx/conf/nginx.conf
http.server下添加:
location /hls {
# CORS setup
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
# allow CORS preflight requests
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp/;
add_header Cache-Control no-cache;
}
rtmp.server下添加:
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
推送视频及访问
1.推送rtmp
ffmpeg -re -i your_source.mp4 -vcodec copy -f flv rtmp://localhost:1935/live/test
rtmp://192.168.0.199:1935/live/source
ffplay rtmp://localhost:1935/live/source
2.推送hsl
ffmpeg -re -i czyyjx.mp4 -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -f flv rtmp://localhost:1935/live/source
ffmpeg -re -i czyyjx.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:1935/hls/source
http://192.168.0.199/hls/source.m3u8
相关问题
1. 解决ffmpeg执行报错“ffmpeg: error while loading shared libraries: libx264.so.138: cannot open shared object file: No such file or directory”的问题
cd ffmpeg
ldd ffmpeg
find /usr -name 'libx264.so.138' ({path})
export LD_LIBRARY_PATH={path}
2.防火墙设置:
(/etc/sysconfig/iptables)
yum install iptables-services
systemctl enable iptables
systemctl start iptables
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 1935 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
service iptables save
service iptables restart