如何在python中向UDP流添加元数据

发布于 2025-01-12 20:47:32 字数 4796 浏览 0 评论 0原文

我有一个客户端服务器应用程序,它在 python 中将 udp 服务器从一个客户端发送到另一个客户端。在 UDP 流中,我通过使用 CV2 从磁盘读取视频然后使用 UDP 发送来发送视频。

我想随视频流一起添加一些元数据。我想将帧号、时间戳、视频名称、播放时间和开始时间添加到视频流中。

我怎样才能做同样的事情。我创建了多个使用线程管理的 UDP 套接字。

以下是发送方的代码

import cv2
import socket
import math
import pickle
import sys

max_length = 65000
host = '127.0.0.1'
port = 6000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

cap = cv2.VideoCapture("input2.mp4")
print(cap)
ret, frame = cap.read()
print(ret)
while ret:
    # compress frame
    retval, buffer = cv2.imencode(".jpg", frame)

    if retval:
        # convert to byte array
        buffer = buffer.tobytes()
        # get size of the frame
        buffer_size = len(buffer)
        print(buffer_size)
        num_of_packs = 1
        if buffer_size > max_length:
            num_of_packs = math.ceil(buffer_size/max_length)

        frame_info = {"packs":num_of_packs}

        # send the number of packs to be expected
        print("Number of packs:", num_of_packs)
        sock.sendto(pickle.dumps(frame_info), (host, port))
        
        left = 0
        right = max_length

        for i in range(num_of_packs):
            print("left:", left)
            print("right:", right)

            # truncate data to send
            data = buffer[left:right]
            left = right
            right += max_length

            # send the frames accordingly
            sock.sendto(data, (host, port))
    
    ret, frame = cap.read()

print("done")

以下是服务器的代码 以下

from threading import Thread
import cv2
import socket
import math
import pickle
import sys
import numpy as np
import json


def sendToClient(threadNum,hostRec,portRec,hostSend,portSend):
    max_length = 65540
    sockRec = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    sockRec.bind((hostRec,portRec))
    sockSend = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  
    while True:
        data, address = sockRec.recvfrom(max_length)
        print("Received data on "+str(threadNum))
        sockSend.sendto(data,(hostSend,portSend))

def main():
    host = '0.0.0.0'
    ports = [6000,6001,6002]
    
    thread1 = Thread(name = 'Thread-1',target = sendToClient, args=('thread-1',host,ports[0],'127.0.0.1',7000))
    thread2 = Thread(name = 'Thread-2',target = sendToClient, args=('thread-2',host,ports[1],'127.0.0.1',7001))
    thread3 = Thread(name = 'Thread-3',target = sendToClient, args=('thread-3',host,ports[2],'127.0.0.1',7002))

    thread1.daemon = True
    thread2.daemon = True
    thread3.daemon = True

    thread1.start()
    thread2.start()
    thread3.start()

    thread1.join()
    thread2.join()
    thread3.join()

main()

是接收方的代码

from concurrent.futures import thread
import cv2
import socket
import pickle
import numpy as np
from threading import Thread

def display(threadNum,host,port,videoPlayerInfo):
    max_length = 65540
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind((host, port))

    frame_info = None
    buffer = None
    frame = None

    print("-> waiting for connection on thread "+ str(threadNum))

    while True:
        data, address = sock.recvfrom(max_length)
        print("Received Data from "+str(threadNum))
        if len(data) < 100:
            frame_info = pickle.loads(data)

            if frame_info:
                nums_of_packs = frame_info["packs"]

                for i in range(nums_of_packs):
                    data, address = sock.recvfrom(max_length)

                    if i == 0:
                        buffer = data
                    else:
                        buffer += data

                frame = np.frombuffer(buffer, dtype=np.uint8)
                frame = frame.reshape(frame.shape[0], 1)

                frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
                frame = cv2.flip(frame, 1)
                
                if frame is not None and type(frame) == np.ndarray:
                    cv2.imshow(videoPlayerInfo, frame)
                    if cv2.waitKey(1) == 27:
                        break
                    
    print("goodbye from "+str(threadNum))

def main():
    host = "0.0.0.0"
    ports = [7000,7001,7002]
    thread1 = Thread(name = 'Thread-1',target = display, args=('thread-1',host,ports[0],'player1'))
    thread2 = Thread(name = 'Thread-2',target = display, args=('thread-2',host,ports[1],'player2'))
    thread3 = Thread(name = 'Thread-3',target = display, args=('thread-3',host,ports[2],'player3'))

    thread1.daemon = True
    thread2.daemon = True
    thread3.daemon = True   

    thread1.start()
    thread2.start()
    thread3.start() 

    thread1.join()
    thread2.join()
    thread3.join()

