环境准备
- 下载FFMPEG后,配置环境变量即可。校验命令:ffmpeg -version
- 下载ngxin-gryphon,再下载nginx-rpst-module-master,将目录移动到ngxin-gryphon目录下,改名为nginx-rtmp-module,最后配置一下文件nginx-win.conf(配置放在下方)
- vlc播放器:用于播放网络串流或推流。
- EasyDarwin:下载打开exe即可。用于开启本地rtsp端口。
配置文件
nginx-win.conf
#user nobody;
# multiple workers works !
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 8192;
# max value 32768, nginx recycling connections+registry optimization =
# this.value * 20 = max concurrent connections currently tested with one worker
# C1000K should be possible depending there is enough ram/cpu power
# multi_accept on;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application live { #rtmp地址
live on;
}
application hls { #如果是rtmp流这个可以不要
live on;
# 开启hls切片
hls on;
# m3u8地址
hls_path html/hls;
# 一个切片多少秒
hls_fragment 8s;
# on_play http://localhost:8081/auth; #权限钩子,该自定义接口返回true才推流
# on_publish http://localhost:8081/auth; #权限钩子,该自定义接口返回true才推流
# on_done http://localhost:8081/auth; #权限钩子,该自定义接口返回true才推流
}
}
}
http {
#include /nginx/conf/naxsi_core.rules;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr:$remote_port - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
# # loadbalancing PHP
# upstream myLoadBalancer {
# server 127.0.0.1:9001 weight=1 fail_timeout=5;
# server 127.0.0.1:9002 weight=1 fail_timeout=5;
# server 127.0.0.1:9003 weight=1 fail_timeout=5;
# server 127.0.0.1:9004 weight=1 fail_timeout=5;
# server 127.0.0.1:9005 weight=1 fail_timeout=5;
# server 127.0.0.1:9006 weight=1 fail_timeout=5;
# server 127.0.0.1:9007 weight=1 fail_timeout=5;
# server 127.0.0.1:9008 weight=1 fail_timeout=5;
# server 127.0.0.1:9009 weight=1 fail_timeout=5;
# server 127.0.0.1:9010 weight=1 fail_timeout=5;
# least_conn;
# }
sendfile off;
#tcp_nopush on;
server_names_hash_bucket_size 128;
## Start: Timeouts ##
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 30;
send_timeout 10;
keepalive_requests 10;
## End: Timeouts ##
#gzip on;
# 配置流媒体
server {
listen 2025;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
location /hls { #如果是rtmp流,这个配置可以不要
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
expires -1;
add_header Access-Control-Allow-Origin *;
}
#charset koi8-r;
#access_log logs/host.access.log main;
## Caching Static Files, put before first location
#location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
# expires 14d;
# add_header Vary Accept-Encoding;
#}
# For Naxsi remove the single # line for learn mode, or the ## lines for full WAF mode
location / {
#include /nginx/conf/mysite.rules; # see also http block naxsi include line
##SecRulesEnabled;
##DeniedUrl "/RequestDenied";
##CheckRule "$SQL >= 8" BLOCK;
##CheckRule "$RFI >= 8" BLOCK;
##CheckRule "$TRAVERSAL >= 4" BLOCK;
##CheckRule "$XSS >= 8" BLOCK;
root html;
index index.html index.htm;
}
# For Naxsi remove the ## lines for full WAF mode, redirect location block used by naxsi
##location /RequestDenied {
## return 412;
##}
## Lua examples !
# location /robots.txt {
# rewrite_by_lua '
# if ngx.var.http_host ~= "localhost" then
# return ngx.exec("/robots_disallow.txt");
# end
# ';
# }
#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; # single backend process
# fastcgi_pass myLoadBalancer; # or multiple, see example above
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$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 spdy;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_prefer_server_ciphers On;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:ECDH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!eNULL:!MD5:!DSS:!EXP:!ADH:!LOW:!MEDIUM;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
启动关闭nginx
启动 nginx.exe -c conf/nginx-win.conf
关闭 nginx -s stop -c conf/nginx-win.conf
推流,拉流,播放
有摄像头的可以按照摄像头的操作地址,获取摄像头的rtsp流,比如大华摄像头的格式就是:
rtsp://用户名:密码@摄像头IP:554/cam/realmonitor?channel=1&subtype=0
如果没有流,我们可以用ffmpeg将本地视频推成rtsp流,模仿摄像头的流进行测试。
推流
FFMPEG推流,需要下载一个EasyDarwin软件,开启本地的554端口,要不然是推流不成功的。
启动EasyDarwin后,打开cmd,执行命令:
ffmpeg -re -i ./333.mp4 -vcodec libx264 -acodec aac -f rtsp rtsp://localhost:554/aaa
将本地的333.MP4视频推到本地的554端口上,生成rtsp流。我们拿到流地址:
rtsp://localhost:554/aaa
如果是摄像头,则直接通电,按照摄像头格式获取摄像头的流地址,大华摄像头如下取流:
rtsp://用户名:密码@摄像头IP:554/cam/realmonitor?channel=1&subtype=0
FFMPEG 部分命令
-re 原帧率
-vcodec 视频编码
-acodec 音频编码
-f flv 以flv容器
-ac 音频通道
-ar 音频采样率
-strict -2 实验参数,配合acc编码默认安装的ffmpeg有aac编码,使用它就无须安装别的音频编码库
-s: scale 分辨率
-preset:v 编码质量ultrafast,superfast, veryfast, faster, fast, medium, slow, slower, veryslow
-analyzeduration 1000000 分析时间
-tune:v zerolatency 零延时编码,画面不稳,容易中断
-sc_threshold 499 场景更改监测,值越高画面越流畅
-fflags nobuffer 不要缓冲
转流(此处介绍rtmp和http格式的)
rtmp:
流推好后,可以用vlc工具测试一下,能不能播放流。vlc可以播放rtsp还有rtmp格式的流(网上有播放教程,此处省略)。
流能正常播放,说明流是可以获取到的。下一步,要将rtsp流转成rtmp流,执行ffmpeg命令,将rtsp流转成rtmp流。
软转:使用cpu转码,依赖于cpu和内存,消耗资源很大,消耗率差不多80%。
ffmpeg -re -rtsp_transport tcp -i
“rtsp://用户名:密码@摄像头IP:554/cam/realmonitor?channel=1&subtype=0”
-fflags nobuffer -sc_threshold 499 -tune:v zerolatency -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 2304x1296 -q 10 “rtmp://localhost:1935/live/dahua”
硬转:使用GPU转码,用显卡的处理器,cpu和内存降低很多,大概消耗10%
ffmpeg -re -rtsp_transport tcp -i
“rtsp://用户名:密码@摄像头IP:554/cam/realmonitor?channel=1&subtype=0”
-codec:v h264_qsv -vprofile baseline -acodec aac -ar 8000 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 “rtmp://localhost:1935/live/dahua”
其中dahua是自定义的名称,将流转到ngxin流媒体的1935端口的live服务下。
这个时候我们就可以用vlc或者ffmpeg命令播放这个流,如果播放成功,前端就可以调用这个地址进行播放(前端页面播放rtmp需要有flash插件环境)。
ffplay “rtmp://localhost:1935/live/dahua live=1”
http
使用http方式的流就不再需要ngxin_gryphon了,需要另外一个http相关的ngxin流媒体nginx-flv,可以从网上下载nginx-flv-file-master的包,然后启动该ngxin。
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 2025;
location /live {
flv_live on;
chunked_transfer_encoding on;
add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header 'Cache-Control' 'no-cache';
}
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 html/nginx-http-flv-module/;
}
location /control {
rtmp_control all;
}
location /rtmp-publisher {
root html/nginx-http-flv-module/test;
}
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html/nginx-http-flv-module/test/www;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|png|js|css)$ {
root html/nginx-http-flv-module/test/www/;
}
#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;
# }
#}
}
rtmp {
server {
listen 1935;
chunk_size 8000;
application liveapp {
live on;
# on_publish http://localhost:8081/auth;
# on_publish_done http://localhost:8081/auth;
}
}
}
启动后,和上面rtmp流步骤类似,执行转流命令即可:
软转:
ffmpeg -re -rtsp_transport tcp -i
“rtsp://用户名:密码@摄像头IP:554/cam/realmonitor?channel=1&subtype=0”
-fflags nobuffer -sc_threshold 499 -tune:v zerolatency -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 2304x1296 -q 10 “rtmp://localhost:1935/liveapp/dahua”
硬转:
ffmpeg -re -rtsp_transport tcp -i
“rtsp://用户名:密码@摄像头IP:554/cam/realmonitor?channel=1&subtype=0”
-codec:v h264_qsv -vprofile baseline -acodec aac -ar 8000 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 “rtmp://localhost:1935/liveapp/dahua”
转完后,可以通过nginx的代理配置,拿到http形式的流,如:
http://localhost:2025/live?port=1935&app=liveapp&stream=dahua
补充:硬转的方式里面的配置-codec:v h264_qsv,是制定显卡的型号,如果是inter类型的显卡,就是qsv。amd和英伟达的是另外两种格式,网上搜一下就有。
总结
自己搭建流媒体需要很高配置,要不然很吃资源,具体情况还要具体分析,不能确定需要多大的资源,也不是专门搞音视频方面的。只能利用现有工具进行操作。现在浏览器很多不支持flash了,所以rtmp这种的还是不够方便,用http格式的用html的播放标签就能播放,比较方便。我机器配置转完后延时3s左右,如果ffmpeg命令不带上面的参数优化, 延时是5-9秒,上面的-fflags nobuffer -sc_threshold 499 -tune:v zerolatency都是降低延时用的。但是好像只限于软转的形式,硬转不需要带这些参数,延时差不多。
用vlc软件推流很容易断,我的机器过十秒左右就会断开,不知道为什么,开始我一直以为ffmpeg推流的命令有问题,后面才发现是vlc推流容易断,导致ffmpeg转流的时候就断了,所以我这里用ffmpeg命令去推流,是比较可靠的。