video_player.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env python3
  2. import cv2
  3. import pygame
  4. import pygame.font
  5. import time
  6. import os
  7. HOME = os.getenv('HOME')
  8. pygame.init()
  9. class Vopen():
  10. def __init__(self):
  11. self.fname = HOME+'/Downloads/video.mp4'
  12. self.fname = HOME+'/Downloads/video.ogv'
  13. self.fname = HOME+'/Downloads/bbb_sunflower_480x320.mp4'
  14. self.scale = 80 #%
  15. self.x = 0
  16. self.y = 0
  17. self.cap = None
  18. self.shape = [200,200]
  19. self.success = 1
  20. self.cv2 = None
  21. try:
  22. self.cv2 = cv2
  23. except:
  24. pass
  25. self.im = None
  26. self.pos = 0
  27. self.buffer = []
  28. self.init()
  29. def init(self):
  30. print(self,"init()",self.fname)
  31. if not os.path.isfile(self.fname):
  32. print()
  33. print("video file does not exits !! >",self.fname)
  34. print()
  35. exit()
  36. self.buffer = []
  37. if self.cv2:
  38. cap = self.cv2.VideoCapture(self.fname)
  39. self.cap = cap
  40. self.success, self.img = self.cap.read()
  41. try:
  42. self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  43. #self.shape = self.img.shape[:2]
  44. #self.img = self.rescale_frame(self.img, percent=10)
  45. #self.buffer.append(self.img)
  46. self.pos = 0
  47. except:pass
  48. self.shape = self.img.shape[1::-1]
  49. for i in range(900):
  50. self.read()
  51. def read(self):
  52. #print(self,"read()")
  53. #print(self.success)
  54. try:
  55. self.success, self.img = self.cap.read()
  56. self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  57. self.img = self.rescale_frame(self.img, percent=self.scale)
  58. #self.shape = self.img.shape[:2]
  59. #self.img = self.rescale_frame(self.img, percent=0)
  60. self.shape = self.img.shape[1::-1]
  61. except Exception as e:
  62. print("exception 432",e)
  63. def prev(self):
  64. self.pos -= 1
  65. if self.pos < 0:
  66. self.pos = len(self.buffer)-1
  67. if self.pos >= len(self.buffer):
  68. self.pos = len(self.buffer)-1
  69. self.im = self.buffer[self.pos]
  70. def rescale_frame(self,frame, percent=75):
  71. width = int(frame.shape[1] * percent/ 100)
  72. height = int(frame.shape[0] * percent/ 100)
  73. dim = (width, height)
  74. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  75. def next(self):
  76. #print(self,"play",time.time())
  77. #print(dir(self.cap))
  78. #print(self.cap.set.__doc__)
  79. #print(self.cap.grab.__doc__)
  80. self.read()
  81. try:
  82. img = self.img #self.rescale_frame(self.img, percent=30)
  83. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  84. self.buffer.append(self.im)
  85. self.pos += 1
  86. # wn.blit(im, (self.x, self.y))
  87. except AttributeError as e:
  88. print("except",e)
  89. time.sleep(1)
  90. self.init()
  91. def draw(self,wn=None):
  92. if self.success and wn and self.im: # is not None:
  93. wn.blit(self.im, (self.x, self.y))
  94. def overlay(self,wn=None,mode="x"):
  95. # overlay
  96. shape = self.img.shape
  97. shape = [0,0,0]
  98. pygame.draw.rect(wn,[255,200,0],[5+self.x-3,4+self.y-1+shape[0],140,20])
  99. font15 = pygame.font.SysFont("freemonobold",17)
  100. fr = font15.render(">:{}".format(mode) ,1, (0,0,0))
  101. wn.blit(fr,(3+self.x,4+self.y+shape[0]))
  102. fr = font15.render("FRAME:{}".format(self.pos) ,1, (0,0,0))
  103. wn.blit(fr,(45+self.x,4+self.y+shape[0]))
  104. v = Vopen()
  105. shape = [300,300]
  106. if v.shape:
  107. shape = v.shape
  108. wn = pygame.display.set_mode(v.shape,pygame.RESIZABLE)
  109. window = wn
  110. clock = pygame.time.Clock()
  111. pygame.display.set_caption('LibreLight VIDEO PLAYER (BOUNCE-LOOP)')
  112. window.fill((30,30,20))
  113. pygame.display.update()
  114. def grab(x=55,y=55,w=60,h=60):
  115. # usage
  116. # sub = grab()
  117. # window.blit(sub, (500,10))
  118. rect = pygame.Rect(x, y, w, h)
  119. sub = window.subsurface(rect)
  120. #pixArray = pygame.PixelArray(screen)
  121. crop = pygame.Surface((w,h))
  122. crop.blit(sub, (0,0))
  123. return crop
  124. class VideoDemo():
  125. def __init__(self):
  126. pass
  127. max_frame=0
  128. success=1
  129. loop = 1
  130. run = 1
  131. while v.success and success:
  132. window.fill((30,30,20))
  133. for event in pygame.event.get():
  134. if event.type == pygame.QUIT:
  135. success = False
  136. _button = None
  137. if "button" in event.dict:
  138. _button = event.dict["button"]
  139. if event.type:
  140. print(_button, event.type,run)
  141. if _button == 1:
  142. if event.type == 5:
  143. print("----")
  144. if run:
  145. run = 0
  146. else:
  147. run = 1
  148. # error message
  149. pygame.draw.rect(wn,[255,0,0],[18,48,100,20])
  150. font15 = pygame.font.SysFont("freemonobold",20)
  151. fr = font15.render("NO VIDEO" ,1, (0,0,0))
  152. wn.blit(fr,(20,50))
  153. d = "PAUSE"
  154. #v.pos
  155. #v.next()
  156. #v.prev()
  157. if run:
  158. if loop:
  159. d = "PLAY"
  160. if max_frame < 100:
  161. v.next()
  162. max_frame+=1
  163. else:
  164. d = "REVERSE"
  165. #max_frame = 0
  166. #v.init()
  167. v.prev()
  168. if v.pos <= 0:
  169. max_frame = 0
  170. v.pos = len(v.buffer)-1
  171. else:
  172. v.next()
  173. #print(i)
  174. if wn:
  175. v.draw(wn) #,x=0,y=0)
  176. # overlay
  177. v.overlay(wn,d)
  178. sub = grab()
  179. wn.blit(sub, (500,10))
  180. pygame.display.update()
  181. clock.tick(420)
  182. #clock.tick(60)
  183. pygame.quit()