同时同步捕获多个 RTSP 流

同时同步捕获多个 RTSP 流

我想同时从最多六个 IP 摄像头捕获 h264 rtsp 流并将它们保存到单独的文件中。这些摄像头连接到千兆交换机,该交换机连接到我的电脑。输出视频必须同步才能进行进一步处理:视频需要具有相同的持续时间、开始时间、停止时间和帧速率。我正尝试使用 ffmpeg 来完成这项任务。

显然,我的 IP 摄像机输出的帧速率是可变的。为了同时录制全高清的多个流,我计划先复制流,然后再重新编码为恒定的帧速率。这要求捕获的流带有每个帧的时间戳。

我第一次尝试录制 10 秒的流媒体是:

ffmpeg -loglevel verbose -i rtsp://192.168.1.101 -i rtsp://192.168.1.102 \
-map 0:v -vcodec copy -an -t 10 -y out1.mp4 \
-map 1:v -vcodec copy -an -t 10 -y out2.mp4  

这应该会将两个流的秒数保存到 out1.mp4 和 out2.mp4,复制视频,丢弃音频,如果文件存在则覆盖。然而,结果是两个视频的长度不同,在 5 到 7 秒之间,并且它们不同步。此外,视频每三秒左右就会冻结一次。(详细)日志输出内容如下:

ffmpeg version N-50911-g9efcfbe Copyright (c) 2000-2013 the FFmpeg developers
  built on Mar 13 2013 21:26:48 with gcc 4.7.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libg
sm --enable-libilbc --enable-libmp3lame --enable-libopencore-amrnb --enable-libo
pencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-li
bschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-lib
twolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enabl
e-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
  libavutil      52. 19.100 / 52. 19.100
  libavcodec     55.  0.100 / 55.  0.100
  libavformat    55.  0.100 / 55.  0.100
  libavdevice    54.  4.100 / 54.  4.100
  libavfilter     3. 45.103 /  3. 45.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
[rtsp @ 027698e0] SDP:
v=0
o=StreamingServer 3331435948 1116907222000 IN IP4 192.168.1.101
s=h264.mp4
c=IN IP4 0.0.0.0
t=0 0
a=control:*
m=video 0 RTP/AVP 96
a=control:trackID=0
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z0KAKNoB4AiTAWoEBARgQAAF9eE
AAC+vCve+Eg==,aM48gA==
m=application 0 RTP/AVP 107
a=control:trackID=2
a=rtpmap:107 vnd.onvif.metadata/90000


[h264 @ 0276c520] Current profile doesn't provide more RBSP data in PPS, skippin
g
[rtsp @ 027698e0] Estimating duration from bitrate, this may be inaccurate
Input #0, rtsp, from 'rtsp://192.168.1.101':
  Metadata:
    title           : h264.mp4
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: h264 (Baseline), yuv420p, 1920x1088 [SAR 1:1 DAR 30:17],
 30.17 tbr, 90k tbn, 180k tbc
    Stream #0:1: Data: none
[rtsp @ 027682a0] SDP:
v=0
o=StreamingServer 3331435948 1116907222000 IN IP4 192.168.1.102
s=h264.mp4
c=IN IP4 0.0.0.0
t=0 0
a=control:*
m=video 0 RTP/AVP 96
a=control:trackID=0
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z0KAKNoB4AiTAWoEBARgQAB9AAA
DARlAve+EgA==,aM48gA==
m=application 0 RTP/AVP 107
a=control:trackID=2
a=rtpmap:107 vnd.onvif.metadata/90000


[h264 @ 0495b420] Current profile doesn't provide more RBSP data in PPS, skippin
g
[rtsp @ 027682a0] Estimating duration from bitrate, this may be inaccurate
Input #1, rtsp, from 'rtsp://192.168.1.102':
  Metadata:
    title           : h264.mp4
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #1:0: Video: h264 (Baseline), yuv420p, 1920x1088 [SAR 1:1 DAR 30:17],
 30.17 tbr, 90k tbn, 180k tbc
    Stream #1:1: Data: none
