Python OpenCV | Playing Video From File














































Python OpenCV | Playing Video From File



Playing Video From File


It is same as capturing from Camera , just change camera index with video file name. Also while displaying the frame, use appropriate time for cv2.waitKey(). If it is too less, video will be very fast and if it is too high, video will be slow (Well, that is how you can display videos in slow motion). 30 milliseconds will be OK in normal cases.

Code :

import cv2
import numpy as np

class Capture_Video:
   
   
def __init__(self, video_name):
        self.video_name = str(video_name)

   
def OpenVideo(self):
        self.cap = cv2.VideoCapture(self.video_name)

       
while (self.cap.isOpened()):

           
# Capture frame-by-frame
            self.ret, self.frame = self.cap.read()

           
#Display the resulting frame
            cv2.imshow(
"Window", self.frame)

           
# Press q to Close
           
if cv2.waitKey(30) & 0xFF == ord('q'):
               
break
               
        self.cap.release()
        cv2.destroyAllWindows()

def main():
    obj = Capture_Video(
"asl.mp4")
    obj.OpenVideo()

if __name__ == "__main__":
    main()


Comments