opencv读取流视频保存为本地文件

 

import cv2
# from matplotlib import pyplot as plt

# 通过cv2中的类获取视频流操作对象cap
cap = cv2.VideoCapture(r'http://ivi.bupt.edu.cn/hls/cctv1.m3u8')
# 调用cv2方法获取cap的视频帧(帧:每秒多少张图片)
fps = cap.get(cv2.CAP_PROP_FPS)
print(fps)
# 获取cap视频流的每帧大小
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print(size)

# 定义编码格式mpge-4
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')
# 定义视频文件输入对象
dir = 0

def save(dir):
    # 获取视频流打开状态
    if cap.isOpened():
        rval, frame = cap.read()
        print('ture')
    else:
        rval = False
        print('False')

    tot = 1
    c = 1
    # 循环使用cv2的read()方法读取视频帧
    while rval:
        rval, frame = cap.read()
        # cv2.imshow('live', frame)
        # 每间隔200帧保存一张图像帧
        if tot % 200 == 0 :
            print('tot=', tot)
            # cv2.imwrite('cut/'+'cut_'+str(c)+'.jpg',frame)
            # c += 1
        tot += 1
        # print('tot=', tot)
        # 使用VideoWriter类中的write(frame)方法,将图像帧写入视频文件
        outVideo.write(frame)
        cv2.waitKey(25)
        # if cv2.waitKey(1) & 0xFF == ord('q'):
        #     break
        if tot % 12000 == 0 :
            dir += 1
            print(dir)
            outVideo.release()
            break
    return dir

while True:

    outVideo = cv2.VideoWriter(r'video/'+str(dir) + 'saveDir.avi', fourcc, fps, size)
    dir = save(dir)
    if dir == 36 :
        print('输出结束')
        break
cap.release()
outVideo.release()
cv2.destroyAllWindows()

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值