- 安装ningx
#下载nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz
#解压nignx
tar -zxvf nginx-1.12.2.tar.gz#切换到nginx中cd nginx-1.12.2
#生成配置文件,将上述下载的文件配置到configure中
./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2t --add-module=../nginx-rtmp-module-1.2.1
#编译程序
make
make install
#查看nginx模块
nginx -V
- 推流到nginx
ffmpeg -re -fflags +genpts -f v4l2 -i /dev/video0 -an -c:v libx264 -b:v 500k -preset fast -f flv rtmp://192.168.1.167:1935/live/test
推流 hls直播:
ffmpeg -re -fflags +genpts -stream_loop -1 -i test1.flv -c:v libx264 -c:a libfdk_aac -vf: scale=-2:720 -b:v 1024k -bsf:v h264_mp4toannexb -preset slow -f flv rtmp://127.0.0.1:1935/hls/test1
播放:
ffplay http://192.168.1.230/hls/test1.m3u8
ffplay rtmp://192.168.1.230:1935/live/test1
ffplay rtmp://192.168.1.230:1935/vod/pikachu.mp4
- 配置文件
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/log/nginx/nginx.pid;
events {worker_connections 1024;
}
rtmp {server {listen 1935;chunk_size 4096;application vod {play /root/Videos/rtmp/vod;}application live {live on;#直播模式 }application hls {live on;#直播模式hls on; #这个参数把直播服务器改造成实时回放服务器。wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。hls_path /root/Videos/rtmp/hls/; #切片视频文件存放位置。hls_fragment 10s; #每个视频切片的时长。hls_playlist_length 60s; #总共可以回看的事件,这里设置的是1分钟。hls_continuous on; #连续模式。hls_cleanup on; #对多余的切片进行删除。hls_nested on; #嵌套模式。# disable consuming the stream from nginx as rtmpdeny play all;}}
}
http {access_log /var/log/nginx/access.log;include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server { listen 80;server_name localhost;location /stat {rtmp_stat all;rtmp_stat_stylesheet stat.xsl;}location /stat.xsl {root /data/python/software/nginx-rtmp/nginx-rtmp-module-1.2.1;}location /hls {# Disable cacheadd_header Cache-Control no-cache;# CORS setupadd_header 'Access-Control-Allow-Origin' '*' always;add_header 'Access-Control-Expose-Headers' 'Content-Length';# allow CORS preflight requestsif ($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 /root/Videos/rtmp;}location /vod {root /root/Videos/rtmp;}location / {root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
}