video_player.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.pos = 0
  14. self.buffer = []
  15. self.init()
  16. def init(self):
  17. print(self,"init()",self.fname)
  18. self.buffer = []
  19. cap = cv2.VideoCapture(self.fname)
  20. self.cap = cap
  21. self.success, self.img = self.cap.read()
  22. try:
  23. self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
  24. #self.buffer.append(self.img)
  25. self.pos = 0
  26. except:pass
  27. self.shape = self.img.shape[1::-1]
  28. def read(self):
  29. #print(self,"read()")
  30. self.success, self.img = self.cap.read()
  31. #print(self.success)
  32. try:
  33. self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
  34. except Exception as e:
  35. print("exception 432",e)
  36. def prev(self):
  37. self.pos -= 1
  38. if self.pos < 0:
  39. self.pos = len(self.buffer)-1
  40. self.im = self.buffer[self.pos]
  41. def next(self):
  42. #print(self,"play",time.time())
  43. #print(dir(self.cap))
  44. #print(self.cap.set.__doc__)
  45. #print(self.cap.grab.__doc__)
  46. self.read()
  47. try:
  48. self.im = pygame.image.frombuffer(self.img.tobytes(), self.shape, "RGB")
  49. self.buffer.append(self.im)
  50. self.pos += 1
  51. # wn.blit(im, (self.x, self.y))
  52. except AttributeError as e:
  53. print("except",e)
  54. time.sleep(1)
  55. self.init()
  56. def draw(self,wn=None):
  57. if self.success and wn: # is not None:
  58. wn.blit(self.im, (self.x, self.y))
  59. v = Vopen()
  60. shape = [300,300]
  61. if v.shape:
  62. shape = v.shape
  63. wn = pygame.display.set_mode(v.shape,pygame.RESIZABLE)
  64. window = wn
  65. clock = pygame.time.Clock()
  66. pygame.display.set_caption('LibreLight VIDEO PLAYER (BOUNCE-LOOP)')
  67. def grab(x=55,y=55,w=60,h=60):
  68. # usage
  69. # sub = grab()
  70. # window.blit(sub, (500,10))
  71. rect = pygame.Rect(x, y, w, h)
  72. sub = window.subsurface(rect)
  73. #pixArray = pygame.PixelArray(screen)
  74. crop = pygame.Surface((w,h))
  75. crop.blit(sub, (0,0))
  76. return crop
  77. max_frame=0
  78. success=1
  79. loop = 1
  80. run = 1
  81. while v.success and success:
  82. for event in pygame.event.get():
  83. if event.type == pygame.QUIT:
  84. success = False
  85. _button = None
  86. if "button" in event.dict:
  87. _button = event.dict["button"]
  88. if event.type:
  89. print(_button, event.type,run)
  90. if _button == 1:
  91. if event.type == 5:
  92. print("----")
  93. if run:
  94. run = 0
  95. else:
  96. run = 1
  97. if run:
  98. if loop:
  99. if max_frame < 100:
  100. v.next()
  101. max_frame+=1
  102. else:
  103. #max_frame = 0
  104. #v.init()
  105. v.prev()
  106. if v.pos <= 0:
  107. max_frame = 0
  108. v.pos = len(v.buffer)-1
  109. else:
  110. v.next()
  111. #print(i)
  112. if wn:
  113. v.draw(wn) #,x=0,y=0)
  114. sub = grab()
  115. wn.blit(sub, (500,10))
  116. pygame.display.update()
  117. clock.tick(420)
  118. #clock.tick(60)
  119. pygame.quit()