Is there a way to reconnect to a RTSP stream after it disappears from the network?
There's an IP camera, and I can connect to its RTSP stream using the following address: rtsp://user:pass@192.168.1.242:554/stream1 I wanted to save the stream to a file (30s per segment), and the following command does the job: $ ffmpeg -loglevel debug -use_wallclock_as_timestamps 1 \ -rtsp_transport tcp -i rtsp://user:pass@192.168.1.242:554/stream1 \ -vcodec copy -acodec copy \ -f segment -segment_format mkv -segment_time 30 -reset_timestamps 1 \ -strftime 1 %Y-%m-%d-%H-%M-%S.mkv The problem is when the camera device disappears from the network, for instance it experiences some power outage. When the camera comes back to life, ffmpeg doesn't stream anymore, it just hang. So the question is how to reconnect to the stream and do the streaming like nothing happened?
I asked this question about audio streams a month ago, never got an answer. streamripper (audio streams only) keeps on trying for the whole time specified and quits at the end of the appointed time, so it can be done. Unfortunately streamripper works on shoutcast streams only.
On 2025-01-05 16:20, arthurpeabody wrote:
I asked this question about audio streams a month ago, never got an answer. streamripper (audio streams only) keeps on trying for the whole time specified and quits at the end of the appointed time, so it can be done. Unfortunately streamripper works on shoutcast streams only.
Hmm, it's kind of weird thing it's not working, because after the camera comes back to life, I can see network packets in wireshark flowing between the camera and the already running ffmpeg instance. The state of the connection is also ESTABLISHED. So they're taking but there's no stream...
When streamripper restarts after a dropout it creates a new file. When one archives a log file in Linux, the daemon that writes to it stops writing to it, for which reason one must stop a daemon when archiving its log file, then restart. This could be ffmpeg's problem: the file handle to which it was writing has disappeared and it isn't opening a new one.
On 2025-01-05 18:31, arthurpeabody wrote:
When streamripper restarts after a dropout it creates a new file. When one archives a log file in Linux, the daemon that writes to it stops writing to it, for which reason one must stop a daemon when archiving its log file, then restart. This could be ffmpeg's problem: the file handle to which it was writing has disappeared and it isn't opening a new one.
I think I solved this. I mean, I'm not sure whether ffmpeg is able to do what I wanted it to do, but I created the following systemd service which is able to restart the ffmpeg proces when it timeouts: ------------------------------------ [Unit] Description=FFMPEG streaming Documentation=man:ffmpeg DefaultDependencies=yes After=media-Zami.mount After=local-fs.target network-online.target nss-lookup.target Before=multi-user.target ConditionPathIsReadWrite=/media/Zami/ffmpeg-rec/ ConditionFileIsExecutable=/usr/bin/ffmpeg [Service] #StandardError=null #StandardOutput=null # Systemd specifiers are indicated by the % sign. Use "%%" in place of "%" to specify a single # percent sign (in order to use the strftime ffmpeg option to get the timestamp in the filenames). Environment="FILE_NAME=/media/Zami/ffmpeg-rec/%%Y-%%m-%%d-%%H-%%M-%%S.mkv" ExecStart=/usr/bin/ffmpeg \ -hide_banner -loglevel error \ -timeout 1000000 -use_wallclock_as_timestamps 1 \ -rtsp_transport tcp -i rtsp://user:pass@192.168.1.242:554/stream1 \ -vcodec copy -acodec copy \ -f segment -segment_format mkv -segment_time 30 \ -reset_timestamps 1 -strftime 1 \ $FILE_NAME #Restart=on-failure Restart=always RestartSec=5 TimeoutSec=1s #RuntimeMaxSec=3600 # Hardening ProtectSystem=full PrivateTmp=true SystemCallArchitectures=native MemoryDenyWriteExecute=true NoNewPrivileges=true [Install] WantedBy=multi-user.target ------------------------------------ So, in the ffmpeg exec line I added the `-timeout 1000000` parameter. The option makes sure that when camera disappears ffmpeg will terminate gracefully after one second (it will stop recording and close the file). Then systemd detect this failure and initiates the service restart. Then if the host is still down, ffmpeg will terminate because of "Error opening input files: Connection timed out". In the 5s intervals, systemd tries to restart the service, and if the camera is down, it tries over and over till it shows up on the network. When the camera comes back to life, ffmpeg starts recording with new a file. Nothing gets corrupted, and everything seems to work just fine. Probably the systemd service file needs some adjustments, but for now, it works more or less in the way I wanted. I still dont't know if ffmpeg is capable of doing this task alone. So if someone knows the answer, please let me know.
Your ffmpeg has switches mine doesn't.
On 1/6/2025 7:17 AM, arthurpeabody wrote:
Your ffmpeg has switches mine doesn't.
Are you running the latest? (7.1 was released 4 months ago, and it continually evolves) z!
Quoth Carl Zwanzig: 'Are you running the latest?' Yes. I get updates for all my packages daily. I didn't search on help full. I see that there are a bunch of reconnect switches, which should work for me, but none of them are for RTSP.
On 2025-01-06 19:16, arthurpeabody wrote:
Quoth Carl Zwanzig: 'Are you running the latest?' Yes. I get updates for all my packages daily. I didn't search on help full.
I see that there are a bunch of reconnect switches, which should work for me, but none of them are for RTSP.
I've also tried all of those reconnect switches, but none of them seems to be working in the case of RTSP.
On 1/6/2025 10:16 AM, arthurpeabody wrote:
Yes. I get updates for all my packages daily.
For completeness, what version do you have? Just running ffmpeg provides very useful info (and should always be included when asking questions): This is a very old build.... $ /c/bin/ffmpeg-6.1.1-full_build/bin/ffmpeg.exe ffmpeg version 6.1.1-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers built with gcc 12.2.0 (Rev10, Built by MSYS2 project) configuration: --enable-gpl --enable-version3 --enable-static --pkg-config=pkgconf --disable-w32threads --disable-autodet ect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable- libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libblur ay --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable- libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable -libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation -- enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-liblensfun --enable-li bvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-dxva2 --enable-d3d11va --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --e nable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-l ibmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libi lbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enab le-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint libavutil 58. 29.100 / 58. 29.100 libavcodec 60. 31.102 / 60. 31.102 libavformat 60. 16.100 / 60. 16.100 libavdevice 60. 3.100 / 60. 3.100 libavfilter 9. 12.100 / 9. 12.100 libswscale 7. 5.100 / 7. 5.100 libswresample 4. 12.100 / 4. 12.100 libpostproc 57. 3.100 / 57. 3.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... BTW, I looked back six months and don't see your audio question z!
Quoth Carl Zwanzig: 'For completeness, what version do you have?' ffmpeg version 7.1 Copyright (c) 2000-2024 the FFmpeg developers built with gcc 14.2.0 (GCC) configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/doc/ffmpeg-7.1/html --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-gpl --enable-version3 --arch=x86_64 --disable-encoder=aac --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gnutls --enable-libass --enable-libbluray --enable-libcaca --enable-libcdio --enable-frei0r --enable-openal --enable-libopus --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libmp3lame --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libpulse --enable-libsmbclient --enable-libxml2 --enable-librsvg --enable-vulkan --enable-libdrm --enable-libaom --enable-libdav1d --enable-libshaderc --enable-gcrypt --enable-libplacebo --enable-lcms2 libavutil 59. 39.100 / 59. 39.100 libavcodec 61. 19.100 / 61. 19.100 libavformat 61. 7.100 / 61. 7.100 libavdevice 61. 3.100 / 61. 3.100 libavfilter 10. 4.100 / 10. 4.100 libswscale 8. 3.100 / 8. 3.100 libswresample 5. 3.100 / 5. 3.100 libpostproc 58. 3.100 / 58. 3.100 Universal media converter usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... Use -h to get full help or, even better, run 'man ffmpeg' Quoth Carl Zwanzig: 'I looked back six months and don't see your audio question' Neither did I. It seems I sent it from the wrong address. Mr Morfikov's problem got me to figure out the answer for myself - if the reconnect parameters work.
On 1/5/2025 6:49 AM, Mikhail Morfikov wrote:
There's an IP camera, and I can connect to its RTSP stream using the following address:
Out of curiosity, have you tried RTMP instead to see if it behaves differently? (In general, I expect there is better software for this use case than ffmpeg standalone, but if you've got something now that works, great.) z! (goes off to see how ffmpeg handles signals)
On 2025-01-06 22:12, Carl Zwanzig wrote:
Out of curiosity, have you tried RTMP instead to see if it behaves differently?
No, this is a cheap IP camera, supporting only RTSP or/and some proprietary protocol if you use their android app. I don't want to use the app, and since it gives me the RTSP stream, I wanted to record it from time to time. FFmpeg does it without problems, but it fails when can't connect to the camera. With the systemd service, everything works just fine, but what if I got some other system without systemd? So I'm still looking for some more universal solution.
participants (3)
-
arthurpeabody -
Carl Zwanzig -
Mikhail Morfikov