Av1an/av1an/manager/Counter.py

54 lines
1.2 KiB
Python
Raw Normal View History

2020-12-27 04:56:43 +00:00
from multiprocessing.managers import BaseManager
2021-04-12 23:23:48 +00:00
try:
from tqdm import tqdm
except ImportError:
tqdm = None
2020-12-27 04:56:43 +00:00
def Manager():
"""
Thread save manager for frame counter
"""
m = BaseManager()
m.start()
return m
class Counter:
"""
2020-12-27 05:49:31 +00:00
Frame Counter
2020-12-27 04:56:43 +00:00
"""
2021-04-12 23:23:48 +00:00
2020-12-27 05:49:31 +00:00
def __init__(self, total, initial, use_tqdm=True):
2020-12-27 04:56:43 +00:00
self.first_update = True
self.initial = initial
self.left = total - initial
2020-12-27 05:49:31 +00:00
self.current = 0
2021-04-12 23:23:48 +00:00
self.use_tqdm = use_tqdm and (tqdm is not None)
2020-12-27 05:49:31 +00:00
if use_tqdm:
2021-04-12 23:23:48 +00:00
self.tqdm_bar = tqdm(
total=self.left,
initial=0,
dynamic_ncols=True,
unit="fr",
leave=True,
smoothing=0.01,
)
2020-12-27 04:56:43 +00:00
def update(self, value):
2020-12-27 05:49:31 +00:00
if self.use_tqdm:
if self.first_update:
self.tqdm_bar.reset(self.left)
self.first_update = False
self.tqdm_bar.update(value)
else:
self.current += value
2020-12-27 04:56:43 +00:00
def close(self):
if self.use_tqdm:
self.tqdm_bar.close()
2020-12-27 04:56:43 +00:00
2020-12-27 05:49:31 +00:00
def get_frames(self):
return self.current