opencv保存视频文件很大

目前接触到的视频保存方法有两种,一种是cv2.VideoWriter,另一种是imageio,对比下来imageio保存的录像文件要比opencv保存的小很多,具体原因暂时不清楚。
存在的问题:
imageio保存的视频不能获得视频总帧数这个属性

import imageio
import cv2
from tqdm import tqdm

class MyVideoWriter:
    def __init__(self, save_path, height=None, width=None, fps=25):
        self.save_path = save_path
        self.height = height
        self.width = width
        self.fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        self.cur_frame_id = -1
        self.fps = fps
        self.videoWriter = None

    def save(self, frame):
        self.cur_frame_id += 1
        if self.cur_frame_id == 0:
            if self.height is None or self.width is None:
                self.height, self.width, _ = frame.shape
            self.videoWriter = cv2.VideoWriter(self.save_path, self.fourcc, self.fps, (self.width, self.height))
        self.videoWriter.write(frame)

    def release(self):
        if self.videoWriter:
            self.videoWriter.release()
            self.videoWriter = None

    def __del__(self):
        self.release()

# reader = imageio.get_reader("io.mp4")
# for frame in tqdm(reader):
#     print("xxxx")

# print(imageio.get_reader("cv.mp4").get_meta_data())
# cv_reader = cv2.VideoCapture("cv.mp4")
# print(cv_reader.get(5), cv_reader.get(7))

fps = 10

cv_writer = MyVideoWriter("cv.mp4",fps=fps)
io_writer = imageio.get_writer("io.mp4", fps=fps)



rtsp = "rtsp://xxxxxxxxxxxxxxxxxxxxxx"

cap = cv2.VideoCapture(rtsp)
# cap = imageio.get_reader(rtsp)
# print(cap.get_meta_data())

for _ in range(1000):
    ret, frame = cap.read()
    cv_writer.save(frame)
    io_writer.append_data(frame[...,::-1])

cv_writer.release()
io_writer.close()
cap.release()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值