FFmpeg编码海康DVR无有效音轨视频时卡顿的问题求助
海康DVR无音轨摄像头FFmpeg录制卡顿问题解决
我有两台海康DVR(一台老旧模拟型号),用bash脚本通过FFmpeg获取指定时长的录像/直播流,用于Home Assistant移动告警存储。带音频的摄像头正常工作,但无有效音轨的摄像头会在接近指定时长时卡顿(旧设备卡在9.96秒,新设备卡在9.97秒),生成0KB文件。推测是时间戳缺失导致编码空音轨时卡住。
使用的FFmpeg命令
makejob=$(ffmpeg -fflags +genpts -y -ac 1 -channel_layout mono -rtsp_transport tcp -hide_banner -loglevel info -err_detect ignore_err -i "rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/${camnumber}01?starttime=$starttime&endtime=$endTime" -map 0:v -map 0:a? -ar 44100 -filter:a "volume=$volume" -t $seconds -acodec aac -vcodec copy $filepathname)
场景日志
(A) 带音频摄像头(正常录制)
Downloading from DVR BeB-DVR a 7 seconds video of the CAM CAMERA-1 recorded from 2023-08-19 10:10:10 Input #0, rtsp, from 'rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/101?starttime=20230819T101010Z&endtime=20230819T101020Z': Metadata: title : HIK Media Server V4.32.116 comment : HIK Media Server Session Description : standard Duration: 462428:22:56.-75, start: 0.000000, bitrate: N/A Stream #0:0: Video: h264 (Baseline), yuvj420p(pc, bt709, progressive), 1920x1080, 25 fps, 25 tbr, 90k tbn, 180k tbc Stream #0:1: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s Output #0, mp4, to '/home/pi/hikdwn/downloaded/BeB-DVR-CAMERA-1-2023-08-19-h01010-(10s).mp4': Metadata: title : HIK Media Server V4.32.116 comment : HIK Media Server Session Description : standard encoder : Lavf57.56.101 Stream #0:0: Video: h264 (Baseline) ([33][0][0][0] / 0x0021), yuvj420p(pc, bt709, progressive), 1920x1080, q=2-31, 25 fps, 25 tbr, 90k tbn, 90k tbc Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, mono, fltp, 69 kb/s Metadata: encoder : Lavc57.64.101 aac Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (pcm_mulaw (native) -> aac (native)) Press [q] to stop, [?] for help [mp4 @ 0x212a9d0] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly frame= 175 fps= 33 q=-1.0 Lsize= 1708kB time=00:00:07.00 bitrate=1998.8kbits/s speed=1.34x video:1643kB audio:60kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.350157% [aac @ 0x212cf00] Qavg: 111.884 Download completed!
(B) 无有效音轨摄像头(卡顿生成0KB文件)
新DVR日志
Input #0, rtsp, from 'rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/501?starttime=20230819T101010Z&endtime=20230819T101020Z': Metadata: title : HIK Media Server V4.32.116 comment : HIK Media Server Session Description : standard Duration: 462428:23:09.-90, start: 0.020000, bitrate: N/A Stream #0:0: Video: hevc (Main), yuv420p(tv), 2560x1440, 50 fps, 25 tbr, 90k tbn, 50 tbc Stream #0:1: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s Output #0, mp4, to '/home/pi/hikdwn/downloaded/BeB-DVR-CAMERA-5-2023-08-19-h01010-(10s).mp4': Metadata: title : HIK Media Server V4.32.116 comment : HIK Media Server Session Description : standard encoder : Lavf57.56.101 Stream #0:0: Video: hevc (Main) ([35][0][0][0] / 0x0023), yuv420p(tv), 2560x1440, q=2-31, 50 fps, 25 tbr, 90k tbn, 90k tbc Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, mono, fltp, 69 kb/s Metadata: encoder : Lavc57.64.101 aac Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (pcm_mulaw (native) -> aac (native)) Press [q] to stop, [?] for help frame= 251 fps=149 q=-1.0 size= 0kB time=00:00:09.97 bitrate= 0.0kbits/s speed=5.94x
旧DVR日志
Input #0, rtsp, from 'rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/501?starttime=20230819T101010Z&endtime=20230819T101020Z': Metadata: title : HIK Media Server V3.1.4 comment : HIK Media Server Session Description : standard Duration: 97123:37:11.-81, start: 0.240000, bitrate: N/A Stream #0:0: Video: h264 (Baseline), yuv420p(progressive), 352x288, 25 fps, 25 tbr, 90k tbn, 50 tbc Stream #0:1: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s Output #0, mp4, to '/home/pi/hikdwn/downloaded/HomeDVR-VIALE-2023-08-19-h01010-(10s).mp4': Metadata: title : HIK Media Server V3.1.4 comment : HIK Media Server Session Description : standard encoder : Lavf57.56.101 Stream #0:0: Video: h264 (Baseline) ([33][0][0][0] / 0x0021), yuv420p(progressive), 352x288, q=2-31, 25 fps, 25 tbr, 90k tbn, 90k tbc Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, mono, fltp, 69 kb/s Metadata: encoder : Lavc57.64.101 aac Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (pcm_mulaw (native) -> aac (native)) Press [q] to stop, [?] for help [mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 2, current: -17998; changing to 3. This may result in incorrect timestamps in the output file. [mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 3, current: -14398; changing to 4. This may result in incorrect timestamps in the output file. [mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 4, current: -10798; changing to 5. This may result in incorrect timestamps in the output file. [mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 5, current: -7198; changing to 6. This may result in incorrect timestamps in the output file. [mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 6, current: -3600; changing to 7. This may result in incorrect timestamps in the output file. [mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 7, current: 2; changing to 8. This may result in incorrect timestamps in the output file. frame= 256 fps= 49 q=-1.0 size= 0kB time=00:00:09.96 bitrate= 0.0kbits/s speed=1.89x
问题分析
无有效音轨的摄像头,DVR依然返回了音轨流,但实际没有音频数据。FFmpeg尝试编码空音轨时,因时间戳异常导致卡顿无法完成封装,最终生成0KB文件。
解决方案
方案1:以最短流时长为准,避免空音轨卡住
添加-shortest参数,让FFmpeg自动以存在有效数据的流时长结束录制:
makejob=$(ffmpeg -fflags +genpts -y -ac 1 -channel_layout mono -rtsp_transport tcp -hide_banner -loglevel info -err_detect ignore_err -i "rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/${camnumber}01?starttime=$starttime&endtime=$endTime" -map 0:v -map 0:a? -ar 44100 -filter:a "volume=$volume" -t $seconds -acodec aac -vcodec copy -shortest $filepathname)
方案2:强制禁用音频,仅录制视频
适合确定无音轨的摄像头,直接跳过音频处理:
makejob=$(ffmpeg -fflags +genpts -y -rtsp_transport tcp -hide_banner -loglevel info -err_detect ignore_err -i "rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/${camnumber}01?starttime=$starttime&endtime=$endTime" -map 0:v -t $seconds -vcodec copy $filepathname)
方案3:增强错误处理,跳过损坏数据包
添加-fflags +discardcorrupt丢弃损坏数据包,-max_interleave_delta增大音视频交错阈值,缓解时间戳异常问题:
makejob=$(ffmpeg -fflags +genpts+discardcorrupt -y -ac 1 -channel_layout mono -rtsp_transport tcp -hide_banner -loglevel info -err_detect ignore_err -i "rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/${camnumber}01?starttime=$starttime&endtime=$endTime" -map 0:v -map 0:a? -ar 44100 -filter:a "volume=$volume" -t $seconds -acodec aac -vcodec copy -max_interleave_delta 1000000 $filepathname)
内容的提问来源于stack exchange,提问作者SkizZO
相关产品推荐
相关产品推荐

