Skip to content

Commit

Permalink
Use float32 for levels
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Aug 5, 2024
1 parent 600e3b5 commit 0841e5c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
22 changes: 11 additions & 11 deletions auto_editor/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def obj_tag(tag: str, tb: Fraction, obj: dict[str, Any]) -> str:
return key


def iter_audio(src, tb: Fraction, stream: int = 0) -> Iterator[float]:
def iter_audio(src, tb: Fraction, stream: int = 0) -> Iterator[np.float32]:
fifo = AudioFifo()
try:
container = av.open(src.path, "r")
Expand Down Expand Up @@ -117,13 +117,13 @@ def iter_audio(src, tb: Fraction, stream: int = 0) -> Iterator[float]:
audio_chunk = fifo.read(current_size)
assert audio_chunk is not None
arr = audio_chunk.to_ndarray().flatten()
yield float(np.max(np.abs(arr)))
yield np.max(np.abs(arr))

finally:
container.close()


def iter_motion(src, tb, stream: int, blur: int, width: int) -> Iterator[float]:
def iter_motion(src, tb, stream: int, blur: int, width: int) -> Iterator[np.float32]:
container = av.open(src.path, "r")

video = container.streams.video[stream]
Expand Down Expand Up @@ -155,11 +155,11 @@ def iter_motion(src, tb, stream: int, blur: int, width: int) -> Iterator[float]:

current_frame = frame.to_ndarray()
if prev_frame is None:
value = 0.0
value = np.float32(0.0)
else:
# Use `int16` to avoid underflow with `uint8` datatype
diff = np.abs(prev_frame.astype(np.int16) - current_frame.astype(np.int16))
value = np.count_nonzero(diff) / total_pixels
value = np.float32(np.count_nonzero(diff) / total_pixels)

for _ in range(index - prev_index):
yield value
Expand Down Expand Up @@ -237,7 +237,7 @@ def cache(self, tag: str, obj: dict[str, Any], arr: np.ndarray) -> np.ndarray:

return arr

def audio(self, stream: int) -> NDArray[np.float64]:
def audio(self, stream: int) -> NDArray[np.float32]:
if stream >= len(self.src.audios):
raise LevelError(f"audio: audio stream '{stream}' does not exist.")

Expand All @@ -256,12 +256,12 @@ def audio(self, stream: int) -> NDArray[np.float64]:
bar = self.bar
bar.start(inaccurate_dur, "Analyzing audio volume")

result = np.zeros((inaccurate_dur), dtype=np.float64)
result = np.zeros((inaccurate_dur), dtype=np.float32)
index = 0
for value in iter_audio(self.src, self.tb, stream):
if index > len(result) - 1:
result = np.concatenate(
(result, np.zeros((len(result)), dtype=np.float64))
(result, np.zeros((len(result)), dtype=np.float32))
)
result[index] = value
bar.tick(index)
Expand All @@ -270,7 +270,7 @@ def audio(self, stream: int) -> NDArray[np.float64]:
bar.end()
return self.cache("audio", {"stream": stream}, result[:index])

def motion(self, stream: int, blur: int, width: int) -> NDArray[np.float64]:
def motion(self, stream: int, blur: int, width: int) -> NDArray[np.float32]:
if stream >= len(self.src.videos):
raise LevelError(f"motion: video stream '{stream}' does not exist.")

Expand All @@ -289,12 +289,12 @@ def motion(self, stream: int, blur: int, width: int) -> NDArray[np.float64]:
bar = self.bar
bar.start(inaccurate_dur, "Analyzing motion")

result = np.zeros((inaccurate_dur), dtype=np.float64)
result = np.zeros((inaccurate_dur), dtype=np.float32)
index = 0
for value in iter_motion(self.src, self.tb, stream, blur, width):
if index > len(result) - 1:
result = np.concatenate(
(result, np.zeros((len(result)), dtype=np.float64))
(result, np.zeros((len(result)), dtype=np.float32))
)
result[index] = value
bar.tick(index)
Expand Down
7 changes: 2 additions & 5 deletions auto_editor/subcommands/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,11 @@ def print_arr(arr: NDArray) -> None:
print("")


def print_arr_gen(arr: Iterator[int | float]) -> None:
def print_arr_gen(arr: Iterator[float | np.float32]) -> None:
print("")
print("@start")
for a in arr:
if isinstance(a, float):
print(f"{a:.20f}")
else:
print(a)
print(f"{a:.20f}")
print("")


Expand Down

0 comments on commit 0841e5c

Please sign in to comment.