Ask Your Question

How to read/write video with OpenCV in Python?

asked 2012-06-08 08:50:21 -0500

Kirill Kornyakov gravatar image

I want to create a small test application, and I need to input/output video with OpenCV Python API. How can I do that?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-06-08 11:01:41 -0500

Andrey Kamaev gravatar image

Here is a minimal example:

import cv2
capture = cv2.VideoCapture("video.avi")
while True:
    ret, img =

    result = processFrame(img)

    cv2.imshow('some', result)
    if 0xFF & cv2.waitKey(5) == 27:
edit flag offensive delete link more


Does this still stand with the newest versions of opencv and python? (python 3.6.1 64 bit and opencv 3.2)

FullMetal863 gravatar imageFullMetal863 ( 2017-04-19 14:41:00 -0500 )edit

answered 2018-03-05 01:36:31 -0500

saideepthik gravatar image

yes it will work for updated versions also and you can try like this also. import cv2 import numpy as np

cap = cv2.VideoCapture("challenge.mp4") # if you are giving camera as input then give (0) to videoCapture. while(cap.isOpened()):

    # Take each frame
ret, image =

if ret:
          # do operations on image as per your application

if cv2.waitKey(25) & 0xFF == ord('q'):

cap.release() cv2.destroyAllWindows()

this all things you can get directly from opencv tutorial... try @Kirill

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2012-06-08 08:50:21 -0500

Seen: 8,825 times

Last updated: Mar 05 '18