1. 配置nginx
1.1下载nginx依赖
yum install gcc-c++ -y #nginx编译依赖 gcc 环境
yum install pcre pcre-devel -y #nginx的http模块使用pcre来解析正则表达式
yum install zlib zlib-devel -y #nginx使用zlib对http包的内容进行gzip
openssl version #nginx可能需要支持HTTPS,最好在系统中安装好openssl
yum install openssl openssl-devel -y #若提示未安装,执行命令安装
1.2配置nginx
- 各版本nignx下载网址,版本应使用大于或者等于 1.2.6
- 下载并解压nginx安装包
wget http://nginx.org/download/nginx-1.25.0.tar.gz tar -zxvf nginx-1.25.0.tar.gz
- 下载并解压nginx-http-flv-module-1.2.11
wget https://github.com/winshining/nginx-http-flv-module/archive/v1.2.11.tar.gz tar -zxvf v1.2.11.tar.gz
- 将nginx-http-flv-module添加至nginx,重新编译nginx并重新安装nginx
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx/nginx-http-flv-module-1.2.11 make&makeinstall #执行命令
- 修改nginx相关配置支持HTTP FLV(/usr/local/nginx/conf文件)
#user nobody; worker_processes 1; #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; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; server { listen 8553; server_name localhost; location / { root html; index index.html index.htm; } location /live { flv_live on; #当HTTP请求以/live结尾,匹配这儿,这个选项表示开启了flv直播播放功能 chunked_transfer_encoding on; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' '*'; } location /hls{ add_header Access-Control-Allow-Origin *; types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias /test; expires -1; } location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/nginx/nginx-http-flv-module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } rtmp { server { listen 1938; #Nginx监听的RTMP推流/拉流端口 application myapp { live on; #当推流时,RTMP路径中的APP(RTMP中一个概念)匹配myapp时,开启直播 meta off; #为了兼容网页前端的flv.js,设置为off可以避免报错 gop_cache on; #支持GOP缓存,以减少首屏时间 allow play all; #允许来自任何ip的人拉流 record off; # 添加HLS支持 hls on; hls_path /test; #/usr/local/nginx/html/hls hls_fragment 1s; } } }
1.3 启动nginx,验证是否正确
- http://ip:8553
- http://ip:8553/status
2.FFmpeg安装
- 安装nasm
# cd /usr/local # wget https://www.nasm.us/pub/nasm/releasebuilds/2.14/nasm-2.14.tar.gz # tar -zxvf nasm-2.14.tar.gz # cd nasm-2.14 # ./configure # make && make install
- 安装x264(视频流转码,必用,不然会推流失败)
# cd /usr/local # wget https://code.videolan.org/videolan/x264/-/archive/master/x264-master.zip # unzip x264-master.zip # cd x264-master # ./configure --enable-static --enable-shared # make && make install
- 安装FFmpeg,时间长耐心等待
# cd /usr/local # wget http://www.ffmpeg.org/releases/ffmpeg-4.3.tar.gz # tar ffmpeg-4.3.tar.gz # tar -zxvf nasm-2.14.tar.gz # cd ffmpeg-4.3 # ./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264 # make && make install # cd /usr/local/ffmpeg/bin/* /usr/bin/ # ffmpeg -version
3.推流
- 服务器推本地视频流命令:
ffmpeg -stream_loop -1 -re -i "/home/1.mp4" -vcodec libx264 -f flv -an rtmp://ip:1938/myapp/main
- 对应的拉流地址:
http方式:
flv:http://ip:8553/live?port=1938&app=myapp&stream=main
hls:http://ip:8553/hls/main.m3u8
rtmp方式:
rtmp://ip:1938/myapp/main
4.VUE播放实时视频流(支持flv、hls格式)
具体实现播放步骤参考:https://blog.csdn.net/weixin_43883951/article/details/131065700?spm=1001.2014.3001.5502