【问题标题】:convert yuv420p raw data to image opencv将 yuv420p 原始数据转换为图像 opencv
【发布时间】:2021-02-03 22:59:35
【问题描述】:

我有来自 rtmp 服务器的原始数据,像素格式为 yuv420p

我使用管道来读取数据。但我不知道如何将原始数据解码为图像。

command = ['ffmpeg']
command.extend(["-loglevel", "fatal", "-i", 'rtmp://localhost/live/stream', "-f", "flv", "-pix_fmt" , 'yuv420p', '-vcodec', 'h264', "-"])
self.process = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE)
self.output = self.process.stdout
self.fs = width*height*3 // 2
while True:
    data = self.output.read(self.fs)

我已经尝试像这样解码enter link description here

但结果是enter image description here

谁能帮我解决这个问题?

【问题讨论】:

    标签: python opencv ffmpeg


    【解决方案1】:

    我不是ffmpeg 方面的专家,所以我会听从任何更了解的人的意见,如果证明不正确,我会删除我的答案。

    据我所知,您有一个 RTMP 流,您想将其摄取到 OpenCV 中。 OpenCV 使用带有 BGR 排序的 Numpy 数组来存储图像 - 显然还有视频帧,它们只是一个接一个的大量图像。所以,我建议你问ffmpeg 将 Flash 视频流转换为 OpenCV 想要的:

    ffmpeg <RTMP INPUT STUFF> -pix_fmt bgr24 -f rawvideo -
    

    然后更改它,因为它现在是 BGR888:

    self.fs = width * height * 3
    

    因为我没有可用的 RTMP 源,所以我生成了一个这样的测试流:

    # Generate raw video stream to read into OpenCV    
    ffmpeg -f lavfi -i testsrc=duration=10:size=640x480:rate=30 -pixel_format rgb24 -f rawvideo -
    

    然后我通过管道将其导入 Python:

    ffmpeg -f lavfi -i testsrc=duration=10:size=640x480:rate=30 -pixel_format rgb24 -f rawvideo - | ./PlayRawVideo
    

    Python 程序PlayRawVideo 如下所示:

    #!/usr/bin/env python3
    
    import numpy as np
    import cv2
    import sys
    
    # Set width and height
    w, h = 640, 480
    
    while True:
        data = sys.stdin.buffer.read(w * h *3)
        if len(data) == 0:
            break
        frame = np.frombuffer(data, dtype=np.uint8).reshape((h, w, 3))
        cv2.imshow("Stream", frame)
        cv2.waitKey(1)
        
    

    请注意,我必须使用 sys.stdin.buffer.read() 来获取原始二进制数据。

    【讨论】:

    • 原来我在 ffmpeg 命令中输入了错误的选项 -f。感谢您的回答
    猜你喜欢
    • 2019-08-14
    • 1970-01-01
    • 2018-01-08
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 2014-10-01
    相关资源
    最近更新 更多