video_player.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. import cv2
  3. import pygame
  4. import time
  5. class Vopen():
  6. def __init__(self):
  7. self.fname = '/home/user/Downloads/video.mp4'
  8. self.fname = '/home/user/Downloads/video.ogv'
  9. self.fname = '/home/user/Downloads/bbb_sunflower_480x320.mp4'
  10. self.x = 0
  11. self.y = 0
  12. self.im = None
  13. self.init()
  14. def init(self):
  15. print(self,"init()")
  16. cap = cv2.VideoCapture(self.fname)
  17. self.cap = cap
  18. self.success, self.img = self.cap.read()
  19. try:
  20. self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
  21. except:pass
  22. self.shape = self.img.shape[1::-1]
  23. def read(self):
  24. print(self,"read()")
  25. self.success, self.img = self.cap.read()
  26. print(self.success)
  27. try:
  28. self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
  29. except Exception as e:
  30. print("exception 432",e)
  31. def next(self):
  32. print(self,"play",time.time())
  33. self.read()
  34. try:
  35. self.im = pygame.image.frombuffer(self.img.tobytes(), self.shape, "RGB")
  36. # wn.blit(im, (self.x, self.y))
  37. except AttributeError as e:
  38. print("except",e)
  39. time.sleep(1)
  40. self.init()
  41. self.success,self.shape = v.init()
  42. def draw(self,wn):
  43. if self.success:
  44. wn.blit(self.im, (self.x, self.y))
  45. v = Vopen()
  46. #success,shape = v.init()
  47. shape = [300,300]
  48. if v.shape:
  49. shape = v.shape
  50. wn = pygame.display.set_mode(v.shape,pygame.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  51. #wn = pygame.display.set_mode(shape)
  52. window = wn
  53. clock = pygame.time.Clock()
  54. def grab(x=55,y=55,w=60,h=60):
  55. # usage
  56. # sub = grab()
  57. # window.blit(sub, (500,10))
  58. rect = pygame.Rect(x, y, w, h)
  59. sub = window.subsurface(rect)
  60. #pixArray = pygame.PixelArray(screen)
  61. crop = pygame.Surface((w,h))
  62. crop.blit(sub, (0,0))
  63. return crop
  64. max_frame=0
  65. success=1
  66. while v.success and success:
  67. for event in pygame.event.get():
  68. if event.type == pygame.QUIT:
  69. success = False
  70. if max_frame < 300:
  71. v.next()
  72. max_frame+=1
  73. #print(i)
  74. v.draw(wn) #,x=0,y=0)
  75. sub = grab()
  76. window.blit(sub, (500,10))
  77. pygame.display.update()
  78. clock.tick(60)
  79. pygame.quit()