【问题标题】:FFMPEG - local video to UDP streaming to OpenCV - video quality degradedFFMPEG - 本地视频到 UDP 流式传输到 OpenCV - 视频质量下降
【发布时间】:2020-08-09 20:46:46
【问题描述】:

我的目标是将本地视频内容/桌面截屏重新流式传输到我需要在 Python 脚本上处理的 UDP 流。

这是我正在使用的 FFMPEG 脚本:

ffmpeg -re -i C:\Users\test\Downloads\out.ts -strict -2 -c:v copy -an -preset slower -tune stillimage -b 11200k -f rawvideo udp://127.0.0.1:5000

下面是应该读取流的简单 Python 脚本:

import cv2

cap = cv2.VideoCapture('udp://127.0.0.1:5000',cv2.CAP_FFMPEG)
if not cap.isOpened():
    print('VideoCapture not opened')
    exit(-1)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)  # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
print(str(width))
print(str(height))
while True:
    ret, frame = cap.read()
    imgray = frame[int(round((height/100)*70,0)):int(round((width/100)*42,0)), int(round((height/100)*74,0)):int(round((width/100)*54,0))]
    if not ret:
        print('frame empty')
        break
    cv2.imshow('image', imgray)
    if cv2.waitKey(1)&0XFF == ord('q'):
        break
cap.release()

我能够按预期可视化流视频的一部分,但我在视频质量下降方面面临很多问题,特别是视频伪影可能是由于缺少数据包处理:

这些也是我从脚本中得到的错误日志:

[h264 @ 0000026eb272f280] error while decoding MB 105 66, bytestream -21
[h264 @ 0000026eb2fcb740] error while decoding MB 100 53, bytestream -11
[h264 @ 0000026eb272f280] error while decoding MB 32 22, bytestream -11
[h264 @ 0000026ead9ee300] error while decoding MB 60 20, bytestream -25
[h264 @ 0000026eb27f00c0] error while decoding MB 9 62, bytestream -5
[h264 @ 0000026ead9ee780] error while decoding MB 85 44, bytestream -5
[h264 @ 0000026eb27f0800] error while decoding MB 64 25, bytestream -15
[h264 @ 0000026eb272f280] error while decoding MB 112 23, bytestream -17
[h264 @ 0000026eb2735200] error while decoding MB 30 21, bytestream -7

其实我不关心视频流畅度,我也可以降低FPS,重要的是视频质量。不确定我是否在脚本 python 部分做错了,或者我是否使用了错误的 FFMPEG 命令。

非常感谢

【问题讨论】:

  • 你终于找到解决方案了吗?在我将缓冲区大小从默认值提高到 65535(在 cv2.VideoCapture 中的 udp 端口​​地址之后)后,我克服了这个问题,它对我有用。
  • 不,我还没有找到解决方案。你是如何修改缓冲区大小的? cv2.set(cv2.CAP_PROP_BUFFERSIZE, 65535)?

标签: python opencv ffmpeg video-streaming http-live-streaming


【解决方案1】:

对于视频质量,您必须使用多线程。它会修复它。我会给你一个你可以学习的例子。

import threading
import queue
import cv2

q = queue.Queue()

def receive():
    cap = cv2.VideoCapture('udp://@127.0.0.1:5000?buffer_size=65535&pkt_size=65535&fifo_size=65535')
    ret, frame = cap.read()
    q.put(frame)
    while ret:
        ret, frame = cap.read()
        q.put(frame)

def display():
    while True:
        if q.empty() != True:
            frame = q.get()
            cv2.imshow('Video', frame)

        k = cv2.waitKey(1) & 0xff
        if k == 27:  # press 'ESC' to quit
            break

tr = threading.Thread(target=receive, daemon=True)
td = threading.Thread(target=display)

tr.start()
td.start()

td.join()

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2019-10-16
    • 1970-01-01
    • 2011-11-26
    • 2018-06-09
    • 2016-05-30
    相关资源
    最近更新 更多