Pythone OpenCV学习笔记之:视频文件读取与保存
Pythone OpenCV学习笔记之:视频文件读取与保存
IOTService 发表于1年前
Pythone OpenCV学习笔记之:视频文件读取与保存
  • 发表于 1年前
  • 阅读 13
  • 收藏 1
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

摘要: 代码地址:https://github.com/juxiangwu/tensorflow-learning/tree/master/opencv/core
# -*- coding: utf-8 -*-
# 读取和保存视频

import cv2 as cv
import numpy as np

def decode_fourcc(v):
    v = int(v)
    return "".join([chr((v >> 8 * i) & 0xFF) for i in range(4)])

videoCapture = cv.VideoCapture('../../datas/videos/video.avi')

#获取码率及尺寸
fs = videoCapture.get(cv.CAP_PROP_FPS)
fourccD = decode_fourcc(videoCapture.get(cv.CAP_PROP_FOURCC))
fourcc = videoCapture.get(cv.CAP_PROP_FOURCC)
size = (int(videoCapture.get(cv.CAP_PROP_FRAME_WIDTH)),
        int(videoCapture.get(cv.CAP_PROP_FRAME_HEIGHT)))
print('fs = ',fs)
print('size = ',size)
print('fourccD = ',fourccD)
print('fourcc = ',fourcc)
fourcc = cv.VideoWriter_fourcc(*'XVID')
# I420-avi, MJPG-mp4
videoWriter = cv.VideoWriter(filename='../../datas/videos/video-out.avi',
                             fourcc=fourcc,
                             fps=int(fs), frameSize=size)
img_empty = np.zeros((512,512,3),np.uint8)
while videoCapture.isOpened():
    sucess,frame = videoCapture.read()
    if sucess == True:
        gray = cv.cvtColor(frame,cv.COLOR_RGB2GRAY)
        videoWriter.write(frame)
        cv.imshow("video",frame)
        cv.imshow("video-gray",gray)
    else:
        print("read frame failed")
        cv.imshow("video",img_empty)
        #break
    key = cv.waitKey(10) & 0xFF
    if key == 27:
        break
cv.destroyAllWindows()
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 9
博文 44
码字总数 8719
×
IOTService
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: