Search code examples
python-3.xopencvrtsp

Python- How to handle error for RTSP link


I've created a python script that checks muliple different urls and ports and detects if there is an RTSP stream on them - it is working fine, but it creates errors when the stream doesn't exist (which I'd obviously expect).

I'm getting [rtsp @ 0x16745c0] method DESCRIBE failed: 451 ERROR

What I want to do it add a line to my script so if I get the above error, then I just display it in a message on screen. I've tried the following with no luck:

for x in feedList:

    print("[INFO] Checking Link..." + x)

    cap=cv2.VideoCapture(x)

    try:

        # Check if camera opened successfully
        if (cap.isOpened()== True): 
            streamlink = x
            print("[INFO] FOUND! Stream Link..." + x)
            break
    except socket.error:
        print("[NO STREAM]" + x)
    except:
        print("[FAILED]" + x)
        pass

The Except cases never get hit, I always just get [rtsp @ 0x16745c0] method DESCRIBE failed: 451 ERROR

Any help would be appreciated.

Thanks Chris


Solution

  • If the stream on the link does not exist, creating VideoCapture object on that link would still be successful but you will not be able to process on the object.

    You code's control flow just might be going in and checking if (cap.isOpened()== True) but there is no else block to handle what would happen if if (cap.isOpened() != True). So just try adding an else block to display the error message.

    for x in feedList:
    
        print("[INFO] Checking Link..." + x)
    
        cap=cv2.VideoCapture(x)
    
        try:   
            # Check if camera opened successfully
            if (cap.isOpened()== True): 
                streamlink = x
                print("[INFO] FOUND! Stream Link..." + x)
                break
            # Else is important to display error message on the screen if can.isOpened returns false
            else
                print("[NO STREAM]" + x)
        except socket.error:
            print("[NO STREAM]" + x)
        except:
            print("[FAILED]" + x)
            pass
    

    If this doesn't work: following might solve the issue:

    One of the main issues is that every camera manufacturer uses their own protocol (RTSP URI formatting). Finding the correct URL for your IP-camera can be frustrating and time-intensive. When found you can try to open it with VLC, and afterwards with Kerberos.io.

    Depending on the format of the RTSP URI things can go wrong, for example when using a format like above. To solve the problem you'll need to add an question mark "?" at the end of the url.

    As example original link might be:

    rtsp://192.168.2.109:554/user=admin&password=mammaloe&channel=1&stream=0.sdp
    

    So with ? it would be:

    rtsp://192.168.2.109:554/user=admin&password=mammaloe&channel=1&stream=0.sdp?
    

    Source