video_player.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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()",self.fname)
  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. def draw(self,wn):
  42. if self.success:
  43. wn.blit(self.im, (self.x, self.y))
  44. v = Vopen()
  45. shape = [300,300]
  46. if v.shape:
  47. shape = v.shape
  48. wn = pygame.display.set_mode(v.shape,pygame.RESIZABLE)
  49. window = wn
  50. clock = pygame.time.Clock()
  51. pygame.display.set_caption('LibreLight VIDEO PLAYER')
  52. def grab(x=55,y=55,w=60,h=60):
  53. # usage
  54. # sub = grab()
  55. # window.blit(sub, (500,10))
  56. rect = pygame.Rect(x, y, w, h)
  57. sub = window.subsurface(rect)
  58. #pixArray = pygame.PixelArray(screen)
  59. crop = pygame.Surface((w,h))
  60. crop.blit(sub, (0,0))
  61. return crop
  62. max_frame=0
  63. success=1
  64. while v.success and success:
  65. for event in pygame.event.get():
  66. if event.type == pygame.QUIT:
  67. success = False
  68. if 1: #max_frame < 300:
  69. v.next()
  70. max_frame+=1
  71. #print(i)
  72. v.draw(wn) #,x=0,y=0)
  73. sub = grab()
  74. wn.blit(sub, (500,10))
  75. pygame.display.update()
  76. clock.tick(60)
  77. pygame.quit()