Python- 如何处理 RTSP 错误 link

Python- How to handle error for RTSP link

我创建了一个 python 脚本来检查多个不同的 url 和端口并检测它们是否有 RTSP 流 - 它工作正常,但当流不存在时它会产生错误(我显然希望如此)。

我得到 [rtsp @ 0x16745c0] method DESCRIBE failed: 451 ERROR

我想做的是在我的脚本中添加一行,这样如果我收到上述错误,我就将它显示在屏幕上的消息中。我尝试了以下方法但没有成功:

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

除非情况永远不会被击中,我总是得到 [rtsp @ 0x16745c0] method DESCRIBE failed: 451 ERROR

如有任何帮助,我们将不胜感激。

谢谢 克里斯

如果 link 上的流不存在,在 link 上创建 VideoCapture 对象仍然会成功,但您将无法处理该对象。

您的代码的控制流可能只是进入并检查 if (cap.isOpened()== True) 但没有 else 块来处理如果 if (cap.isOpened() != True) 会发生什么。所以只需尝试添加一个 else 块来显示错误消息。

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

如果这不起作用:以下可能会解决问题:

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.

例如原始 link 可能是:

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

因此 ? 将是:

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

Source