Opencv-Python(二) 读取, 写入/出视频

注:如果运行本文中的代码遇到视频无法读取,写出的问题请参考这篇文章的第二大部分的第五第六步

VedioCapture(fileename)–视频读取

使用如下代码可以创建一个名为vedioCapture的opencv2的视频处理句柄(打开视频文件)

videoCapture = cv2.VideoCapture('1.mp4')

参数为视频的名字(或者路径+名字)注意单引号。当使用的参数为一个数字0的时候代表从摄像头获取视频。

videoCapture = cv2.VideoCapture(0)

创建了这个句柄之后可以使用如下语句读取视频的下一帧,第一个返回值为是否成功获取视频帧,第二个返回值为返回的视频帧。

success, frame = videoCapture.read()

使用如下语句可以获得视频文件的属性fps以及size

fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS)
size = (int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
        int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))

cv2.VideoWriter(filename, fourcc, fps, framesize, isColor)–视频写入/出

使用这个函数可以创建一个用于写出视频文件的句柄,第一个参数为写出的视频文件的名字/路径,第二个参数为写出视频的编码格式,第三个参数为写出视频的fps值,第四个参数为写出视频的画面大小,最后一个参数为设置写出视频是否为彩色视频,可以省略,默认为彩色。
其中第二个参数可以选取以下值(分别对应不同的编码方式):

cv2.cv.CV_FOURCC('P', 'I', 'M', '1') = MPEG-1 codec
cv2.cv.CV_FOURCC('M', 'J', 'P', 'G') = motion-jpeg codec
cv2.cv.CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
cv2.cv.CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
cv2.cv.CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
cv2.cv.CV_FOURCC('U', '2', '6', '3') = H263 codec
cv2.cv.CV_FOURCC('I', '2', '6', '3') = H263I codec
cv2.cv.CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
videoWriter = cv2.VideoWriter('oto_other.avi', cv2.cv.CV_FOURCC('X', '2', '6', '4'), fps, size)

这里创建了一个名为videoWriter的句柄,写出文件指向“oto_other.avi”,编码方式为x264,fps和size和之前读入的视频一致。
创建完句柄之后使用如下语句即可向视频文件中写出一帧视频

videoWriter.write(frame)  # write one frame into the output video

使用的所有代码如下:

只需要在文件所在的目录下放一个名为1.mp4的视频文件,运行这个文件即可播放并复制这个视频到一个名为oto_other.avi的文件。

# -*- coding: utf-8 -*-
# =================================================================
# windows10, PyCharm, anaconda2, python 2.7.13, opencv 2.4.13
# 2017-12-17
# powered by tuzixini
# attention: you might need install the encoder fist, like x264vfw
# =================================================================

import cv2  # import opencv

# use opencv open the video 1.mp4
videoCapture = cv2.VideoCapture('1.mp4')
# get the inf of vedio,fps and size
fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS)
size = (int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
        int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))

# point out how to encode videos
# I420-avi=>cv2.cv.CV_FOURCC('X','2','6','4');
# MP4=>cv2.cv.CV_FOURCC('M', 'J', 'P', 'G')
# The mp4 encoder in my computer do not work,so i just use X264
videoWriter = cv2.VideoWriter('oto_other.avi', cv2.cv.CV_FOURCC('X', '2', '6', '4'), fps, size)
# read one frame from the video
success, frame = videoCapture.read()
while success:
    cv2.imshow("Oto Video", frame)  # display this frame
    cv2.waitKey(int(fps))  # delay
    videoWriter.write(frame)  # write one frame into the output video
    success, frame = videoCapture.read()  # get the next frame of the video
# some process after finish all the program
cv2.destroyAllWindows()     # close all the widows opened inside the program
videoCapture.release        # release the video read/write handler
videoWriter.release

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值