Output #0, mp4, to 'out1.mp4':
  Metadata:
    title           : h264.mp4
    encoder         : Lavf55.0.100
    Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1088 [SAR 1
:1 DAR 30:17], q=2-31, 90k tbn, 90k tbc
Output #1, mp4, to 'out2.mp4':
  Metadata:
    title           : h264.mp4
    encoder         : Lavf55.0.100
    Stream #1:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1088 [SAR 1
:1 DAR 30:17], q=2-31, 90k tbn, 90k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #1:0 (copy)
Press [q] to stop, [?] for help
[NULL @ 0495b420] Current profile doesn't provide more RBSP data in PPS, skippin
g
[NULL @ 0276c520] RTP: missed 362 packets
[NULL @ 0276c520] Current profile doesn't provide more RBSP data in PPS, skippin
g
    Last message repeated 1 times
frame=   44 fps=0.0 q=-1.0 q=-1.0 size=     560kB time=00:00:03.24 bitrate=1412.
frame=   60 fps= 59 q=-1.0 q=-1.0 size=     622kB time=00:00:03.77 bitrate=1350.
Current profile doesn't provide more RBSP data in PPS, skipping
[NULL @ 0276c520] Current profile doesn't provide more RBSP data in PPS, skippin
g
frame=   75 fps= 49 q=-1.0 q=-1.0 size=     813kB time=00:00:04.27 bitrate=1559.
frame=   90 fps= 45 q=-1.0 q=-1.0 size=     877kB time=00:00:04.76 bitrate=1507.
Current profile doesn't provide more RBSP data in PPS, skipping
[NULL @ 0276c520] Current profile doesn't provide more RBSP data in PPS, skippin
g
frame=  106 fps= 42 q=-1.0 q=-1.0 size=    1080kB time=00:00:05.29 bitrate=1669.
frame=  121 fps= 40 q=-1.0 q=-1.0 size=    1142kB time=00:00:05.79 bitrate=1613.
Current profile doesn't provide more RBSP data in PPS, skipping
[NULL @ 0276c520] Current profile doesn't provide more RBSP data in PPS, skippin
g
frame=  136 fps= 38 q=-1.0 q=-1.0 size=    1327kB time=00:00:06.29 bitrate=1727.
Current profile doesn't provide more RBSP data in PPS, skipping
frame=  151 fps= 37 q=-1.0 q=-1.0 size=    1382kB time=00:00:06.78 bitrate=1667.
Current profile doesn't provide more RBSP data in PPS, skipping
frame=  166 fps= 36 q=-1.0 q=-1.0 size=    1574kB time=00:00:07.28 bitrate=1770.
Current profile doesn't provide more RBSP data in PPS, skipping
frame=  182 fps= 36 q=-1.0 q=-1.0 size=    1643kB time=00:00:07.81 bitrate=1722.
Current profile doesn't provide more RBSP data in PPS, skipping
frame=  183 fps= 33 q=-1.0 q=-1.0 size=    1647kB time=00:00:06.49 bitrate=2078.
Current profile doesn't provide more RBSP data in PPS, skipping
frame=  183 fps= 30 q=-1.0 q=-1.0 size=    1647kB time=00:00:06.98 bitrate=1931.
frame=  183 fps= 28 q=-1.0 q=-1.0 size=    1647kB time=00:00:07.48 bitrate=1803.
Current profile doesn't provide more RBSP data in PPS, skipping
frame=  183 fps= 26 q=-1.0 q=-1.0 size=    1647kB time=00:00:07.98 bitrate=1690.
No more output streams to write to, finishing.
frame=  183 fps= 26 q=-1.0 Lq=-1.0 size=    1649kB time=00:00:08.01 bitrate=1685
.8kbits/s
video:3803kB audio:0kB subtitle:0 global headers:0kB muxing overhead -56.630223%

我怎样才能同步记录这些流?

答案1

我也遇到了这个问题。

异步是由于与摄像机的连接时间不同造成的。

我正在研究的解决方案是使用 ffserver 连续捕获流并在本地主机上广播它们,然后您可以使用 ffmpeg 访问流而不会产生连接延迟。

相关内容