其他分享
首页 > 其他分享> > OpenCV学习笔记

OpenCV学习笔记

作者:互联网

"""
参数1 即将保存的文件路径
参数2 VideoWriter_fourcc为视频编解码器
    fourcc意为四字符代码(Four-Character Codes),顾名思义,该编码由四个字符组成,下面是VideoWriter_fourcc对象一些常用的参数,注意:字符顺序不能弄混
    cv2.VideoWriter_fourcc('I', '4', '2', '0'),该参数是YUV编码类型,文件名后缀为.avi 
    cv2.VideoWriter_fourcc('P', 'I', 'M', 'I'),该参数是MPEG-1编码类型,文件名后缀为.avi 
    cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'),该参数是MPEG-4编码类型,文件名后缀为.avi 
    cv2.VideoWriter_fourcc('T', 'H', 'E', 'O'),该参数是Ogg Vorbis,文件名后缀为.ogv 
    cv2.VideoWriter_fourcc('F', 'L', 'V', '1'),该参数是Flash视频,文件名后缀为.flv
    cv2.VideoWriter_fourcc('m', 'p', '4', 'v')    文件名后缀为.mp4
参数3 为帧播放速率
参数4 (width,height)为视频帧大小

 

import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc('F', 'L', 'V', '1')
out = cv2.VideoWriter('a.flv',fourcc,20.0,(640,480))
while(cap.isOpened()):
    ret,frame = cap.read()
    if ret==True:
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
out.release()
cap.release()
cv2.destroyAllWindows()
import numpy as np
import cv2
cap = cv2.VideoCapture('anew.flv')
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
fps = cap.get(cv2.CAP_PROP_FPS)
print(fps)
#视频图像的宽度
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(frame_width)
print(frame_height)
out = cv2.VideoWriter('hopenow.avi',fourcc,fps,(frame_width,frame_height))
while(True):
    ret,frame = cap.read()
    if ret == True:
        frame = cv2.flip(frame,1)
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(25) & 0xff == ord('q'):
            break
    else:
        break
out.release()
cap.release()
cv2.destroyAllWindows()

 

 

标签:frame,cap,cv2,笔记,学习,OpenCV,fourcc,out,VideoWriter
来源: https://blog.csdn.net/winggyn/article/details/113180087