我有一个通过套接字与摄像机连接的程序。当我通过此套接字发送消息时,此摄像机开始直播视频,并在发送保持 Activity 的消息时继续直播。视频通过UDP作为mpeg-ts数据包传输。我正在尝试使用OpenCV播放视频,但是

[udp @ 0x10d07e0] bind failed: Address already in use

执行错误。这是我的代码(ip4和LIVEPORT是常量):
dataLive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
td = threading.Timer(0, send_keepalive_msg, [dataLive, KA_DATA_MSG, (ip4,LIVEPORT)])
td.start()

videoLive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tl = threading.Timer(0, send_keepalive_msg, [videoLive, KA_VIDEO_MSG, (ip4, LIVEPORT)])
tl.start()

videourl = "udp://@{0}:{1}/".format(ip4, LIVEPORT)
cap = cv2.VideoCapture(videourl)

while cap.isOpened():
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow("frame", gray)
    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

这段代码是使用Gstreamer的前一个代码的改进,效果很好:
PIPELINE_DEF = "udpsrc do-timestamp=true name=src closefd=false !" \
               "mpegtsdemux !" \
               "queue !" \
               "ffdec_h264 max-threads=0 !" \
               "ffmpegcolorspace !" \
               "xvimagesink name=video"

dataLive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
td = threading.Timer(0, send_keepalive_msg, [dataLive, KA_DATA_MSG, (ip4,LIVEPORT)])
td.start()

videoLive = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tl = threading.Timer(0, send_keepalive_msg, [videoLive, KA_VIDEO_MSG, (ip4, LIVEPORT)])
tl.start()

# Create gstreamer pipeline to stream video
pipeline = gst.parse_launch(PIPELINE_DEF)

# Source element: video socket. Sockfd file for UDP reception. socket.fileno() returns socket descriptor
src = pipeline.get_by_name("src")
src.set_property("sockfd", videoLive.fileno())

pipeline.set_state(gst.STATE_PLAYING)

while True:
    state_change_return, state, pending_state = pipeline.get_state(0)
    if gst.STATE_CHANGE_FAILURE == state_change_return:
        break

send_keep_alive函数并不重要,但是下面是代码:
def send_keepalive_msg(socket, msg, peer):
    while True:
        socket.sendto(msg, peer)
        time.sleep(timeout)

如何正确地将opencv CaptureVideo与套接字绑定(bind)?提前致谢!

最佳答案

也许您像我一样从事相同的项目:]。在此之前,我已经查看了您发布的一页。它说你想尝试GST 1.0而不是GST 0.10。我仍然使用它,几乎放弃了。现在我只能使用gst 0.10。现在,我已经为gst管道编辑了一些命令行。它运作良好。希望它能对您有所帮助,并回复我任何改进。以下是我自己发布的答案:
Grab the frame from gst pipeline to opencv with python

关于python - Python OpenCV : Play UDP video streaming while sending keep alive messages,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37660982/

10-11 06:29