海康威视RSTP摄像头视频数据从内网读取在web用HLS播放

30 篇文章 0 订阅

环境:海康威视录像机DS-7804NB-K1/4P,Ubuntu16.04

录像机内网地址192.168.100.99,一个摄像头接在4号口(32口以下通道号从33开始),摄像头内网地址 192.168.1.64,服务器内网地址192.168.100.75,测试PC192.168.100.83

1.摄像头取rtsp流、VLC或EasyDarwin串流

从摄像头直接取RTSP流 rtsp://admin:123456@192.168.1.64/Streaming/Channels/1

从录像机取摄像头RTSP流 rtsp://admin:123456@192.168.100.99:554/h264/ch36/main/av_stream

VLC:

sudo apt-get install vlc,

媒体->流->网络->输入摄像头RTSP流->串流->地址rtsp://admin:123456@192.168.100.99:554/h264/ch36/main/av_stream->激活转码、Video-H.264+MP3(MP4)->流->端口5580,路径/hiktest/h264/ch36/main

则流网络地址为rtsp://192.168.100.75:5580/hiktest/h264/ch36/main

EasyDarwin:https://github.com/EasyDarwin/EasyDarwin/releases

下载EasyDanwin,直接解压,sudo bash start.sh

浏览器访问localhost:10008配置推流

输入rtsp://admin:123456@192.168.100.99:554/h264/ch36/main/av_stream

输出/hiktest/h264/ch36/main

则流网络地址为rtsp://192.168.100.75/hiktest/h264/ch36/main

以下步骤以EasyDanwin串流地址为例

2.nginx+ffmpeg将rtsp流转为hls流

安装nginx和rtmp模块(需更新Ubuntu源,nginx需1.14以上才能直接apt安装rtmp的mod):

sudo apt-get install software-properties-common python-software-properties
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx
sudo apt-get install libnginx-mod-rtmp
sudo mkdir /HLS
sudo mkdir /HLS/hls
sudo chmod -R 777 /HLS
sudo chmod -R 777 /HLS/hls
cd /etc/nginx
sudo vi /etc/nginx/sites-enabled/default

在http的server内部加入

        location /hls {
            #add_header 'Access-Control-Allow-Origin' '*' always;
            #add_header 'Access-Control-Expose-Headers' 'Content-Length';
            add_header Cache-Control no-cache;
            #if ($request_method = 'OPTIONS') {
            #    add_header 'Access-Control-Allow-Origin' '*';
            #    add_header 'Access-Control-Max-Age' 1728000;
            #    add_header 'Content-Type' 'text/plain charset=UTF-8';
            #    add_header 'Content-Length' 0;
            #    return 204;
            #}
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /HLS;
            expires -1;
        }

sudo nginx -s reload

ffmpeg:http://www.ffmpeg.org/download.html

直接解压,运行

./ffmpeg -rtsp_transport tcp -r 25 -i rtsp://192.168.100.75/hiktest/h264/ch36/main -fflags flush_packets -max_delay 1 -segment_time 5 -an -flags -global_header -hls_time 1 -hls_list_size 3 -hls_wrap 4 -vcodec copy -y /HLS/hls/ch36.m3u8

P.S.这里先串流是因为我直接将原RTSP流做输入时会报错,有待再查证原因;后来出现过Invalid UE golomb code-1.0、pkt->duration = 0, maybe the hls segment duration will not precise、Cannot use rename on non file protocol, this may lead to races and temporary partial files等问题,不太记得具体怎么解决的,注意apt-get install ffmpeg的版本过旧,hls_wrap要比hls_list_size大,最好用-r指定fps

3.web客户端

HTML中嵌入

<script src="https://cdn.jsdelivr.net/hls.js/latest/hls.min.js"></script>
<video id="video" style="width:100%;" controls></video>
<script>
    var video=document.getElementById('video');
    if(Hls.isSupported()){
        var video = document.getElementById('video');
        var hls = new Hls();
        hls.loadSource('http://192.168.100.75/hls/ch36.m3u8');
        hls.attachMedia(video);
        hls.on(Hls.Events.MANIFEST_PARSED,function() {
            video.play();
        });
     }
     else if(video.canPlayType('application/vnd.apple.mpegurl')){
        source=document.createElement('source');
        source.setAttribute("type","application/vnd.apple.mpegurl");
        source.setAttribute("src","http://192.168.100.75/hls/ch36.m3u8");
        video.appendChild(source);
     }
     else{
        alert("not support!");
     }
</script>

更新:

nginx在nginx.conf配置rtmp模块:

rtmp{                                                                                                                       
    server{                                                                                                                     
        listen 1935;                                                                                                            
        chunk_size 1024;                                                                                                        
        application live{                                                                                                           
            live on;                                                                                                            
        }                                                                                                                   
    }                                                                                                                   }

rtsp转rtmp:

ffmpeg -re -rtsp_transport tcp -i rtsp://admin:123456@192.168.0.99:554/h264/ch36/sub/av_stream -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:1935/live/stream

参考链接:

https://www.dreamvps.com/tutorials/hls-and-nginx-on-ubuntu/

https://www.cnblogs.com/Jim-william/p/7991928.html

http://mufool.com/2016/08/01/hls-ios-profile/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值