Python OpenCV cap.read()解压压缩帧?
你好,Stack社区 我正在从IP摄像机流中读取帧,并将其存储在列表中,以便稍后创建视频文件。 我正在使用python OpenCV库,它工作得很好,但是。。 从IP摄像机发送的帧应该有h264压缩,但当我检查帧的大小时,对于4K流,它们是25 MB。我的内存很快就用完了。 这不是代码,但与之类似:Python OpenCV cap.read()解压压缩帧?,python,opencv,compression,mp4,Python,Opencv,Compression,Mp4,你好,Stack社区 我正在从IP摄像机流中读取帧,并将其存储在列表中,以便稍后创建视频文件。 我正在使用python OpenCV库,它工作得很好,但是。。 从IP摄像机发送的帧应该有h264压缩,但当我检查帧的大小时,对于4K流,它们是25 MB。我的内存很快就用完了。 这不是代码,但与之类似: import cv2 cap = cv2.VideoCapture(0) list = [] while(cap.isOpened()): ret, frame = cap.read()
import cv2
cap = cv2.VideoCapture(0)
list = []
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
list.append(frame)
cap.release()
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
for frm in list:
out.write(frm)
out.release()
cv2.destroyAllWindows()
看起来像ret,frame=cap.read打开框架?
这会对每个循环产生额外的处理,对于我使用脚本的意图来说是不必要的。有没有一种方法可以在不解包的情况下检索帧
请原谅我可能的无知。我构建了一个测试示例,用于使用将h264流读取到内存中 这个样本从一个文件中读取数据,我没有测试它的摄像头。 我还测试了从RTSP流读取的代码 以下是代码,请阅读注释:
import ffmpeg
import threading
import io
in_filename = 'test_vid.264' # Input file for testing (".264" or ".h264" is a convention for elementary h264 video stream file)
## Build synthetic video, for testing:
################################################
# ffmpeg -y -r 10 -f lavfi -i testsrc=size=192x108:rate=1 -c:v libx264 -crf 23 -t 50 test_vid.264
width, height = 192, 108
(
ffmpeg
.input('testsrc=size={}x{}:rate=1'.format(width, height), f='lavfi')
.output(in_filename, vcodec='libx264', crf=23, t=50)
.overwrite_output()
.run()
)
################################################
# Use ffprobe to get video frames resolution
###############################################
# p = ffmpeg.probe(in_filename, select_streams='v');
# width = p['streams'][0]['width']
# height = p['streams'][0]['height']
###############################################
# Stream the video as array of bytes (simulate the stream from the camera for testing)
###############################################
## https://github.com/kkroening/ffmpeg-python/blob/master/examples/README.md
#sreaming_process = (
# ffmpeg
# .input(in_filename)
# .video # Video only (no audio).
# .output('pipe:', format='h264')
# .run_async(pipe_stdout=True) # Run asynchronous, and stream to stdout
#)
###############################################
# Read from stdout in chunks of 16K bytes
def reader():
chunk_len_in_byte = 16384 # I don't know what is the optimal chunk size
in_bytes = chunk_len_in_byte
# Read until number of bytes read are less than chunk_len_in_byte
# Also stop after 10000 chucks (just for testing)
chunks_counter = 0
while (chunks_counter < 10000):
in_bytes = process.stdout.read(chunk_len_in_byte) # Read 16KBytes from PIPE.
stream.write(in_bytes) # Write data to In-memory bytes streams
chunks_counter += 1
if len(in_bytes) < chunk_len_in_byte:
break
# Use public RTSP Streaming for testing
# in_stream = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov"
# Execute ffmpeg as asynchronous sub-process.
# The input is in_filename, and the output is a PIPE.
# Note: you should replace the input from file to camera (I might forgot an argument that tells ffmpeg to expect h264 input stream).
process = (
ffmpeg
.input(in_filename) #.input(in_stream)
.video
.output('pipe:', format='h264')
.run_async(pipe_stdin=True, pipe_stdout=True)
)
# Open In-memory bytes streams
stream = io.BytesIO()
thread = threading.Thread(target=reader)
thread.start()
# Join thread, and wait for processes to end.
thread.join()
try:
process.wait(timeout=5)
except sp.TimeoutExpired:
process.kill() # Kill subprocess in case of a timeout (there might be a timeout because input stream still lives).
#sreaming_process.wait() # sreaming_process is used
stream.seek(0) #Seek to beginning of stream.
# Write result to "in_vid.264" file for testing (the file is playable).
with open("in_vid.264", "wb") as f:
f.write(stream.getvalue())
如果您觉得有用,我可以在代码之前添加更多的背景描述
请让我知道,如果代码是与相机工作,以及你必须修改什么 也许您应该使用其他工具来获取流并将其直接保存到文件中,而无需使用Python-ie.ffmpeg,vlc-Python对于我来说是必不可少的/使用其他工具获取所有图像,然后使用Python处理本地视频。如果你学会了如何使用ffmpeg,vlc,那么你就可以使用Python使用ffmpeg,vlc-ie.。非常感谢你的输入,我将尝试在我的脚本中实现这一点。为了了解更多,它与我目前使用的openCV方法有何不同?我知道ffmpeg似乎是人们推荐的。OpenCV将每个帧解码为通常的BGR格式。对于4K流,每个解码帧为3840*2160*3字节,用于存储。您需要对其进行编码[压缩]。使用FFmpeg,您可以选择获取编码的压缩h264流,该流平均可以小100到1000倍。您可以将编码后的视频流存储在磁盘或RAM中,然后进行解码,仅当您需要解码帧时才进行逐帧解码,例如在显示视频时。非常感谢,这正是我所需要的。非常感谢!