用ffmpeg+nginx+海康威視網絡攝像頭rtsp在手機端和電腦端實現直播


用ffmpeg+nginx+海康威視網絡攝像頭rtsp在手機端和電腦端實現直播

原料:海康威視攝像頭,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


#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;
}

rtmp {
server {
listen 1935;

application myapp {
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
}
}


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;
}

#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;
# }
#}

}
保存完配置文件之后,啟動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中添加下面內容:


location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}

2.由於延遲比較大,根據前輩們的經驗,在nginx配置文件中,進行修改

application hls {
live on;
hls on;
hls_path /data/misc/hls;
hls_fragment 1s;
hls_playlist_length 3s;
} <pre name="code" class="html">
到這里准備工作就已經做好了,下面進入正題:
在Linux服務器上執行ffmpeg命令,實現轉碼以及推流


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,此時我們就可以看到攝像頭的監控畫面。


如果您覺得本文章對您有幫助,感謝您的支持!


————————————————
版權聲明:本文為CSDN博主「zfgogo」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/zfgogo/java/article/details/52439849

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM