video_player.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. cap = cv2.VideoCapture('/home/user/Downloads/bbb_sunflower_480x320.mp4')
  9. self.cap = cap
  10. def init(self):
  11. success, img = self.cap.read()
  12. try:
  13. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  14. except:pass
  15. self.shape = img.shape[1::-1]
  16. #print( img.shape[0:10] )
  17. return success,self.shape
  18. def read(self):
  19. success, img = self.cap.read()
  20. try:
  21. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  22. except:
  23. pass
  24. #cv2.CvtColor(img, im, cv.CV_BGR2RGB)
  25. #print( img.shape[0:10] )
  26. #print( self.shape )
  27. return success,img
  28. v = Vopen()
  29. success,shape = v.init()
  30. wn = pygame.display.set_mode(shape,pygame.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  31. #wn = pygame.display.set_mode(shape)
  32. window = wn
  33. clock = pygame.time.Clock()
  34. def grab(x=55,y=55,w=60,h=60):
  35. # usage
  36. # sub = grab()
  37. # window.blit(sub, (500,10))
  38. rect = pygame.Rect(x, y, w, h)
  39. sub = window.subsurface(rect)
  40. #pixArray = pygame.PixelArray(screen)
  41. crop = pygame.Surface((w,h))
  42. crop.blit(sub, (0,0))
  43. return crop
  44. while success:
  45. clock.tick(60)
  46. success, img = v.read()
  47. for event in pygame.event.get():
  48. if event.type == pygame.QUIT:
  49. success = False
  50. try:
  51. im = pygame.image.frombuffer(img.tobytes(), shape, "RGB")
  52. #im = pygame.image.frombuffer(img.tobytes(), shape, "BGR")
  53. wn.blit(im, (0, 0))
  54. except AttributeError as e:
  55. print("except",e)
  56. time.sleep(1)
  57. v = Vopen()
  58. success,shape = v.init()
  59. sub = grab()
  60. window.blit(sub, (500,10))
  61. pygame.display.update()
  62. pygame.quit()