video_player.py 4.7 KB

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