Nginx&FFmpeg&nginx-http-flv-module直播推流教程
环境
Ubuntu20.04
进入root模式
sudo su
apt-get update
apt-get upgrade
安装依赖包
apt-get install g++ openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev make yasm libx264-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libxext-dev libxfixes-dev
Nginx配置
1.获取nginx软件包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
2.解压
tar -xzvf nginx-1.18.0.tar.gz
3.重命名
mv nginx-1.18.0 nginx
4.移动
mv nginx/ /usr/local/
Nginx-http-flv-module配置
1.解压到对应目录下
unzip nginx-http-flv-module-master.zip
mv nginx-http-flv-module-master /usr/local/nginx/nginx-http-flv-module
2.配置nginx编译环境
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx/nginx-http-flv-module
3.Nginx编译&安装
make
make install
4.设置Nginx环境变量
vi /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
source /etc/profile
安装FFMPEG
1.解压ffmpeg软件包
unzip FFmpeg-master.zip
2.移动到相应目录下
mv FFmpeg-master /usr/local/ffmpeg
3.配置编译环境
./configure --enable-shared --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264
4.编译&安装
make -j6 //根据自己条件分配核心数目
make install
5.增加安装目录的动态链接库
export LD_LIBRARY_PATH=/usr/local/ffmpeg/lib:$LD_LIBRARY_PATH
6.设置ffmpeg环境变量
vi /etc/profile
export PATH=$PATH:/usr/local/ffmpeg
source /etc/profile
7.查看
ldd ffmpeg
ffmpeg -version
若出现报错
ffmpeg: error while loading shared libraries: libavdevice.so
可通过一下操作
gedit /etc/ld.so.conf
/usr/local/ffmpeg/lib/
ldconfig
ffmpeg
修改Nginx配置文件
1.将下方文本替换/usr/local/nginx/conf/nginx.conf中的文本
#user nobody;
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 80; #http-flv的拉流端口
# http-flv的相关配置
location /live {
flv_live on; #打开HTTP播放FLV直播流功能
chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回复
add_header 'Access-Control-Allow-Origin' '*'; #添加额外的HTTP头
add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的HTTP头
}
}
}
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
timeout 15s;
drop_idle_publisher 15s;
log_interval 5s; #log模块在access.log中记录日志的间隔时间,对调试非常有用
log_size 1m; #log模块用来记录日志的缓冲区大小
server {
listen 1935;
#ffmpeg推流的application
application app {
live on;
allow publish 127.0.0.1;
allow play all;
record off;
meta copy;
gop_cache on; #打开GOP缓存,减少首屏等待时间
}
}
}
2.查看是否修改成功
/usr/local/nginx/sbin/nginx -t
测试
ffmpeg -re -i xxx.mp4 -vcodec copy -acodec copy -f flv "rtmp://***"