Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cache for level command #626

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions auto_editor/subcommands/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def levels_options(parser: ArgumentParser) -> ArgumentParser:
def print_arr(arr: NDArray) -> None:
print("")
print("@start")
if arr.dtype == np.float64:
if arr.dtype in (np.float64, np.float32, np.float16):
for a in arr:
sys.stdout.write(f"{a:.20f}\n")
elif arr.dtype == np.bool_:
Expand Down Expand Up @@ -131,18 +131,49 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
levels = Levels(src, tb, bar, False, log, strict=True)
try:
if method == "audio":
container = av.open(src.path, "r")
audio_stream = container.streams.audio[obj["stream"]]
log.experimental(audio_stream.codec)
print_arr_gen(iter_audio(audio_stream, tb))
container.close()
if (arr := levels.read_cache("audio", (obj["stream"],))) is not None:
print_arr(arr)
else:
container = av.open(src.path, "r")
audio_stream = container.streams.audio[obj["stream"]]
log.experimental(audio_stream.codec)

values = []

def value_storing_generator() -> Iterator[np.float32]:
for value in iter_audio(audio_stream, tb):
values.append(value)
yield value

print_arr_gen(value_storing_generator())
container.close()

cache_array = np.array(values, dtype=np.float32)
levels.cache(cache_array, "audio", (obj["stream"],))

elif method == "motion":
container = av.open(src.path, "r")
video_stream = container.streams.video[obj["stream"]]
log.experimental(video_stream.codec)
print_arr_gen(iter_motion(video_stream, tb, obj["blur"], obj["width"]))
container.close()
mobj = (obj["stream"], obj["width"], obj["blur"])
if (arr := levels.read_cache("motion", mobj)) is not None:
print_arr(arr)
else:
container = av.open(src.path, "r")
video_stream = container.streams.video[obj["stream"]]
log.experimental(video_stream.codec)

values = []

def value_storing_generator() -> Iterator[np.float32]:
for value in iter_motion(
video_stream, tb, obj["blur"], obj["width"]
):
values.append(value)
yield value

print_arr_gen(value_storing_generator())
container.close()

cache_array = np.array(values, dtype=np.float32)
levels.cache(cache_array, "motion", mobj)

elif method == "subtitle":
print_arr(levels.subtitle(**obj))
Expand Down
Loading