場景
Windows上搭建Nginx RTMP服務器並使用FFmpeg實現本地視頻推流:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/120868728
在上面實現的是對本地視頻的推流,要對接海康威視攝像頭獲取視頻流並推流和預覽。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
1、使用Nginx搭建RTMP服務器
流程和上面搭建的時候一致。完整nginx的配置再記錄一遍
#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 live { live on; } application hls { live on; hls on; hls_path temp/hls; hls_fragment 8s; } } } 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 110; 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; # } #} }
2、啟動nginx后,在FFmpeg.exe目錄中再新建bat啟動腳本
ffmpeg.exe -i "rtsp://admin:admin123456@攝像頭的ip:554/h264/ch1/main/av_stream" -vcodec copy -acodec copy -f flv rtmp://localhost:8822/hls/badao pause
腳本說明:
海康威視攝像頭,
它的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。
3、雙擊腳本啟動FFmpeg實現攝像頭推流
4、然后打開VLC播放器-媒體-打開網絡串流
輸入地址
http://127.0.0.1:81/hls/badao.m3u8