video_player.py 5.6 KB

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