Ryan Griggs
Ryan Griggs

Reputation: 2758

How to stop FFMPEG from running if connection to input stream is lost?

I am capturing thumbnails from a webcam RTMP stream every 1 second to JPG files. Here's my command line:

ffmpeg -i rtsp://192.168.1.89:554/11 -f image2 -r 1 thumb%03d.jpg

How can I make FFMPEG die with an error if the input RTMP stream is lost for a given timeout period? Currently, if I lose connection to the webcam, FFMPEG starts throwing "Unknown error" messages, but when the network reconnects, ffmpeg appears to reconnect to the stream, but does not output any more captured frames. I have to manually kill the process and restart it to again start capturing frames.

It would be nice to simply have ffmpeg die when it loses connection for a specific timeout period. Then I could monitor the process and restart when it ends.

Any ideas?

Upvotes: 6

Views: 13649

Answers (2)

The solution is actually to put a timeout on the socket connection:

[...] -rtsp_transport tcp -stimeout 30000000 -i rtsp://192.168.1.89:554/11 [...]

(30 seconds in this case)

Upvotes: 3

TopReseller
TopReseller

Reputation: 666

You can set ffmpeg timeout for RTSP :

timeout Set maximum timeout (in seconds) to wait for incoming connections.

A value of -1 means infinite (default). This option implies the rtsp_flags set to ‘listen’.

stimeout Set socket TCP I/O timeout in microseconds.

Upvotes: 2

Related Questions