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;
# 轉推騰訊雲
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拉攝像頭的視訊流
注:若不加
-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 的情況;
如果id = none 的情況在 打開解碼器的時候肯定會報錯,是以在 執行 avformat_find_stream_info 的時候有必要判斷一下視訊資訊的有效性!
在 avformat_open_input打開流位址之前 強制ffmpeg 以tcp傳輸 ,可以解決上面傳回 none的情況。
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.
[[email protected] 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)檢測是否推送到騰訊雲
如果推送成功則可以在流管理界面的狀态看到直播中的字樣。
點選操作中的測試則可以看到攝像頭的直播畫面如下: