我有一个通过插座连接相机的程序。当我通过此套接字发送消息时,此摄像机开始直播视频,并在我发送保持活动消息时继续。视频通过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和套接字?提前谢谢!
答案 0 :(得分:0)
也许你工作的同一个项目喜欢我:]。我在此之前查看了一页。它说你想尝试gst 1.0而不是gst 0.10。我仍然使用它,几乎放弃了。现在我只能使用gst 0.10。现在,我已经为gst管道编辑了一些命令行。它运作良好。希望它会帮助你,并回复我任何改进。以下是我自己发布的答案: Grab the frame from gst pipeline to opencv with python