video_player.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import cv2
  2. import pygame
  3. import time
  4. class Vopen():
  5. def __init__(self):
  6. cap = cv2.VideoCapture('/home/user/Downloads/video.mp4')
  7. #cap = cv2.VideoCapture('/home/user/Downloads/video.ogv')
  8. self.cap = cap
  9. def init(self):
  10. success, img = self.cap.read()
  11. self.shape = img.shape[1::-1]
  12. return success,self.shape
  13. def read(self):
  14. success, img = self.cap.read()
  15. return success,img
  16. v = Vopen()
  17. success,shape = v.init()
  18. wn = pygame.display.set_mode(shape)
  19. window = wn
  20. clock = pygame.time.Clock()
  21. def grab(x=55,y=55,w=60,h=60):
  22. # usage
  23. # sub = grab()
  24. # window.blit(sub, (500,10))
  25. rect = pygame.Rect(x, y, w, h)
  26. sub = window.subsurface(rect)
  27. #pixArray = pygame.PixelArray(screen)
  28. crop = pygame.Surface((w,h))
  29. crop.blit(sub, (0,0))
  30. return crop
  31. while success:
  32. clock.tick(60)
  33. success, img = v.read()
  34. for event in pygame.event.get():
  35. if event.type == pygame.QUIT:
  36. success = False
  37. try:
  38. wn.blit(pygame.image.frombuffer(img.tobytes(), shape, "RGB"), (0, 0))
  39. #wn.blit(pygame.image.frombuffer(img.tobytes(), shape, "BGR"), (0, 0))
  40. except AttributeError as e:
  41. print("except",e)
  42. time.sleep(1)
  43. v = Vopen()
  44. success,shape = v.init()
  45. sub = grab()
  46. window.blit(sub, (500,10))
  47. pygame.display.update()
  48. pygame.quit()