video_player.py 3.6 KB

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