从文件中播放视频 VideoCapture_read_avi.py

来源:互联网 发布:网络部各人员提成方案 编辑:程序博客网 时间:2024/05/18 03:15


# -*- coding: utf-8 -*-
"""
Created on Fri Jan 3 21:06:22 2014

@author: duan
"""

import numpy as np
import cv2

cap = cv2.VideoCapture('output.avi')

width=cap.get(3)
height=cap.get(4)
print width
print height

ret=cap.set(3,320)
ret=cap.set(4,240)
width=cap.get(3)
height=cap.get(4)
print width
print height

if cap.isOpened():
  while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    if ret==True:
      #frame = cv2.flip(frame,0)
      # Our operations on the frame come here
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      gray = cv2.flip(gray,0)
      # Display the resulting frame
      cv2.imshow('frame',gray)
    key=cv2.waitKey(25)
    if key & 0xFF == ord('q'):
     break
    elif key & 0xFF == ord('a'):
     ret=cap.set(3,640)
     ret=cap.set(4,480)
    elif key & 0xFF == ord('s'):
     ret=cap.set(3,320)
     ret=cap.set(4,240)
  # When everything done, release the capture
  cap.release()
  cv2.destroyAllWindows()
else:
  print 'cap is not Opened!'


从文件中播放视频
与从摄像头中捕获一样,你只需要把设备索引号改成视频文件的名字。在
播放每一帧时,使用cv2.waiKey() 设置适当的持续时间。如果设置的太低视
频就会播放的非常快,如果设置的太高就会播放的很慢(你可以使用这种方法
控制视频的播放速度)。通常情况下25 毫秒就可以了。


0 0
原创粉丝点击