您的位置:首页 > 其它

从文件中播放视频 VideoCapture_read_avi.py

2016-12-11 15:06 525 查看
# -*- 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 毫秒就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: