我的目标是将本地视频内容/桌面截屏重新流式传输到我需要在 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 命令。
非常感谢
最佳答案
为了提高视频质量,您必须使用多线程。它会解决它。我将给您一个可以使用的示例。
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()
关于python - FFMPEG - 本地视频到 UDP 流式传输到 OpenCV - 视频质量下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61439529/