Reputation: 372
EDIT: At the time of this writing there is no functionality within FFMPEG that can detect/handle when an RTP/RTSP stream is still active but is no longer delivering valid frames. The only solution I could find was to periodically reset the stream by stopping recording, then reconnecting and starting a new recording which -f segment
does NOT do.
I'm recording an RTSP stream from a network camera with FFMPEG, and after some time (usually about an hour and a half to two hours). I'm sure it's specifically a problem with the cameras I'm using and not FFMPEG or my system resources.
What specifically happens is the video freezes but network traffic with the camera continues - it just doesn't seem to send new frames. Because of this behaviour FFMPEG doesn't disconnect/keeps recording because the network connection is still alive. After a few minutes there will always be a single warning in the FFMPEG output:
More than 1000 frames duplicated
But it keeps recording - it's just the same frame over and over.
The command I'm using is:
ffmpeg -stimeout 1000000 -rtsp_transport udp -fflags discardcorrupt -i rtsp://192.168.1.163/live/0/MAIN -vc libx265 -f segment -segment_time 300 -segment_atclocktime 1 -reset_timestamps 1 -strftime 1 "163-%Y-%m-%d_%H-%M-%S-h265.mp4"
Some notes:
Having FFMPEG fail and exit after > 1000 frames are duplicated would actually be ideal, as then I can just spawn FFMPEG from a script, monitor the process, and restart it when the process ends. Any solution would be great though.
Upvotes: 7
Views: 4208
Reputation: 1231
While have been struggling with similar issue for a while, I haven't found any reliable solutions. The best option so far was to run watchdog script that monitors all running ffmpeg processes reading RTSP streams, and tracks CPU usage for every process. When usage drops to zero between a few consecutive checks, 99.9% that means that ffmpeg is stuck reading the stream. Watchdog just kills corresponding ffmpeg process, and it starts again from another shell wrapper script.
I also use stimeout
, and it actually helps in cases when there are various network issues, but as stated by the topic starter it doesn't have any effect when network is fine and it's just RTSP source (camera) that stops working correctly.
This is indeed ugly but tends to work. Appreciate if anyone would share better approaches.
Upvotes: 5
Reputation: 61
This guy will help you:
11.134 mpdecimate
Drop frames that do not differ greatly from the previous frame in order to reduce frame rate.
The main use of this filter is for very-low-bitrate encoding (e.g. streaming over dialup modem), but it could in theory be used for fixing movies that were inverse-telecined incorrectly.
use like that: "-vf", "mpdecimate,setpts=N/FRAME_RATE/TB",
Upvotes: 2