Linux系统下实现监控摄像头RTSP协议对接腾讯云直播

Linux系统下实现监控摄像头RTSP协议对接腾讯云直播

1.ffmpeg安装

sudo yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
yum -y install ffmpeg

2.nginx-rtmp安装和配置

安装nginx组件

yum -y install gcc openssl-devel pcre-devel zlib-devel

下载nginx-rtmp-module

//方式一:git命令下载
git clone https://github.com/arut/nginx-rtmp-module.git

//方式二:下载nginx-rtmp-module-1.2.0.tar.gz
//解压
tar -zxvf nginx-rtmp-module-1.2.0.tar.gz
//重命名
mv nginx-rtmp-module-1.2.0 nginx-rtmp-module

安装nginx

tar zxvf nginx-1.14.2.tar.gz 
cd nginx-1.14.2/

编译和安装

./configure --prefix=/usr/local/nginx  --add-module=../nginx-rtmp-module  --with-http_ssl_module 
make 
make install

3.开通腾讯云直播

(1)点击当即使用开通服务

在这里插入图片描述

(2)点击辅助工具选择地址生成器生成推流地址

在这里插入图片描述

4. nginx-rtmp配置

(1)建立nginx本地文件存放目录

mkdir -p /mnt/share/html/hls

(2)rtmp配置

vim /usr/local/nginx/conf/nginx.conf


#文件末尾追加
rtmp {

server {

    listen 1935;  #监听的端口  

    chunk_size 4000;

    # rtmp推流请求路径 
    application hls {   
        live on;
        hls on;
        # 本地保存hlv格式文件
        hls_path /mnt/share/html/hls; 
        hls_fragment 10s;
        # 转推[腾讯云](https://l.gushuji.site/tencent)
        push rtmp://106630.livepush.myqcloud.com/live/rtsptest?txSecret=b8d28c20ab68f2467727b837eefcb4a2&txTime=5F140099;
    }
  }
}

(3)检查配置是否正确

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

(4)启动nginx

/usr/local/nginx/sbin/nginx

5.ffmpeg拉摄像头的视频流

ffmpeg -rtsp_transport tcp -i "rtsp://admin:1111aaaa@172.19.20.201:554/cam/realmonitor?channel=1&subtype=0" -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 4000x3000 -q 10 -f flv "rtmp://127.0.0.1:1935/hls"

注:若不加-rtsp_transport可能会报 Could not find codec parameters for stream 0 (Video: h264, none) 错误,ffmpeg默认使用udp方式传输,在使用:avformat\_find\_stream\_info 会出现返回 codec\_id ==AV\_CODEC\_ID\_NONE 的状况;html

若是id = none 的状况在 打开解码器的时候确定会报错,因此在 执行 avformat\_find\_stream\_info 的时候有必要判断一下视频信息的有效性!nginx

在 avformat\_open\_input打开流地址以前 强制ffmpeg 以tcp传输 ,能够解决上面返回 none的状况。git

6.检测

(1) 检测ffmpeg是否从摄像头拉流到nginx-rtmp, 若是成功,能够看到:

[aac @ 0x12fb900] Queue input is backward in time0:01:59.57 bitrate=4261.0kbits/s speed=0.934x    
[flv @ 0x12e37a0] Non-monotonous DTS in output stream 0:1; previous: 119622, current: 119605; changing to 119622. This may result in incorrect timestamps in the output file.
[aac @ 0x12fb900] Queue input is backward in time0:02:14.50 bitrate=4266.0kbits/s speed=0.933x    
[flv @ 0x12e37a0] Non-monotonous DTS in output stream 0:1; previous: 134622, current: 134605; changing to 134622. This may result in incorrect timestamps in the output file.
[aac @ 0x12fb900] Queue input is backward in time0:02:29.15 bitrate=4263.7kbits/s speed=0.932x    
[flv @ 0x12e37a0] Non-monotonous DTS in output stream 0:1; previous: 149621, current: 149606; changing to 149621. This may result in incorrect timestamps in the output file.
[aac @ 0x12fb900] Queue input is backward in time0:02:44.50 bitrate=4267.9kbits/s speed=0.931x    
[flv @ 0x12e37a0] Non-monotonous DTS in output stream 0:1; previous: 164621, current: 164606; changing to 164621. This may result in incorrect timestamps in the output file.
[flv @ 0x12e37a0] Failed to update header with correct duration.ate=4267.0kbits/s speed=0.93x     
[flv @ 0x12e37a0] Failed to update header with correct filesize.
frame= 4333 fps= 23 q=-1.0 Lsize=   90306kB time=00:02:53.47 bitrate=4264.6kbits/s speed=0.931x    
video:86498kB audio:3599kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.231989%
[aac @ 0x12fb900] Qavg: 1180.000
Exiting normally, received signal 2.
[root@localhost hls]# ll
总用量 54324
-rw-r--r--. 1 nobody nobody 5484336 7月  16 07:03 live-10.ts
-rw-r--r--. 1 nobody nobody 6584324 7月  16 07:03 live-11.ts
-rw-r--r--. 1 nobody nobody 5495052 7月  16 07:03 live-12.ts
-rw-r--r--. 1 nobody nobody 5491292 7月  16 07:04 live-13.ts
-rw-r--r--. 1 nobody nobody 5482456 7月  16 07:04 live-14.ts
-rw-r--r--. 1 nobody nobody 5491104 7月  16 07:04 live-15.ts
-rw-r--r--. 1 nobody nobody 4022072 7月  16 07:04 live-16.ts
-rw-r--r--. 1 nobody nobody 5485652 7月  16 07:02 live-7.ts
-rw-r--r--. 1 nobody nobody 5487720 7月  16 07:03 live-8.ts
-rw-r--r--. 1 nobody nobody 6585452 7月  16 07:03 live-9.ts
-rw-r--r--. 1 nobody nobody     155 7月  16 07:04 live.m3u8

(2)检测是否推送到腾讯云

若是推送成功则能够在流管理界面的状态看到直播中的字样。github

在这里插入图片描述

点击操做中的测试则能够看到摄像头的直播画面以下:vim

在这里插入图片描述

相关文章
相关标签/搜索