RTMP 使用的默认端口是 1935
1 大纲
- 搭建 RTMP 服务器
- 使用 OBS 推流
- 使用 VLC 拉流
2 搭建 RTMP 服务器
此处我们使用 Nginx 来搭建服务
- 下载 Nginx 源码并编译
- 安装 Nginx 的 rtmp 扩展模块
- 修改 Nginx 配置并启动服务
2.1 下载 Nginx 源码并编译
#version: nginx-1.19.1.tar.gz
#download: http://nginx.org/download/
tar -zxvf /mnt/share/nginx-1.19.1.tar.gz -C .
cd nginx-1.19.1/
git clone https://github.com/arut/nginx-rtmp-module.git
# 使用 --add-module 来指定安装
./configure --prefix=/root/rtmp-server/nginx-1.19.1/_install --add-module=/root/rtmp-server/nginx-1.19.1/nginx-rtmp-module/
make && make install
# 启动和停止
./_install/sbin/nginx
./_install/sbin/nginx -s stop
- 编译之前要保证自己的编译环境有
pcre pcre-devel zlib zlib-devel openssl openssl-devel
的依赖
2.2 安装 Nginx 的 rtmp 扩展模块
- 下载扩展包
git clone https://github.com/arut/nginx-rtmp-module.git
- 编译 Nginx 的时候使用
--add-module=
来指定扩展包的路径 - 配置文件修改扩展包的路径
location /stat.xsl
- Gentoo 安装的时候只需添加 RTMP 的
USE
即可
2.3 修改 Nginx 配置并启动服务
用法:
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1# ./_install/sbin/nginx -h
nginx version: nginx/1.19.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /root/rtmp-server/nginx-1.19.1/_install/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1# ls _install/
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1# ls _install/conf/
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1#
- 可以使用
-c
来指定配置文件
范例:
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1# cat /usr/local/nginx/conf/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 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;
}
#wzy
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx-rtmp-module-master/;
}
#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;
}
# 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;
# }
#}
}
# wzy
rtmp {
server {
listen 1935;
chunk_size 4096;
application vod {
play /wzy/;
}
application live {
live on;
record off;
}
}
}
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1#
修改:
# 如果提示 403 错误
user root;
...
http {
...
server {
...
# wzy
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /root/rtmp-server/nginx-1.19.1/nginx-rtmp-module;
}
}
}
...
# wzy
rtmp {
server {
listen 1935;
chunk_size 4096;
# 点播: /wzy/xxx.mp4
application vod {
play /wzy/;
}
# 直播
application live {
live on;
record off;
}
}
}
启动:
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1# ./_install/sbin/nginx
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1# ps aux | grep nginx
root 31036 0.0 0.0 39684 908 ? Ss 17:41 0:00 nginx: master process ./_install/sbin/nginx
root 31037 0.0 0.0 44516 3824 ? S 17:41 0:00 nginx: worker process
root 31039 0.0 0.0 11464 1036 pts/1 S+ 17:41 0:00 grep --color=auto nginx
root@wzy-VirtualBox:~/rtmp-server/nginx-1.19.1#
- welcome
- live[stat]
3 使用 OBS 推流
- OBS Studio 可以使用腾讯电脑管家下载也可以自己百度下载
- 打开 OBS->设置->推流,服务选自定义,地址填
rtmp://192.168.16.223/live
- 选择媒体源,点击开始推流,目前我自己测试 OBS 使用的是 MP4 格式,不支持 4k,不支持 h264
- OBS push
4 使用 VLC 拉流
- 打开 VLC->媒体->打开网络串流->网络,地址填
rtmp://192.168.16.223/live
- vlc pull
5 RTMP 点播
- 将 MP4 文件放置到
/wzy/
路径下 - 打开 VLC->媒体->打开网络串流->网络,地址填
rtmp://192.168.16.223:1935/vod/aaa.mp4
- vlc vod
- vod[stat]
#rtmpdump# #RTMP# #OBS# #推流# #Nginx# #VLC# #点播#
交叉编译のrtmpdump