techoverload/slave/video.py

65 lines
1.8 KiB
Python

from random import randint
import cv2
import multiprocessing
import time
import sys
class Video:
def __init__(self, x, y):
self.x = x
self.y = y
def start(self, path):
self.cap = cv2.VideoCapture(path)
# Check if camera opened successfully
if (self.cap.isOpened()== False):
print("Error opening video file")
totalframecount= int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
print("The total number of frames in this video is ", totalframecount)
i = randint(0, int(totalframecount * 0.9))
self.cap.set(cv2.CAP_PROP_POS_FRAMES, i)
# Read until video is completed
fps = self.cap.get(cv2.CAP_PROP_FPS)
delay = 1/fps * 1000
print(fps, delay)
i = 0
playframes = randint(300, 500)
while(self.cap.isOpened() ):
i += 1
if i > playframes:
break
# Capture frame-by-frame
ret, frame = self.cap.read()
if ret == True:
# Display the resulting frame
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.moveWindow("window", self.x, self.y)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow("window", frame)
if cv2.waitKey(int(delay)) & 0xFF == ord('q'):
self.cap.release()
cv2.destroyAllWindows()
sys.exit(0)
# Break the loop
else:
break
self.cap.release()
# Closes all the frames
#cv2.destroyAllWindows()
if __name__ == "__main__":
video = Video(0, 0)
video.start("slave/videos/sample-mp4-file.mp4")