我正在使用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()
答案 0 :(得分:5)
这是RTSP流到视频小部件。我建议创建另一个线程来获取帧,因为cv2.VideoCapture.read()
被阻止了。这可能会很昂贵,并且会导致等待时间,因为主线程必须等待直到获得帧为止。通过将此操作放到一个单独的线程中,该线程仅专注于抓取帧并在主线程中处理/保存帧,从而显着提高了性能。您还可以尝试使用其他编解码器,但是使用MJPG
应该是安全的,因为它内置于OpenCV中。我使用了IP摄像机流,并将帧保存到output.avi
。确保将rtsp_stream_link
更改为您自己的RTSP流链接。 :)
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