如何使用OpenCV VideoWriter根据触发器来启动和停止保存视频帧

问题描述 投票:1回答:1

我正在构建一个通过RTSP记录IP摄像机的帧的应用程序。

我的引擎负责在Opencv VideoWriter正常工作的情况下将视频保存为mp4。我正在寻找的是创建一个startRecord和stopRecord类方法,它们将根据触发器分别开始和停止记录(这可能是我传递给线程的参数)。有谁知道做这种事情的最好方法吗?

这是我的课程:

from threading import Thread
import cv2
import time
import multiprocessing
import threading
class RTSPVideoWriterObject(object):
    def __init__(self, src=0):
        # Create a VideoCapture object
        self.capture = cv2.VideoCapture(src)

        # 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 endRecord(self):
        self.capture.release()
        self.output_video.release()
        exit(1)

    def startRecord(self,endRec):

        self.frame_width = int(self.capture.get(3))
        self.frame_height = int(self.capture.get(4))
        self.codec = cv2.VideoWriter_fourcc(*'mp4v')
        self.output_video = cv2.VideoWriter('fileOutput.mp4', self.codec, 30, (self.frame_width, self.frame_height))
        while True:          
            try:
                self.output_video.write(self.frame)
                if endRec:
                    self.endRecord()
            except AttributeError:
                pass




if __name__ == '__main__':

    rtsp_stream_link = 'rtsp://foo:192.5545....'
    video_stream_widget = RTSPVideoWriterObject(rtsp_stream_link)

    stop_threads = False
    t1 = threading.Thread(target = video_stream_widget.startRecord, args =[stop_threads]) 
    t1.start() 
    time.sleep(15)
    stop_threads = True

如您所见,我主要阅读框架并将它们存储在单独的线程中。然后,我开始记录(记录方法带有无限循环,因此受阻),然后在15秒后,我尝试传递“ stop_record”参数以正确停止记录。

部分代码来自Storing RTSP stream as video file with OpenCV VideoWriter

有人有主意吗?我读到很多内容,OpenCV对于多线程可能非常棘手]

N。

python multithreading opencv ffmpeg rtsp
1个回答
0
投票

而不是将参数传递给线程,请在类中使用内部标志来确定何时开始/停止记录。触发可能很简单,只需按下空格键即可开始/停止录制。按下空格键时,它将切换内部变量,例如,从self.recordTrue开始记录,而从False停止记录。具体来说,要检查何时按下空格键,可以检查从cv2.waitKey()返回的键值是否为32。如果要基于任何其他键来触发,请查看this post确定键代码。这是一个使用空格键开始/停止录制视频的简单示例:

from threading import Thread
import cv2

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

        # 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(*'mp4v')
        self.output_video = cv2.VideoWriter('output.mp4', 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)
            if self.record:
                self.save_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)
        # Press spacebar to start/stop recording
        elif key == 32:
            if self.record:
                self.record = False
                print('Stop recording')
            else:
                self.record = True
                print('Start recording')

    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()
        except AttributeError:
            pass
© www.soinside.com 2019 - 2024. All rights reserved.