Parcourir la source

add: loop, and mouse click

micha il y a 2 ans
Parent
commit
ae770adad8
1 fichiers modifiés avec 61 ajouts et 11 suppressions
  1. 61 11
      media/video_player.py

+ 61 - 11
media/video_player.py

@@ -11,15 +11,21 @@ class Vopen():
         self.x = 0
         self.y = 0
         self.im = None
+        self.pos = 0
+        self.buffer = []
         self.init()
 
     def init(self):
         print(self,"init()",self.fname)
+        self.buffer = []
         cap = cv2.VideoCapture(self.fname)
+
         self.cap = cap
         self.success, self.img = self.cap.read()
         try:
             self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
+            #self.buffer.append(self.img)
+            self.pos = 0
         except:pass
         self.shape = self.img.shape[1::-1]
 
@@ -31,20 +37,31 @@ class Vopen():
             self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
         except Exception as e:
             print("exception 432",e)
+    def prev(self):
+        self.pos -= 1
+        if self.pos < 0:
+            self.pos = len(self.buffer)-1
+        self.im = self.buffer[self.pos]
 
     def next(self):
         #print(self,"play",time.time())
+        #print(dir(self.cap))
+        #print(self.cap.set.__doc__)
+        #print(self.cap.grab.__doc__)
+         
         self.read()
         try:
             self.im = pygame.image.frombuffer(self.img.tobytes(), self.shape, "RGB")
+            self.buffer.append(self.im)
+            self.pos += 1
             # wn.blit(im, (self.x, self.y))
         except AttributeError as e:
             print("except",e)
             time.sleep(1)
             self.init()
 
-    def draw(self,wn):
-        if self.success:
+    def draw(self,wn=None):
+        if self.success and wn: # is not None:
             wn.blit(self.im, (self.x, self.y))
 
 v = Vopen()
@@ -56,7 +73,7 @@ if v.shape:
 wn = pygame.display.set_mode(v.shape,pygame.RESIZABLE)
 window = wn
 clock = pygame.time.Clock()
-pygame.display.set_caption('LibreLight VIDEO PLAYER')
+pygame.display.set_caption('LibreLight VIDEO PLAYER (BOUNCE-LOOP)')
 
 def grab(x=55,y=55,w=60,h=60):
     # usage
@@ -72,21 +89,54 @@ def grab(x=55,y=55,w=60,h=60):
 max_frame=0
 success=1
 
+loop = 1
+run = 1
 while v.success and success:
+    
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
             success = False
+
+        _button = None
+        if "button" in event.dict:
+             _button =  event.dict["button"]
+
+        if event.type:
+            print(_button, event.type,run)
+
+        if _button == 1:
+           if event.type == 5:
+               print("----")
+               if run:
+                   run = 0
+               else:
+                   run = 1
+
     
-    if 1: #max_frame < 300:
-        v.next()
-        max_frame+=1
+    if run:
+        if loop:
+            if max_frame < 100:
+                v.next()
+                max_frame+=1
+            else:
+                #max_frame = 0
+                #v.init()
+                v.prev()
+                if v.pos <= 0:
+                    max_frame = 0
+                    v.pos = len(v.buffer)-1
+
+        else:
+            v.next()
     #print(i)
-    v.draw(wn) #,x=0,y=0)
+    if wn:
+        v.draw(wn) #,x=0,y=0)
 
-    sub = grab()
-    wn.blit(sub, (500,10))
-    pygame.display.update()
-    clock.tick(60)
+        sub = grab()
+        wn.blit(sub, (500,10))
+        pygame.display.update()
+    clock.tick(420)
+    #clock.tick(60)
 
  
 pygame.quit()