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

8
我正在开发一个Python模块,使用OpenCV连接到一个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()

你能看到视频流吗?我的意思是用imshow而不是保存到磁盘上?你能创建一个带有虚拟图像的视频吗?比如一个全红的图像?你应该一步一步来,首先确保你能连接并获取图像,然后再专注于保存...你提供的链接看起来更像是rtmp链接而不是rtsp,通常端口554是用于rtsp,而1935是用于rtmp... - api55
嗨@api55!是的,我可以看到从RTSP流检索到的帧。实际上,目前我将它们存储为jpeg图像。但将它们存储为视频应该更有效率。但正如你所说,保存可能出了问题。我用numpy准备了一个虚拟帧,但视频仍然出错。请参见附加的代码,生成了一个240.924字节的视频,无法在VLC中显示。 - rafamartinc
1
太好了,现在你的问题有了一个MCVE。我知道获取fourcc +扩展名通常是一种可怕的经历...首先尝试查看您的计算机上安装了哪些编解码器,而不是通过FOURCC传递-1,然后会出现一个菜单,您可以选择一个编解码器。 - api55
它在Ubuntu上返回消息“OpenCV: FFMPEG: format avi / AVI(音频视频交错)”,以及包含489个标签的列表,例如“fourcc tag 0x34363248 / 'H264' codec_id 001B”。例如,对于那个问题,当尝试将其用作fourcc标记时,即使它在列表中,它也会回复“找不到编解码器ID 27的编码器:未找到”。 - rafamartinc
1个回答

10
这是一个用于视频小部件的RTSP流。建议创建另一个线程以获取帧,因为cv2.VideoCapture.read()是阻塞的。这可能很昂贵,并导致延迟,因为主线程必须等待它获得帧。通过将此操作放入专门用于抓取帧并在主线程中处理/保存帧的单独线程中,可以大大提高性能。您还可以尝试其他编解码器,但使用MJPG应该是安全的,因为它内置于OpenCV中。我使用了我的IP摄像头流,并将帧保存到output.avi。请务必将rtsp_stream_link更改为您自己的RTSP流链接。 :)

Output Video Screenshot

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

相关的摄像头/IP/RTSP/流媒体、FPS、视频、线程和多进程帖子

  1. Python OpenCV多线程实时摄像头视频流处理-时间戳

  2. 使用OpenCV cv2.VideoCapture在Python中从IP摄像头进行视频流处理

  3. 如何使用OpenCV捕获多个摄像头流?

  4. OpenCV实时视频流捕获速度慢。如何丢弃帧或与实时同步?

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

  6. OpenCV视频保存

  7. Python OpenCV多进程cv2.VideoCapture mp4


谢谢@nathancy !!! 我的VLC也显示了一些错误,即使视频已经被正确存储,所以有点混乱。但是多亏了你的代码,我可以解决所有问题。我会将修复后的代码添加到顶部。 - rafamartinc
很高兴能帮忙!如果您的视频帧出现卡顿,请尝试调整cv2.VideoWriter()的FPS参数,或者访问您的IP摄像头来调整摄像头的FPS设置。 - nathancy
谢谢您的帮助。是否有一种方法可以将音频记录在其他格式(如mp4)中? - sanjay
这超出了这个问题的范围,你应该开一个新的问题来问。 - nathancy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接