跳到主要内容

nginx-rtmp-module

https://nginx.org/en/download.html https://github.com/arut/nginx-rtmp-module.git

市面上优秀的流媒体服务器解决方案有很多,比如SRS,Red5,EasyDarwin,nginx-rtmp,live555,mediasoup

上传到linux服务器,然后解压压缩包

在/opt 下新建文件夹存放压缩包。比如 mkdir nginx-rtmp,然后将文件上传文件夹下解压缩。

$tar xvf nginx-1.20.1.tar.gz $unzip nginx-rtmp-module-master.zip

3.创建build目录

$cd nginx-1.20.1 $mkdir build

4.config & make & make install


yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel


#配置nginx
#--prefix 指定安装的目录
#/usr/local/nginx 是安装目录,不能和自己下载的文件目录重了
#./configure --prefix=/usr/local/nginx

#带ssl stub_status模块 添加strem模块 –with-stream,这样就能传输tcp协议了
#http_stub_status_module 状态监控
#http_ssl_module 配置https
#stream 配置tcp得转发
#http_gzip_static_module 压缩
#http_sub_module 替换请求           
--with-http_v2_module \              
--with-http_flv_module \              
--with-http_mp4_module \


(1)./configure --prefix=/opt/nginx-rtmp/nginx-1.20.2/build --add-module=/opt/nginx-rtmp/nginx-rtmp-module-1.2.2/

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gunzip_module --with-http_gzip_static_module --add-module=../nginx-rtmp-module

(2)make
(3)make install


补充:config过程中错误及错误处理

./configure: error: the HTTP rewrite module requires the PCRE library.

yum -y install pcre-devel

./configure: error: SSL modules require the OpenSSL library.

yum -y install openssl openssl-devel

./configure如果输出not found等不需要管,直接进行make;./configure是对nginx进行配置。

5.修改nginx配置文件

进入/opt/nginx-rtmp/nginx-1.20.2/build/conf/nginx.conf,增加rtmp服务器配置


#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;
}


rtmp_auto_push on;

rtmp {
server {
listen 1935;
chunk_size 4096;

# live on
application rtmp_live {
live on;
#hls on; #这个参数把直播服务器改造成实时回放服务器。
#wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。
#hls_path ./sbin/html; #切片视频文件存放位置。
#hls_fragment 10s; #每个视频切片的时长
#hls_playlist_length 60s; #总共可以回看的时间,这里设置的是1分钟。
#hls_continuous on; #连续模式。
#hls_cleanup on; #对多余的切片进行删除。
#hls_nested on; #嵌套模式。
}

#play videos
application vod{
play /opt/data/videos; #视频文件
}
}
}
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 65;

#gzip on;

server {
listen 80;
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 /stat { #第二处添加的location字段。

rtmp_stat all;

rtmp_stat_stylesheet stat.xsl;

}

location /stat.xsl { #第二处添加的location字段。

root /usr/local/src/nginx-rtmp-module/;

}

location /live { #这里也是需要添加的字段。

types {

application /vnd.apple.mpegurl m3u8;

video/mp2t ts;

}

alias /opt/data/video/hls;

expires -1;

add_header Cache-Control no-cache;

}
location ~ \.flv$ { #flv 支持 需要安装对应模块
root /opt/video/vod;
}

location ~ \.mp4$ { #MP4 支持 需要安装对应模块
root /opt/video/vod/;
mp4;
mp4_buffer_size 10m;
mp4_max_buffer_size 200m;
}



# 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;
# }
#}
}

nginx报错:nginx: [emerg] unknown directive in /etc/nginx/conf.d/test.conf:4 解决: 第四行出现了 tab 空格 , 换成正常的空格即可

6.启动 nginx

cd /opt/nginx-rtmp/nginx-1.20.2/build

sudo ./sbin/nginx

7.ffmpeg推流

ffmpeg -stream_loop -1 -v verbose -re -i sample_yuancore_720p.mp4 -f flv rtmp://192.168.41.115:1935/rtmp_live/mystream

-stream_loop -1 无限循环推流 -v verbose 详细日志 -re 本来的码率推流 -c:a copy -c:v copy 省略 按照原来编码推流

8.VLC拉流(直播、点播)

直播:rtmp://192.168.41.115:1935/rtmp_live/mystream 点播:rtmp://192.168.41.115:1935/rtmp_play/01.mp4