一、环境搭建
1、依赖安装
sudo apt-get install gcc
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g zlib1g-dev
sudo apt-get install libssl-dev
2、ngnix下载及安装
wget https://nginx.org/download/nginx-1.22.0.tar.gz
tar -zxvf nginx-1.22.0.tar.gz
tar解压完成之后出现nginx-1.22.0,然后在同级目录下载rtmp模块
git clone https://github.com/arut/nginx-rtmp-module
下载完成rtmp模块之后,进行安装,指定安装目录到/usr/local/nginx下
cd nginx-1.22.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_mp4_module --add-module=../nginx-rtmp-module
make
sudo make install
由于nginx本身不支持http-flv,安装http模块,在浏览器即可调用,所以需要单独下载nginx-http-flv-module,重新进行编译,编译方式按照官网说明即可。https://github.com/winshining/nginx-http-flv-module
cd nginx-1.22.0
./configure --add-module=../nginx-http-flv-module-master
make
sudo make install
查看安装结果
lab@lab-System-Product-Name:/usr/local/nginx/sbin$ ./nginx -v
nginx version: nginx/1.22.1
3、ffmpeg安装
sudo apt install ffmpeg
以上命令直接安装,或者参考其他博客,如ubuntu安装ffmpeg_ubuntu下安装ffmpeg_Eanve的博客-优快云博客
二、配置nginx
1、切换到nginx.conf目录
cd /usr/local/nginx/conf/
2、编辑nginx.conf文件,如下:
user root;
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 0;
keepalive_timeout 800;
proxy_connect_timeout 800;
proxy_read_timeout 800;
proxy_send_timeout 800;
client_max_body_size 5120m;
#gzip on;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /pop/video {
alias /var/video;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xls {
root html;
}
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头
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp/hls;
add_header 'Cache-Control' 'no-cache';
}
location /dash {
root /tmp/dash;
add_header 'Cache-Control' 'no-cache';
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;
rtmp {
out_queue 8192; #数据传输块的大小--默认
out_cork 8;
max_streams 128;
timeout 15s;
drop_idle_publisher 15s;
server {
listen 1935; #服务端口--默认
server_name rtmp;
application haikang01 {
live on;
gop_cache on; #打开GOP缓存,减少首屏等待时间
}
application hls {
live on;
hls on;
hls_fragment 25s;
hls_path /tmp/hls;
gop_cache on;
}
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
}
3、重启nginx
sudo ./nginx -s reload
1935端口是默认的推流端口,浏览器查看,http:\\localhost:8080
三、ffmpeg推流
1、推流到nginx的rtmp服务
sudo ffmpeg -i rtsp://admin:azure2023@10.150.10.20:554/h264/ch1/main/av_stream -vcodec copy -acodec copy -f flv rtmp://localhost:1935/hls/haikang01
其中rtsp为摄像头的推流地址,rtmp为nginx搭建的服务地址,注意rtmp://localhost:1935/hls/haikang01地址要和nginx中配置一致,否则会出现:rtmp://localhost:1935/hls/haikang01: Input/output error错误,出现以下界面表示推流成功
2、vlc拉流
打开VLC,媒体->打开网络串流->网络
输入rtmp://localhost:1935/hls/haikang01
或者
输入http://localhost:8080/live?port=1935&app=hls&stream=haikang01