Forráskód Böngészése

cleanup: Video Player

micha 2 éve
szülő
commit
1092d44532
1 módosított fájl, 54 hozzáadás és 31 törlés
  1. 54 31
      media/video_player.py

+ 54 - 31
media/video_player.py

@@ -1,36 +1,60 @@
+#!/usr/bin/env python3
 import cv2
 import pygame
 import time
 
 class Vopen():
     def __init__(self):
-        cap = cv2.VideoCapture('/home/user/Downloads/video.mp4')
-        #cap = cv2.VideoCapture('/home/user/Downloads/video.ogv')
-        cap = cv2.VideoCapture('/home/user/Downloads/bbb_sunflower_480x320.mp4')
-        self.cap = cap
+        self.fname = '/home/user/Downloads/video.mp4'
+        self.fname = '/home/user/Downloads/video.ogv'
+        self.fname = '/home/user/Downloads/bbb_sunflower_480x320.mp4'
+        self.x = 0
+        self.y = 0
+        self.im = None
+        self.init()
+
     def init(self):
-        success, img = self.cap.read()
+        print(self,"init()")
+        cap = cv2.VideoCapture(self.fname)
+        self.cap = cap
+        self.success, self.img = self.cap.read()
         try:
-            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
+            self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
         except:pass
-        self.shape = img.shape[1::-1]
-        #print( img.shape[0:10] )
-        return success,self.shape
+        self.shape = self.img.shape[1::-1]
+
     def read(self):
-        success, img = self.cap.read()
+        print(self,"read()")
+        self.success, self.img = self.cap.read()
+        print(self.success)
+        try:
+            self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
+        except Exception as e:
+            print("exception 432",e)
+
+    def next(self):
+        print(self,"play",time.time())
+        self.read()
         try:
-            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
-        except:
-            pass
-        #cv2.CvtColor(img, im, cv.CV_BGR2RGB)
-        #print( img.shape[0:10] )
-        #print( self.shape )
-        return success,img
+            self.im = pygame.image.frombuffer(self.img.tobytes(), self.shape, "RGB")
+            # wn.blit(im, (self.x, self.y))
+        except AttributeError as e:
+            print("except",e)
+            time.sleep(1)
+            self.init()
+            self.success,self.shape = v.init()
+
+    def draw(self,wn):
+        if self.success:
+            wn.blit(self.im, (self.x, self.y))
 
 v = Vopen()
-success,shape = v.init()
+#success,shape = v.init()
+shape = [300,300]
+if v.shape:
+    shape  = v.shape
 
-wn = pygame.display.set_mode(shape,pygame.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
+wn = pygame.display.set_mode(v.shape,pygame.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
 #wn = pygame.display.set_mode(shape)
 window = wn
 clock = pygame.time.Clock()
@@ -46,25 +70,24 @@ def grab(x=55,y=55,w=60,h=60):
     crop.blit(sub, (0,0))
     return crop
 
-while success:
-    clock.tick(60)
-    success, img = v.read()
+max_frame=0
+success=1
+
+while v.success and success:
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
             success = False
-    try:
-        im = pygame.image.frombuffer(img.tobytes(), shape, "RGB")
-        #im = pygame.image.frombuffer(img.tobytes(), shape, "BGR")
-        wn.blit(im, (0, 0))
-    except AttributeError as e:
-        print("except",e)
-        time.sleep(1)
-        v = Vopen()
-        success,shape = v.init()
+    
+    if max_frame < 300:
+        v.next()
+        max_frame+=1
+    #print(i)
+    v.draw(wn) #,x=0,y=0)
 
     sub = grab()
     window.blit(sub, (500,10))
     pygame.display.update()
+    clock.tick(60)
 
  
 pygame.quit()