gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:51:19 26 4
gpt4 key购买 nike

我正在使用 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() 正在阻塞。这可能代价高昂并且会导致延迟,因为主线程必须等待直到获得帧。通过将此操作放入一个单独的线程中,该线程仅专注于抓取帧并在主线程中处理/保存帧,它可以显着提高性能。您也可以尝试使用其他编解码器,但使用 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 streaming from camera - multithreading, timestamps

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

  3. How to capture multiple camera streams with OpenCV?

  4. OpenCV real time streaming video capture is slow. How to drop frames or get synced with real time?

  5. Storing RTSP stream as video file with OpenCV VideoWriter

  6. OpenCV video saving

  7. Python OpenCV multiprocessing cv2.VideoCapture mp4

关于python - 使用 OpenCV VideoWriter 将 RTSP 流存储为视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55141315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com