使用 OpenCV VideoWriter 将 RTSP 流存储为视频文件

Storing RTSP stream as video file with OpenCV VideoWriter

我正在使用 OpenCV 开发一个 Python 模块,它连接到 RTSP 流以对视频执行一些预处理(主要是降低 fps 和分辨率),然后将其存储在文件系统中.

但是,即使在尝试了几种编解码器之后,寻找类似的发展...我总是以一个空视频结束。我看过另一个线程 (cv::VideoWriter yields unreadable video),它可能类似,但是是在 C++ 上开发的。

有人做过这方面的工作吗?我一般使用一个示例RTSP流作为参考,比如rtsp://freja.hiof.no:1935/rtplive/definst/hessdalen03.stream,可以接收甚至观看来自 VLC 的流正确。

我看过很多帖子讨论如何从 RTSP 流中捕获视频,或者如何使用 VideoWriters 和 VideoReaders 类 和视频文件,但几乎没有将两者结合起来。

非常感谢任何帮助:)谢谢!!


编辑 1:用于存储帧的示例代码。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cv2
import numpy

# Test frame.
width, height = 400, 300
width_2, height_2 = int(width / 2), int(height / 2)
frame = numpy.zeros((height, width, 3), numpy.uint8)
cv2.rectangle(frame, (0, 0), (width_2, height_2), (255, 0, 0), cv2.FILLED)
cv2.rectangle(frame, (width_2, height_2), (width, height), (0, 255, 0), cv2.FILLED)

frames = [frame for _ in range(100)]
fps = 25

# Define the codec.
#fourcc = cv2.VideoWriter_fourcc(*'X264')
#fourcc = cv2.VideoWriter_fourcc(*'XVID')
fourcc = cv2.VideoWriter_fourcc(*'MJPG')

# Create VideoWriter object
out = cv2.VideoWriter(filename='video.avi',
                      fourcc=fourcc,
                      apiPreference=cv2.CAP_FFMPEG,
                      fps=float(fps),
                      frameSize=(width, height),
                      isColor=True)

result = 0
for frame in frames:
    result += 0 if out.write(frame) is None else 1
print(result)

out.release()

编辑 2:解决方案

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cv2
import numpy

# Test frame.
width, height = 400, 300
width_2, height_2 = int(width / 2), int(height / 2)

frame1 = numpy.zeros((height, width, 3), numpy.uint8)
cv2.rectangle(frame1, (0, 0), (width_2, height_2), (255, 0, 0), cv2.FILLED)
cv2.rectangle(frame1, (width_2, height_2), (width, height), (0, 255, 0), cv2.FILLED)
cv2.imwrite('frame1.jpg', frame1)

frame2 = numpy.zeros((height, width, 3), numpy.uint8)
cv2.rectangle(frame2, (width_2, 0), (width, height_2), (255, 0, 0), cv2.FILLED)
cv2.rectangle(frame2, (0, height_2), (width_2, height), (0, 255, 0), cv2.FILLED)
cv2.imwrite('frame2.jpg', frame2)

range1 = [frame1 for _ in range(10)]
range2 = [frame2 for _ in range(10)]
frames = range1 + range2 + range1 + range2 + range1
fps = 2

# Define the codec.
fourcc = cv2.VideoWriter_fourcc(*'MJPG')

# Create VideoWriter object
out = cv2.VideoWriter('video.avi', fourcc, float(fps), (width, height))

for frame in frames:
    out.write(frame)

out.release()

这是一个 RTSP 流到视频的小部件。我建议创建另一个线程来获取帧,因为 cv2.VideoCapture.read() 正在阻塞。这可能代价高昂并且会导致延迟,因为主线程必须等待直到获得帧。通过将此操作放入一个单独的线程中,该线程仅专注于抓取帧和 processing/saving 主线程中的帧,它显着提高了性能。您也可以尝试使用其他编解码器,但使用 MJPG 应该是安全的,因为它内置于 OpenCV 中。我使用我的 IP 摄像机流并将帧保存到 output.avi。请务必将 rtsp_stream_link 更改为您自己的 RTSP 流 link。 :)

from threading import Thread
import cv2

class RTSPVideoWriterObject(object):
    def __init__(self, src=0):
        # Create a VideoCapture object
        self.capture = cv2.VideoCapture(src)

        # Default resolutions of the frame are obtained (system dependent)
        self.frame_width = int(self.capture.get(3))
        self.frame_height = int(self.capture.get(4))
        
        # Set up codec and output video settings
        self.codec = cv2.VideoWriter_fourcc('M','J','P','G')
        self.output_video = cv2.VideoWriter('output.avi', self.codec, 30, (self.frame_width, self.frame_height))

        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()

    def show_frame(self):
        # Display frames in main program
        if self.status:
            cv2.imshow('frame', self.frame)
        
        # Press Q on keyboard to stop recording
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.capture.release()
            self.output_video.release()
            cv2.destroyAllWindows()
            exit(1)

    def save_frame(self):
        # Save obtained frame into video output file
        self.output_video.write(self.frame)

if __name__ == '__main__':
    rtsp_stream_link = 'your stream link!'
    video_stream_widget = RTSPVideoWriterObject(rtsp_stream_link)
    while True:
        try:
            video_stream_widget.show_frame()
            video_stream_widget.save_frame()
        except AttributeError:
            pass

相关 camera/IP/RTSP/streaming、FPS、视频、线程和多处理帖子

  1. Video Streaming from IP Camera in Python Using OpenCV cv2.VideoCapture