环境准备

  1. 下载FFMPEG后,配置环境变量即可。校验命令:ffmpeg -version
  2. 下载ngxin-gryphon,再下载nginx-rpst-module-master,将目录移动到ngxin-gryphon目录下,改名为nginx-rtmp-module,最后配置一下文件nginx-win.conf(配置放在下方)
  3. vlc播放器:用于播放网络串流或推流。
  4. 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 fragmentstypes {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 modelocation / {#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 headeradd_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP headeradd_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命令去推流,是比较可靠的。

流媒体服务器FFMPEG转码流摄像头取流相关推荐

  1. 国标流媒体服务器可以接入海康摄像头吗?是否会有断流问题?

    近年来,国内视频监控应用发展迅猛,系统接入规模不断扩大,涌现了大量平台提供商,平台提供商的接入协议各不相同,终端制造商需要给每款终端维护提供各种不同平台的软件版本,造成了极大的资源浪费.各地视频大规模 ...

  2. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器前端源码重构(四)- webpack + video.js 打造流媒体服务器前端

    EasyPlayer播放器是基于EasyDSS流媒体服务器视频强大的后台管理能力,提供视频点播和直播播放能力的强大播放载体.流畅稳定的播放性能,集广告植入.数据监测等功能于一身,为开发者提供端到端的一 ...

  3. 萤石开放平台的二次开发——摄像头取流至微博直播平台

    1.申请开放平台账号注册 1.1登录萤石开放平台主页:https://open.ys7.com/cn/s/index 1.2点击首页左上方的"注册"入口 1.3进入注册页面:htt ...

  4. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器前端源码重构(三)- webpack + vue + AdminLTE 多页面引入 element-ui

    EasyDSS流媒体解决方案总体可划分成三个部分:前端视频源设备(PC.手机.摄像机)流媒体数据获取并即时回传.流媒体服务器端直播和录像与回放.客户端直播播放与录像检索回放.前端推流我们使用跨平台的R ...

  5. 萤石开放平台——摄像头取流有问题怎么排查?

    监控地址可以用我们在线工具测试:EZOPEN播放协议 直播地址可以我们在线工具测试:https://open.ys7.com/jssdk/live.html,也可以通过VLC播放器(https://w ...

  6. 流媒体服务器文件传输,mStream - 从任何地方流式传输音乐的个人流媒体服务器...

    mStream是一款免费的开源和跨平台个人音乐流媒体服务器,可让您在所有设备之间同步和流式传输音乐. 它由一个用NodeJS编写的轻量级音乐流媒体服务器组成; 您可以使用它将音乐从家用电脑流式传输到任 ...

  7. 家庭自建流媒体服务器,如何创建自己的“家庭媒体流服务器”使用Plex与FreeNAS - 第3部分...

    每个人都有自己的电影,歌曲,视频歌曲,图片等的收藏.他们很多都想知道我们如何流到我们所有的家庭设备. 这里是我们可以使用Plex Media Server通过LAN网络在我们的家中使用智能电视,iPa ...

  8. 基于S5pv210流媒体服务器的实现之网络摄像头(by liukun321 咕唧咕唧)

    这里仅介绍流媒体服务器端的实现思路,及编码注意问题,不会贴代码的具体实现.直接入正题先介绍一下系统硬件框架: 服务器端连接PC机用VLC播放如下图: 服务器端应用程序可以分为图像采集.视频硬件编码压缩 ...

  9. EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器前端源码重构(二)-webpack + vue + AdminLTE 多页面提取共用文件, 优化编译时间

    接上篇 在上一篇博客中, 我们白手起家, 从零搭建了 webpack + vue + AdminLTE 多页面脚手架. 代码在这里: easydss-web-src , 我为第一篇博客建立了单独的分支 ...

最新文章

  1. CentOS7关闭防火墙
  2. emui10是基于linux的么,华为EMUI10在意义上还能称作安卓吗?谷歌以后的安卓版本,华为是否有必要去更新?...
  3. 数据库存储引擎学习总结
  4. tf.train.examle函数
  5. 台湾大学林轩田机器学习基石课程学习笔记12 -- Nonlinear Transformation
  6. linux怎么运行ofstream,ofstream和ifstream详细用法---转
  7. C语言中的EOF符号常量
  8. 自定义idea archetype
  9. vuex的命名空间有哪些_Vue 3 带来的 Vuex 的替代方案
  10. POJ 2226 二分图最小覆盖
  11. uctf-杂项题目分析
  12. 数据分享 | 高光谱遥感影像数据集汇总
  13. python3 打包exe 加密_python代码加密之生成exe可执行文件
  14. 如何下载离线地图数据(支持谷歌、百度、高德等所有地图源)
  15. echarts 3D 柱状图
  16. 非负数的和与个数c语言编程,C语言习题中,编程实现从键盘任意输入20个整数,统计非负数个数,并计算非负数之和...
  17. 闲鱼双11全链路营销体系初体验
  18. rabbitMQ队列解绑
  19. 支撑压力(刀哥指标)
  20. iPhone13ProMax和华为Mate50Pro区别 哪个好

热门文章

  1. AVFoundation开发秘籍笔记-02播放和录制音频
  2. 【STM32篇】驱动MXL90614红外测温模块
  3. jmeter之取样器(HTTP请求、调试取样器)
  4. 基于html5的矢量图绘制方法研究,基于HTML5和WebGL的三维地形可视化方法研究
  5. VIVO可能在某一段时间内手机充电数据线上并没有ID脚
  6. 网络舆论舆情监测怎么做的系统技术解决办法
  7. MySQL 去除字符串中的括号以及括号内的内容
  8. Android Studio - 北极狐 | 2020.3.1 补丁 2 现已推出
  9. 43.深度解密四十三:网络营销推广实战中的“辅助营销工具”大全
  10. trace-event