Skip to content

Commit a333eb0

Browse files
committed
Use cache for level command
1 parent 0a17a4d commit a333eb0

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

auto_editor/subcommands/levels.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def levels_options(parser: ArgumentParser) -> ArgumentParser:
5959
def print_arr(arr: NDArray) -> None:
6060
print("")
6161
print("@start")
62-
if arr.dtype == np.float64:
62+
if arr.dtype in (np.float64, np.float32, np.float16):
6363
for a in arr:
6464
sys.stdout.write(f"{a:.20f}\n")
6565
elif arr.dtype == np.bool_:
@@ -131,18 +131,49 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
131131
levels = Levels(src, tb, bar, False, log, strict=True)
132132
try:
133133
if method == "audio":
134-
container = av.open(src.path, "r")
135-
audio_stream = container.streams.audio[obj["stream"]]
136-
log.experimental(audio_stream.codec)
137-
print_arr_gen(iter_audio(audio_stream, tb))
138-
container.close()
134+
if (arr := levels.read_cache("audio", (obj["stream"],))) is not None:
135+
print_arr(arr)
136+
else:
137+
container = av.open(src.path, "r")
138+
audio_stream = container.streams.audio[obj["stream"]]
139+
log.experimental(audio_stream.codec)
140+
141+
values = []
142+
143+
def value_storing_generator() -> Iterator[np.float32]:
144+
for value in iter_audio(audio_stream, tb):
145+
values.append(value)
146+
yield value
147+
148+
print_arr_gen(value_storing_generator())
149+
container.close()
150+
151+
cache_array = np.array(values, dtype=np.float32)
152+
levels.cache(cache_array, "audio", (obj["stream"],))
139153

140154
elif method == "motion":
141-
container = av.open(src.path, "r")
142-
video_stream = container.streams.video[obj["stream"]]
143-
log.experimental(video_stream.codec)
144-
print_arr_gen(iter_motion(video_stream, tb, obj["blur"], obj["width"]))
145-
container.close()
155+
mobj = (obj["stream"], obj["width"], obj["blur"])
156+
if (arr := levels.read_cache("motion", mobj)) is not None:
157+
print_arr(arr)
158+
else:
159+
container = av.open(src.path, "r")
160+
video_stream = container.streams.video[obj["stream"]]
161+
log.experimental(video_stream.codec)
162+
163+
values = []
164+
165+
def value_storing_generator() -> Iterator[np.float32]:
166+
for value in iter_motion(
167+
video_stream, tb, obj["blur"], obj["width"]
168+
):
169+
values.append(value)
170+
yield value
171+
172+
print_arr_gen(value_storing_generator())
173+
container.close()
174+
175+
cache_array = np.array(values, dtype=np.float32)
176+
levels.cache(cache_array, "motion", mobj)
146177

147178
elif method == "subtitle":
148179
print_arr(levels.subtitle(**obj))

0 commit comments

Comments
 (0)