-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoPool.py
53 lines (43 loc) · 1.54 KB
/
VideoPool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import glob
import random
import threading
class VideoPool:
def __init__(self, path, refreshInterval=0):
self.list = []
self.popList = []
self.path = path
self.refreshInterval = refreshInterval
self.update()
# 0 means realtime
if ( self.refreshInterval ):
self.setInterval( self.update, self.refreshInterval )
def setInterval(self, func, sec):
def func_wrapper():
self.setInterval( func, sec )
func()
t = threading.Timer(sec, func_wrapper)
t.start()
return t
def update(self):
listcount = len( self.list )
self.list = glob.glob(self.path)
if ( not len( self.popList ) ):
self.popList = self.list[:]
# Only show delta
if ( len( self.list ) > listcount ):
print( "VideoPool found {} new files".format( len( self.list ) - listcount ) )
print( "" )
elif (len( self.list ) < listcount):
print( "VideoPool lost {} files".format( listcount - len( self.list )) )
print( "" )
# TODO: remove LOST files on popList as well (to prevent video stall)
self.popList = self.list[:]
def randomVideo(self):
if ( not self.refreshInterval ):
self.update()
if ( not len(self.list) ):
return False
if ( not len(self.popList) ):
self.popList = self.list[:]
randomChoice = self.popList.pop(random.randrange(len(self.popList)))
return randomChoice