main()

请帮助我如何发送元数据,因为我是 python 套接字编程的新手

I have client server application which is sending udp server from one client to other in python. In the UDP stream I am sending a video by reading the video from disk using CV2 and then sending it using UDP.

I want to add some meta data along with the video stream. I want to add the frame number, timestamp, video name, playback time and the starting time to the video stream.

How can I do the same. I have created multiple UDP sockets which are managed by using threads.

The following is the code for sender

import cv2
import socket
import math
import pickle
import sys

max_length = 65000
host = '127.0.0.1'
port = 6000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

cap = cv2.VideoCapture("input2.mp4")
print(cap)
ret, frame = cap.read()
print(ret)
while ret:
    # compress frame
    retval, buffer = cv2.imencode(".jpg", frame)

    if retval:
        # convert to byte array
        buffer = buffer.tobytes()
        # get size of the frame
        buffer_size = len(buffer)
        print(buffer_size)
        num_of_packs = 1
        if buffer_size > max_length:
            num_of_packs = math.ceil(buffer_size/max_length)

        frame_info = {"packs":num_of_packs}

        # send the number of packs to be expected
        print("Number of packs:", num_of_packs)
        sock.sendto(pickle.dumps(frame_info), (host, port))
        
        left = 0
        right = max_length

        for i in range(num_of_packs):
            print("left:", left)
            print("right:", right)

            # truncate data to send
            data = buffer[left:right]
            left = right
            right += max_length

            # send the frames accordingly
            sock.sendto(data, (host, port))
    
    ret, frame = cap.read()

print("done")

The following is the code for server

from threading import Thread
import cv2
import socket
import math
import pickle
import sys
import numpy as np
import json


def sendToClient(threadNum,hostRec,portRec,hostSend,portSend):
    max_length = 65540
    sockRec = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    sockRec.bind((hostRec,portRec))
    sockSend = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  
    while True:
        data, address = sockRec.recvfrom(max_length)
        print("Received data on "+str(threadNum))
        sockSend.sendto(data,(hostSend,portSend))

def main():
    host = '0.0.0.0'
    ports = [6000,6001,6002]
    
    thread1 = Thread(name = 'Thread-1',target = sendToClient, args=('thread-1',host,ports[0],'127.0.0.1',7000))
    thread2 = Thread(name = 'Thread-2',target = sendToClient, args=('thread-2',host,ports[1],'127.0.0.1',7001))
    thread3 = Thread(name = 'Thread-3',target = sendToClient, args=('thread-3',host,ports[2],'127.0.0.1',7002))

    thread1.daemon = True
    thread2.daemon = True
    thread3.daemon = True

    thread1.start()
    thread2.start()
    thread3.start()

    thread1.join()
    thread2.join()
    thread3.join()

main()

The following is the code for receiver

from concurrent.futures import thread
import cv2
import socket
import pickle
import numpy as np
from threading import Thread

def display(threadNum,host,port,videoPlayerInfo):
    max_length = 65540
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind((host, port))

    frame_info = None
    buffer = None
    frame = None

    print("-> waiting for connection on thread "+ str(threadNum))

    while True:
        data, address = sock.recvfrom(max_length)
        print("Received Data from "+str(threadNum))
        if len(data) < 100:
            frame_info = pickle.loads(data)

            if frame_info:
                nums_of_packs = frame_info["packs"]

                for i in range(nums_of_packs):
                    data, address = sock.recvfrom(max_length)

                    if i == 0:
                        buffer = data
                    else:
                        buffer += data

                frame = np.frombuffer(buffer, dtype=np.uint8)
                frame = frame.reshape(frame.shape[0], 1)

                frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
                frame = cv2.flip(frame, 1)
                
                if frame is not None and type(frame) == np.ndarray:
                    cv2.imshow(videoPlayerInfo, frame)
                    if cv2.waitKey(1) == 27:
                        break
                    
    print("goodbye from "+str(threadNum))

def main():
    host = "0.0.0.0"
    ports = [7000,7001,7002]
    thread1 = Thread(name = 'Thread-1',target = display, args=('thread-1',host,ports[0],'player1'))
    thread2 = Thread(name = 'Thread-2',target = display, args=('thread-2',host,ports[1],'player2'))
    thread3 = Thread(name = 'Thread-3',target = display, args=('thread-3',host,ports[2],'player3'))

    thread1.daemon = True
    thread2.daemon = True
    thread3.daemon = True   

    thread1.start()
    thread2.start()
    thread3.start() 

    thread1.join()
    thread2.join()
    thread3.join()

main()

Please help me how I can send the meta data as I am new to socket programming in python

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文