分享

Nginx+ffmpeg+ckplayer海康监控摄像头在web页面播放RTSP转RTMP

 WindySky 2018-03-05
  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14. rtmp {  
  15.   
  16.     server {  
  17.   
  18.         listen 1935;  
  19.   
  20.         chunk_size 4000;  
  21.   
  22.         # TV mode: one publisher, many subscribers  
  23.         application mylive {  
  24.   
  25.             # enable live streaming  
  26.             live on;  
  27.   
  28.         }  
  29.     }  
  30. }  
  31.   
  32. http {  
  33.     include       mime.types;  
  34.     default_type  application/octet-stream;  
  35.   
  36.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  37.     #                  '$status $body_bytes_sent "$http_referer" '  
  38.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  39.   
  40.     #access_log  logs/access.log  main;  
  41.   
  42.     sendfile        on;  
  43.     #tcp_nopush     on;  
  44.   
  45.     #keepalive_timeout  0;  
  46.     keepalive_timeout  65;  
  47.   
  48.     #gzip  on;  
  49.   
  50.     server {  
  51.         listen       8080;  
  52.         server_name  localhost;  
  53.   
  54.         #charset koi8-r;  
  55.   
  56.         #access_log  logs/host.access.log  main;  
  57.   
  58.         location / {  
  59.             root   html;  
  60.             index  index.html index.htm;  
  61.         }  
  62.   
  63.         #error_page  404              /404.html;  
  64.   
  65.         # redirect server error pages to the static page /50x.html  
  66.         #  
  67.         error_page   500 502 503 504  /50x.html;  
  68.         location = /50x.html {  
  69.             root   html;  
  70.         }  
  71.   
  72.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  73.         #  
  74.         #location ~ \.php$ {  
  75.         #    proxy_pass   http://127.0.0.1;  
  76.         #}  
  77.   
  78.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  79.         #  
  80.         #location ~ \.php$ {  
  81.         #    root           html;  
  82.         #    fastcgi_pass   127.0.0.1:9000;  
  83.         #    fastcgi_index  index.php;  
  84.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  85.         #    include        fastcgi_params;  
  86.         #}  
  87.   
  88.         # deny access to .htaccess files, if Apache's document root  
  89.         # concurs with nginx's one  
  90.         #  
  91.         #location ~ /\.ht {  
  92.         #    deny  all;  
  93.         #}  
  94.     }  
  95.   
  96.   
  97.     # another virtual host using mix of IP-, name-, and port-based configuration  
  98.     #  
  99.     #server {  
  100.     #    listen       8000;  
  101.     #    listen       somename:8080;  
  102.     #    server_name  somename  alias  another.alias;  
  103.   
  104.     #    location / {  
  105.     #        root   html;  
  106.     #        index  index.html index.htm;  
  107.     #    }  
  108.     #}  
  109.   
  110.   
  111.     # HTTPS server  
  112.     #  
  113.     #server {  
  114.     #    listen       443 ssl;  
  115.     #    server_name  localhost;  
  116.   
  117.     #    ssl_certificate      cert.pem;  
  118.     #    ssl_certificate_key  cert.key;  
  119.   
  120.     #    ssl_session_cache    shared:SSL:1m;  
  121.     #    ssl_session_timeout  5m;  
  122.   
  123.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  124.     #    ssl_prefer_server_ciphers  on;  
  125.   
  126.     #    location / {  
  127.     #        root   html;  
  128.     #        index  index.html index.htm;  
  129.     #    }  
  130.     #}  
  131.   
  132. }  

第四步启动服务器,测试

  1. 1、启动:  
  2.   
  3. C:\nginx-1.10.2>start nginx-rtmp  
  4.   
  5. 或  
  6.   
  7. C:\nginx-1.10.2>nginx-rtmp.exe  
  8.   
  9. 注:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。  
  10.   
  11. 2、停止:  
  12.   
  13. C:\nginx-1.10.2>nginx-rtmp.exe -s stop  
  14.   
  15. 或  
  16.   
  17. C:\nginx-1.10.2>nginx-rtmp.exe -s quit  
  18.   
  19. 注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。  
  20.   
  21. 3、重新载入Nginx:  
  22.   
  23. C:\nginx-1.10.2>nginx-rtmp.exe -s reload  
  24.   
  25. 当配置信息修改,需要重新载入这些配置时使用此命令。  
1:运行cmd命令,启动nginx-rtmp.exe服务

(start nginx-rtmp.exe:启动nginx服务,然后看进程中是否有nginx-rtmp.exe)

 2.打开网页输入:localhost或者127.0.0.1本机ip加端口8080

(显示一下界面说明启动成功)



第五步FFMpeg推流

1:海康的摄像头提供rtsp流地址,格式如下 rtsp://admin:123456@192.168.1.64:554/Streaming/channels/101,想了解这个rtsp的格式是怎么拼的,网上有很多资料,不过得找最新的格式,我就被旧的格式给整了半天。
2:用ffmpeg开始推流,必须要下载个ffmpeg工具,地址:http://download.csdn.net/download/liuwen718/10237295
3:进入cmd,如图所示进入到你解压对应的位置。
4:执行命令:ffmpeg -re  -rtsp_transport tcp -i "rtsp://admin:123456@192.168.1.64:554/Streaming/channels/101" -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://192.168.1.84:1935/mylive/test2"
解释:-re  -rtsp_transport tcp 这个参数必须要加不然会推流失败,就是这个参数害得我多弄了半天。
rtmp://192.168.1.84:1935/mylive/test2 这个就是刚才部署的nginx了 ,mylive是在conf配置文件里面配置的,可以修改,
test2可以随便修改。
执行成功如图:
这个时候就可以访问rtmp流了。
5:常用参数说明:
主要参数:
 i 设定输入流 
 f 设定输出格式 
 ss 开始时间 
视频参数: 

b 设定视频流量,默认为200Kbit/s-r 设定帧速率,默认为25 
 s 设定画面的宽与高-aspect 设定画面的比例 
 vn 不处理视频-vcodec 设定视频编解码器,未设定时则使用与输入流相同的编解码器 
音频参数: 
 ar 设定采样率 
 ac 设定声音的Channel数 
 acodec 设定声音编解码器,未设定时则使用与输入流相同的编解码器an 不处理音频

第六步用ckplayer播放

这个可以去官网下载一个最新版的ckplayer程序,按照他们提供的方式来做就行了,这里我提供一个我做测试用的程序包,
下载之后要用iis部署,如果是java就用java的方式部署就行:http://download.csdn.net/download/liuwen718/10237352
下载之后解压,修改index.html中的rtmp地址就行。
如果是用的iis部署,部署成功之后在浏览器上输入:http://localhost:端口/Index1.html就可以播放了。

总结

总算是弄懂这套流程了,在这个过程找了各种资料也越到各种麻烦所幸都解决。最后还有一个遗留问题就是网络问题,
你的摄像头是在局域网内的话是很顺利,如果是访问公网摄像头的rtsp在用ffmpeg推的时候会莫名的停掉,又只能继
续执行命令进行推流,这个问题还有待解决。




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多