Score:0

ffmpeg rtsp server not stopping when peer disconnect

ar flag

There is an ffmpeg instance running as a server on a VPS, that converts RTSP video to HLS and serves it to the web (using Apache). The RTSP video comes from a computer somewhere else (in another town in this case), to which I have an IP camera connected. The computer takes the RTSP data FROM the IP camera and retranslates it TO the VPS.

This is the ffmpeg command I'm using on that computer (the re-streamer, may I say):

ffmpeg \
-rtsp_transport tcp \
-i rtsp://[IP address of the camera]:554/live \
-c:v copy \
-f rtsp \
-rtsp_transport tcp \
rtsp://[IP address of the VPS]:4445/live.sdp

And this is the command I'm using on the VPS:

ffmpeg \
-rtsp_flags listen \
-listen_timeout 5 \
-timeout 5000000 \
-rtsp_transport tcp \
-i rtsp://[ip address of the VPS]:4445/live.sdp \
-map 0 \
-flags +global_header \
-fflags +igndts \
-c:v copy \
-g 0 \
-b:v 125k \
-maxrate 250k \
-bufsize 500k \
-hls_time 1 \
-hls_list_size 15 \
-hls_wrap 15 \
-y /dev/shm/hls/video.m3u8

Everything works fine so far: I can read the HLS stream in browser and it is pretty stable and fast. The problem comes when the connection between the re-streamer and the VPS drops: I have to ssh into the VPS and restart the ffmpeg instance manually. Is it possible to tell ffmpeg to "crash" (or simply stop it's process) when no input is coming on the -i address (on the VPS's side)? I am using a bash script that restarts the process automatically, so that's taken care of.

Just to avoid any confusion: According to ffmpeg's official documentation the timeout flag takes MICROSECONDS as a value and listen_timeout takes SECONDS. I've seen a lot of posts where people were arguing about that :)

Score:0
sx flag

Hope I'm not too late for the party. I solved a similar problem by monitoring the CPU usage. In my case, when the cameras restart or loose connection FFMPEG keeps running but CPU usage falls to 0.0%. I use python to monitor that. Here's the important bit:

import subprocess, psutil
import time, os

# Run ffmpeg command
base_process = subprocess.Popen([
    'ffmpeg',
    '-rtsp_transport', 'tcp',
    '-i', LINK_RTSP,    
    '-map', '0:v', '-map', '0:a', '-vcodec', 'copy', '-acodec', 'aac', '-t', 60,
    os.path.expanduser(f'~/Desktop/{date}.mp4')        
])  

#Check process by CPU usage
control_process = psutil.Process(base_process.pid)
attempts = 5 #Sleep is 1 second, so if ffmpeg is not using cpu for 5 seconds, kill the process
while base_process.poll() is None:
    cpu_percent = control_process.cpu_percent()

    if cpu_percent == 0.0:
        attempts -= 1

    if attempts == 0:
        logger.info("ffmpeg is hanging (CPU usage is 0%). Killing the process!")
        while base_process.poll() is None:
            base_process.terminate()
            time.sleep(1)

        logger.info(f"Files to be recoved: {date}.mp4")
        try:
            for camera in connected_cameras.keys():
                os.rename(os.path.expanduser(f'{BASE_FOLDER}{camera}/{date}.mp4'), os.path.expanduser(f'{BASE_FOLDER}{camera}/{date}_recovery-needed.mp4'))
        except:
            pass
        
        break

    time.sleep(1)

I hope it helps.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.