IP camera recording via RTSP: audio/video desync (dropped frames?)
Hello everyone. I've been having some problems with recording video from an IP camera that transmits a live feed over RTSP. I've searched a lot for potential solutions, but none helped so far. I've been going crazy trying to fix this for days, so now I'm asking here as a last resort. This is the command-line I'm using to record the video. Is is supposed to continously record the camera feed, cutting it up in 10 minute-long segments:
ffmpeg -nostdin -flags low_delay -fflags +nobuffer+genpts+igndts+ignidx+discardcorrupt \ -rtsp_transport tcp -timeout 3000000 \ -i rtsp://login@password@ip.ad.dre.ss:554/url \ -map 0:v -c:v copy -map 0:a -c:a copy -f segment -strftime 1 -reset_timestamps 1 -segment_atclocktime 1 -segment_time 600 "%Y-%m-%dT%H-%M-%S.mkv"
FFMPEG outputs no warnings while running, but the recorded segments exhibit the following undesired issues: 1) Significant video lag gets accumulated over time. E.g. the first frame of the segment recorded at 12:00:00 may show the time 11:59:30 on the camera clock. The delay is only a few seconds at first but gets more and more noticeable with each recording. Note that the camera clock itself is working correctly - restarting the recording process resets the delay. 2) As a result of #1, audio starts skipping ahead of video over time, and just like with the previous issue, the delay gets progressively worse, correlating linearly with the video lag. E.g. if I knock on a solid surface with my finger in view of the camera, the sound will come much earlier than the video. This becomes easily noticeable after only some 10-15 minutes of continuous recording during day-time. The delay between audio and video is about the same as the one between the starting time of the segment and the clock time visible on the first frame. Note that neither the video nor the audio is being transcoded, so this cannot be a result of CPU overload on my end. I do not see any CPU usage spikes while recording. FFMPEG input and output parameters are as follows (according to its own output):
Input #0, rtsp, from 'rtsp://login@password@ip.ad.dre.ss:554/url': Metadata: title : RTSP Server Duration: N/A, start: 0.000000, bitrate: N/A Stream #0:0: Video: h264 (Main), yuvj420p(pc, bt709, progressive), 2560x1440, 25 fps, 25 tbr, 90k tbn Stream #0:1: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s Output #0, segment, to '%Y-%m-%dT%H-%M-%S.mkv': Metadata: title : RTSP Server encoder : Lavf59.16.100 Stream #0:0: Video: h264 (Main), yuvj420p(pc, bt709, progressive), 2560x1440, q=2-31, 25 fps, 25 tbr, 1k tbn Stream #0:1: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (copy)
Here's what I've been able to find out about this problem so far: === 0. The issue only manifests with this specific camera === I have multiple IP cameras at home, and the problematic one is a different make and model than the rest, so it must be something about how it encodes and streams the video/audio. Whatever the cause is, I know that all my cameras stream video over RTSP at 25 FPS and audio at 8000 Hz - and recording with the exact same command-line works perfectly with all of them except for this one. Note that this is also the only camera where "-use_wallclock_as_timestamps 1" causes the video to stutter (see below). The camera itself is a cheap Chinese IP camera. It doesn't have a specific brand on it, but here's what it says in the web UI: "Device Model: F8/IPG-9280PGS-AI". Not sure if this will help, but mentioning it just in case. Since recording from all other cameras works correctly, I assume this is not a bug in FFMPEG itself. I've tested with two different versions (5.0.1 on Windows and 4.4.2 on FreeBSD), and they both exhibit the problem with only this specific camera. === 1. "-use_wallclock_as_timestamps 1" works, but is undesirable === If "-use_wallclock_as_timestamps" 1 is added to the command line before the input, both issues are resolved, HOWEVER, this results in the video constantly stuttering (it looks as if it skips ahead slightly with every keyframe). Therefore, this option cannot be used as a solution. In addition to that, FFMPEG starts complaining about non-monotonous DTS in output stream 0:0 (but all recordings still seem to play back normally, except for the stuttering). === 2. The video lag is related to having audio input === If there is no audio input, then there is no video lag either - the first frame of each segment (give or take a keyframe interval) is always on time according to the camera clock.
# this does not have video lag accumulating over time ffmpeg -nostdin -flags low_delay -fflags +nobuffer+genpts+igndts+ignidx+discardcorrupt \ -rtsp_transport tcp -timeout 3000000 \ -i rtsp://login@password@ip.ad.dre.ss:554/url \ -map 0:v -c:v copy -f segment -strftime 1 -reset_timestamps 1 -segment_atclocktime 1 -segment_time 600 "%Y-%m-%dT%H-%M-%S.mkv"
However, removing audio is not a viable solution either, since I do want to have audio recorded along with the video. === 3. The actual FPS is sometimes less than the one reported by RTSP === The RTSP streaming source reports a constant frame rate of 25 FPS, but I've inspected the recordings and found that at least one segment had only 14900 frames, so its actual frame rate was 14900/600 = 24.8(3) FPS. But even though some frames are missing and the file is reported to have a framerate of 25 FPS and a length of 10 minutes, it does not seem to have any video playback issues at all. This is a significant difference compared to recordings from my other cameras: I tried picking some at random, and they all reported 15000 frames exactly. Some other segments recorded during my later experiments had even less frames, e.g. one had only about 12000, and even though it still had a 10-minute runtime, the camera clock only counted about 9 minutes and 50 seconds from start to end, so I guess it played back a bit slower than in realtime (but I could not perceive any difference). What's more interesting is that all these frame count discrepancies happened only during day-time. It appears that every segment recorded during night-time, when the camera is in IR black-and-white mode, has exactly 15000 frames, meaning a "real" 25 FPS, and the video lag does not increase at that time (and nor does the audio desync). Therefore, I can conclude that the issue has something to do with the camera not being able to encode at a constant 25 FPS at all times. I assume it is possible that an audio desync will happen when frames are dropped. === 4. Adding "-vsync cfr" doesn't help - need to re-encode? === This seemed like the way to go, according to the documentation. If the camera sometimes cannot produce frames at its reported rate, then we should duplicate them to bring the frame count up to the expected value to match the reported FPS. Then the discrepancy will be gone, and the sync issues along with it. Right?
cfr (1) Frames will be duplicated and dropped to achieve exactly the requested constant frame rate.
Unfortunately, this didn't work as I'd hoped: some segments still had less frames than expected, and the lag was still happening as a result.
# This should work but doesn't ffmpeg -nostdin -flags low_delay -fflags +nobuffer+genpts+igndts+ignidx+discardcorrupt \ -rtsp_transport tcp -timeout 3000000 \ -i rtsp://login@password@ip.ad.dre.ss:554/url \ -map 0:v -vsync cfr -c:v copy -map 0:a -c:a copy -f segment -strftime 1 -reset_timestamps 1 -segment_atclocktime 1 -segment_time 600 "%Y-%m-%dT%H-%M-%S.mkv"
Moving "-vsync cfr" to before input didn't appear to change anything. (The documentation says it's global, so I assume its position is irrelevant.) I guess that it's possible that -vsync simply does not work when copying streams, since FFMPEG might need to decode the video to be able to duplicate frames. But it didn't produce an error message, so I thought it would work. Note that transcoding a 1440p video stream 24/7 is completely out of the question for me (the recording server simply does not have that much CPU power). === 5. Sync audio instead of video? === If I cannot drop video frames, perhaps I can adjust the audio track to match the video instead? Like I said before, I cannot transcode the video... but transcoding audio doesn't take nearly as much processing power, so it wouldn't be too big of a problem for me. Furthermore, it is even required if I want to broadcast the feed to YouTube (and in this particular case, I do) because it dictates that the audio must be encoded with either AAC or MP3. For live streaming, I use the AAC encoder with the following options:
-c:a aac -ar 48000 -ac 2 -b:a 128k
With these parameters, when recording from other cameras there was sometimes a slight delay between the audio and video streams (not increasing over time). I managed to remove it by adding:
-af aresample=async=1
Naturally, this didn't help with the audio sync issues for the problematic camera. I also tried increasing the async=value, up to 10000, to no avail. The documentation suggests it might be helpful in syncing the audio track, but so far my experiments with it have been unsuccessful. === 6. Conclusion === At this point, I'm a bit lost. I tried googling, but I couldn't find much about the scenario where video is copied and not transcoded, so I ultimately decided to ask for help. I think the way to go is to tinker with the audio filters (see above), but I don't really have any experience with them, so I'm not exactly sure what I should do to solve the issue. I really hope it is possible to solve it without re-encoding the video stream. Even better if I don't actually have to re-encode the audio either, but it's not a big deal if I do. If necessary, I will try to provide as much additional information as I can that might help finding a solution. Thank you very much. -- Kind regards, Vladimir
Here's some additional information that may or may not be useful in solving this: 1. I removed the "+genpts+igndts+ignidx" from "fflags". From what I could understand, it wasn't necessary to use them, and removing them didn't make anything better or worse. 2. I tried to find out why the video lag gets introduced only when the audio stream is present, so I ran two live recording sessions in parallel: one with audio, and another one without - and found something very interesting. Remember I said that the lag happens when the video does not have enough frames? Well, that's not quite true, it seems - frames do get dropped sometimes, but it's not the actual cause of the problem. I've been analyzing the recorded files, and it looks like the actual issue is with incorrect presentation timestamps (PTS) being transmitted or calculated. The frames themselves come on time, but when FFmpeg records the video along with the audio, it erroneously puts some frames in the next segment, probably in an attempt to synchronize the two streams, which actually makes it worse! Here's the comparison of two segments recorded during the experiment, from the time when the lag began: The segment recorded WITHOUT audio has 15000 frames and PTS ranging from 0 to 612. This is obviously incorrect, because the actual clock time, as seen in the video itself, only counts 600 seconds (from 00:10:00 to 00:20:00). Due to this discrepancy, the video length is also wrong (10 minutes and 12 seconds). However, the next segment does not exhibit any lag (the clock starts at 00:20:00 as expected) and does not seem to have any PTS issues whatsoever. Now, the segment recorded WITH audio is another story entirely. It has only 14750 frames, and the PTS values range from 0 to about 602. The latter corresponds to its reported length of 10 minutes and 2 seconds, but the clock ends counting on 00:19:50 - entire 10 seconds are missing! But they aren't gone, they are actually included in the next segment, which reports 15000 frames just like the silent one, but these frames are not the same - the clock runs from 00:19:50 to 00:20:50 (should be 00:20:00 to 00:30:00), and the PTS also starts from 2 instead of from 0 - but the audio begins playing right from the start. As a result, a lag of 12 seconds between the audio and the video has been introduced. Now, the question is, how to fix this. Like I already said before, "-use_wallclock_as_timestamps 1" works, but causes constant stuttering in the video. But it's not actually necessary to aggressively enforce new PTS at all times. Because a 10-minute segment reports a length of 10 minutes and 12 seconds, it implies that some of the reported (or calculated) timestamps are ahead of real time - which is obviously wrong for a live stream. Perhaps a less aggressive correction of these discrepancies can be implemented with some option or bitstream filter (like "setts") that can detect this scenario of PTS jumping ahead of actual time? Thank you very much. --- Kind regards, Vladimir
After another day of searching for solutions, I finally managed to come up with one. Remember I said that "-use_wallclock_as_timestamps 1" fixes the sync issues but causes the video to stutter? So I attempted to fix the stuttering with the following filter:
setts='max(floor(PTS/X)*X,if(N,PREV_OUTPTS+X))'
Note the X: it has to be substituted with a constant depending on the use case (recording/streaming), and/or the stream the filter is being applied to. Explanation: this filter expects input timestamps to be generated from the wallclock time but not necessarily spread out evenly. To fix stuttering, it adjusts the timestamps to be multiples of X = timebase times 1000 (for 25 FPS this is 0.04*1000=40) by computing the frame number and rounding it down to the nearest integer. It also ensures that the timestamps are always increasing - if the adjusted value is found to be less than the previous value plus one frame, then this sum is used as the output instead. There is also a catch here - the camera can sometimes alter the frame rate, which causes the formula to produce weird results. Rectifying this is possible by assuming a constant frame rate for the input stream ("-r 25"). Also note that for smooth playback, the filter has to be applied both to audio and video. When recording segments without re-encoding audio, X should be set to 40 for both streams (assuming 25 FPS). Streaming, however, is another matter. When RTMP streaming via "-f flv" (I use fifo+flv), X has to be set to 1 for the video stream (I think in this case "floor" can be dropped since all values seem to be integers, but not entirely sure). Audio is another beast entirely: X should be 320 because the original sampling frequency is 8000 Hz mono, meaning 8000/25=320 samples per frame. The filter also needs to be applied via "-af" instead of "-bsf:a" to operate on the source data. The final command-line is as follows (both recording and streaming):
ffmpeg -nostdin -flags low_delay -fflags +nobuffer+discardcorrupt \ -rtsp_transport tcp -timeout 3000000 -use_wallclock_as_timestamps 1 \ -r 25 -i rtsp://login:password@ip.ad.dre.ss:554/url \ -map 0:v -c:v copy -bsf:v setts='max(floor(PTS/40)*40,if(N,PREV_OUTPTS+40))' \ -map 0:a -c:a copy -bsf:a setts='max(floor(PTS/40)*40,if(N,PREV_OUTPTS+40))' \ -f segment -strftime 1 -reset_timestamps 1 -segment_atclocktime 1 -segment_time 600 "%Y-%m-%dT%H-%M-%S.mkv" -map 0:v -c:v copy -bsf:v setts='max(floor(PTS),if(N,PREV_OUTPTS+1))' -map 0:a -c:a aac -ar 48000 -ac 2 -b:a 128k -af asetpts='max(floor(PTS/320)*320,if(N,PREV_OUTPTS+320))' \ -f fifo -fifo_format flv -drop_pkts_on_overflow 1 -attempt_recovery 1 -recover_any_error 1 -format_opts flvflags=no_duration_filesize rtmp://<STREAM_URL>
NB: the current version of FFmpeg in the FreeBSD ports collection (4.4.2) needs these two patches for the proposed solution to work: https://github.com/FFmpeg/FFmpeg/commit/301d275301d72387732ccdc526babaf984dd... https://github.com/FFmpeg/FFmpeg/commit/b0b3fce3c33352a87267b6ffa51da31d5162... The first patch fixes the expression parser erroring out, and the second one fixes the PREV_OUTPTS value always equal to NOPTS. Also, "timeout" has to be replaced with "stimeout". I'm still not sure if this solution is the proper one. So far, it's been running for many hours, and the resulting video is smooth as butter, and without any gradually increasing audio/video lag. But it looks extremely overcomplicated, not to mention it took me several days of researching and analyzing the video files to implement. Also, I don't know where the timestamp drift actually occurs - most signs point to the camera, but there's also the fact that some sort of conversion takes place depending on the output (e.g. segment/mkv measures timestamps in 1/1000ths of a second, but flv measures them in frames), and it might be possible that there's a bug somewhere in there. For simplicity though, let's assume there's no bug, and the fault occurs at the source. We know that the audio is always on time, so why not use the timestamps of the audio packets for the video too? E.g. for each incoming video frame, assign it the timestamp of the latest audio packet received (not the wallclock time). The problem is that "setts" filters cannot interact with each other, so it's not possible to use them for this purpose. Well, even though I've managed to somehow deal with this problem, I'm still no expert. So further comments are still welcome. Until then, I hope the information provided in this thread will be useful to anybody who encounters a similar issue. Thank you very much. --- Kind regards, Vladimir
participants (1)
-
Vladimir Mishonov