用ffmpeg+nginx+海康威视网络摄像头rstp在手机端和电脑端实现直播

原料:海康威视摄像头,nginx服务器,ffmpeg。

首先海康威视摄像头

它的rtsp数据流的地址为:rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream
说明:
username: 用户名。例如admin。
password: 密码。例如12345。
ip: 为设备IP。例如 192.0.0.64。
port: 端口号默认为554,若为默认可不填写。
codec:有h264、MPEG-4、mpeg4这几种。
channel: 通道号,起始为1。例如通道1,则为ch1。
subtype: 码流类型,主码流为main,辅码流为sub。

主码流:rtsp://admin:123456@172.33.23.98:554/h264/ch1/main/av_stream

子码流:rtsp://admin:123456@172.33.23.98:554/h264/ch1/sub/av_stream

然后是nginx服务器,本人是在Linux主机上安装了nginx服务器,对于nginx服务器就不多介绍了。下面是详细的安装步骤,因为考虑的访问量会比较大,所以是用源码包安装的。

下载,地址是,http://nginx.org/en/download.html,下载的是稳定版本的。

下载之后的文件时:nginx-1.10.1.tar.gz,在Linux下通过,tar -cvf nginx-1.10.1.tar.gz 命令可以进行解归档,

为了增加对rtmp的支持下载nginx-rtmp-module,地址:https://github.com/arut/nginx-rtmp-module#example-nginxconf

将该文件解压之后放到/home/user,该目录是自己随意选择的,为了方便。

就可以进入nginx1.10.1的解归档之后文件夹,执行

./configure --prefix=/usr/local/nginx  --add-module=/home/user/nginx-rtmp-module  --with-http_ssl_module  

完成之后执行:

make 

make之后执行

make install

接着就是等待一般半个小时左右,安装完成之后进行配置文件的修改,

vi /etc/local/nginx/conf/nginx.conf

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

cd /usr/local/nginx/sbin

./nginx

启动时可能会遇到端口占用的问题,因为之前nginx已经启动了,所以先把进程停止

killall -9 nginx

然后在启动nginx就不会出错了。

至此,服务器端的nginx服务器就已经搭建好了。

接着就是ffmpeg,用于rtsp协议转换成rtmp协议,并且推流到nginx服务器

首先,下载ffmpeg安装包,http://www.ffmpeg.org/download.html,点击中间那个按钮直接下载。

解压,执行安装命令,./configure make make install 具体如下:

由于名字过于复杂mv ffmpeg-0.4.9-p20051120.tar.bz2 ffmpeg 改个名

配置安装路径:./configure --enable-shared --prefix=/usr/local/ffmpeg

然后  make

最后make install完成ffmpeg源码包的安装。

为了方便起见,我们可以将ffmpeg的安装地址加入到环境变量中,修改/etc/ld.so.conf文件,在末尾加入

/usr/local/ffmpeg/lib,然后就可以在任意目录下执行ffmpeg命令。

额外配置

1.为了能够成功将视频流推送到hls上,还需要对nginx.conf配置文件进行修改,在http中添加下面内容:

[html]  view plain  copy
  1. location /hls {      
  2.                 types {      
  3.                     application/vnd.apple.mpegurl m3u8;      
  4.                     video/mp2t ts;      
  5.                 }      
  6.                 root /tmp;      
  7.                 add_header Cache-Control no-cache;      
  8.         }   

2.由于延迟比较大,根据前辈们的经验,在nginx配置文件中,进行修改

[html]  view plain  copy
  1. application hls {      
  2.                 live on;      
  3.                 hls on;      
  4.                 hls_path /data/misc/hls;    
  5.                 hls_fragment 1s;     
  6.         hls_playlist_length 3s;    
  7.             }    <pre name="code" class="html">  

 
到这里准备工作就已经做好了,下面进入正题: 

在Linux服务器上执行ffmpeg命令,实现转码以及推流

[html]  view plain  copy
  1. ffmpeg -i rtsp://admin:12345@172.33.23.98 -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 -f flv rtmp://172.16.97.29:1935/hls/test  
 

手机端,在任意的浏览器,不是自带的,输入172.16.97.29:1935/hls/test.m3u8,此时我们就可以看到摄像头的监控画面。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值