Skip to content

Commit 5c27af6

Browse files
committed
Remove strict attribute
1 parent 2f8ec54 commit 5c27af6

File tree

5 files changed

+51
-52
lines changed

5 files changed

+51
-52
lines changed

auto_editor/analyze.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ class Levels:
161161
bar: Bar
162162
no_cache: bool
163163
log: Log
164-
strict: bool # This is for `edit_audio()` for Palet functions.
165164

166165
@property
167166
def media_length(self) -> int:
@@ -391,17 +390,12 @@ def subtitle(
391390

392391

393392
def initLevels(
394-
src: FileInfo,
395-
tb: Fraction,
396-
bar: Bar,
397-
no_cache: bool,
398-
log: Log,
399-
strict: bool = False,
393+
src: FileInfo, tb: Fraction, bar: Bar, no_cache: bool, log: Log
400394
) -> Levels:
401395
try:
402396
container = av.open(src.path)
403397
except av.FFmpegError as e:
404398
log.error(e)
405399

406400
mod_time = int(src.path.stat().st_mtime)
407-
return Levels(container, src.path.name, mod_time, tb, bar, no_cache, log, strict)
401+
return Levels(container, src.path.name, mod_time, tb, bar, no_cache, log)

auto_editor/cmds/repl.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
6161

6262
if args.input:
6363
log = Log(quiet=True, temp_dir=args.temp_dir)
64-
strict = len(args.input) < 2
6564
sources = [initFileInfo(path, log) for path in args.input]
6665
src = sources[0]
6766
tb = src.get_fps() if args.timebase is None else args.timebase
6867
env["timebase"] = tb
69-
env["@levels"] = initLevels(src, tb, initBar("modern"), False, log, strict)
68+
env["@levels"] = initLevels(src, tb, initBar("modern"), False, log)
7069

7170
env.update(make_standard_env())
7271
print(f"Auto-Editor {auto_editor.__version__}")

auto_editor/cmds/test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,11 @@ def edit_positive_tests():
545545
def edit_negative_tests():
546546
run.check(
547547
["resources/wav/example-cut-s16le.wav", "--edit", "motion"],
548-
"video stream '0' does not ",
548+
"video stream",
549549
)
550550
run.check(
551551
["resources/only-video/man-on-green-screen.gif", "--edit", "audio"],
552-
"audio stream '0' does not ",
552+
"audio stream",
553553
)
554554

555555
def yuv442p():

auto_editor/lang/palet.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
if TYPE_CHECKING:
2323
from collections.abc import Callable
24-
from typing import Any, NoReturn
24+
from typing import Any, NoReturn, TypeGuard
2525

2626
from numpy.typing import NDArray
2727

@@ -510,15 +510,16 @@ def p_slice(
510510
return seq[start:end:step]
511511

512512

513+
def is_boolarr_f(v: object) -> TypeGuard[np.ndarray]:
514+
return isinstance(v, np.ndarray) and v.dtype.kind == "b"
515+
516+
513517
is_iterable = Contract(
514518
"iterable?",
515519
lambda v: type(v) in {str, range, list, tuple, dict, Quoted}
516520
or isinstance(v, np.ndarray),
517521
)
518-
is_boolarr = Contract(
519-
"bool-array?",
520-
lambda v: isinstance(v, np.ndarray) and v.dtype.kind == "b",
521-
)
522+
is_boolarr = Contract("bool-array?", is_boolarr_f)
522523

523524

524525
def raise_(msg: str | Exception) -> NoReturn:
@@ -569,8 +570,6 @@ def edit_audio(
569570
raise MyError("Can't use `audio` if there's no input media")
570571

571572
levels = cast(Levels, env["@levels"])
572-
strict = levels.strict
573-
574573
stream_data: NDArray[np.bool_] | None = None
575574
if stream == Sym("all"):
576575
stream_range = range(0, len(levels.container.streams.audio))
@@ -585,17 +584,15 @@ def edit_audio(
585584
stream_data = audio_list
586585
else:
587586
stream_data = boolop(stream_data, audio_list, np.logical_or)
588-
except LevelError as e:
589-
raise_(e) if strict else levels.all()
587+
except LevelError:
588+
return np.array([], dtype=np.bool_)
590589

591-
if stream_data is not None:
592-
mut_remove_small(stream_data, minclip, replace=1, with_=0)
593-
mut_remove_small(stream_data, mincut, replace=0, with_=1)
590+
if stream_data is None:
591+
return np.array([], dtype=np.bool_)
594592

595-
return stream_data
596-
597-
stream = 0 if stream == Sym("all") else stream
598-
return raise_(f"audio stream '{stream}' does not exist") if strict else levels.all()
593+
mut_remove_small(stream_data, minclip, replace=1, with_=0)
594+
mut_remove_small(stream_data, mincut, replace=0, with_=1)
595+
return stream_data
599596

600597

601598
def edit_motion(
@@ -607,18 +604,18 @@ def edit_motion(
607604
if "@levels" not in env:
608605
raise MyError("Can't use `motion` if there's no input media")
609606

610-
levels = env["@levels"]
607+
levels = cast(Levels, env["@levels"])
611608
try:
612609
return levels.motion(stream, blur, width) >= threshold
613-
except LevelError as e:
614-
return raise_(e) if levels.strict else levels.all()
610+
except LevelError:
611+
return np.array([], dtype=np.bool_)
615612

616613

617614
def edit_subtitle(pattern, stream=0, **kwargs):
618615
if "@levels" not in env:
619616
raise MyError("Can't use `subtitle` if there's no input media")
620617

621-
levels = env["@levels"]
618+
levels = cast(Levels, env["@levels"])
622619
if "ignore-case" not in kwargs:
623620
kwargs["ignore-case"] = False
624621
if "max-count" not in kwargs:
@@ -627,8 +624,8 @@ def edit_subtitle(pattern, stream=0, **kwargs):
627624
max_count = kwargs["max-count"]
628625
try:
629626
return levels.subtitle(pattern, stream, ignore_case, max_count)
630-
except LevelError as e:
631-
return raise_(e) if levels.strict else levels.all()
627+
except LevelError:
628+
return np.array([], dtype=np.bool_)
632629

633630

634631
class StackTraceManager:

auto_editor/make_layers.py

+27-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from auto_editor.analyze import initLevels
1010
from auto_editor.ffwrapper import FileInfo
11-
from auto_editor.lang.palet import Lexer, Parser, env, interpret, is_boolarr
11+
from auto_editor.lang.palet import Lexer, Parser, env, interpret, is_boolarr_f
1212
from auto_editor.lib.data_structs import print_str
1313
from auto_editor.lib.err import MyError
1414
from auto_editor.timeline import ASpace, TlAudio, TlVideo, VSpace, v1, v3
@@ -122,7 +122,6 @@ def make_timeline(
122122

123123
has_loud = np.array([], dtype=np.bool_)
124124
src_index = np.array([], dtype=np.int32)
125-
concat = np.concatenate
126125

127126
try:
128127
stdenv = __import__("auto_editor.lang.stdenv", fromlist=["lang"])
@@ -137,41 +136,51 @@ def make_timeline(
137136
parser = Parser(Lexer("config.pal", file.read()))
138137
interpret(env, parser)
139138

139+
results = []
140+
src_indexes = []
140141
for i, src in enumerate(sources):
141142
try:
142143
parser = Parser(Lexer("`--edit`", args.edit))
143144
if log.is_debug:
144145
log.debug(f"edit: {parser}")
145146

146147
env["timebase"] = tb
147-
env["src"] = f"{src.path}"
148-
env["@levels"] = initLevels(
149-
src, tb, bar, args.no_cache, log, len(sources) < 2
150-
)
151-
152-
results = interpret(env, parser)
148+
env["@levels"] = initLevels(src, tb, bar, args.no_cache, log)
153149

154-
if len(results) == 0:
150+
inter_result = interpret(env, parser)
151+
if len(inter_result) == 0:
155152
log.error("Expression in --edit must return a bool-array, got nothing")
156153

157-
result = results[-1]
154+
result = inter_result[-1]
158155
if callable(result):
159156
result = result()
160157
except MyError as e:
161158
log.error(e)
162159

163-
if not is_boolarr(result):
160+
if not is_boolarr_f(result):
164161
log.error(
165162
f"Expression in --edit must return a bool-array, got {print_str(result)}"
166163
)
167-
assert isinstance(result, np.ndarray)
168-
169164
mut_margin(result, start_margin, end_margin)
170-
171-
has_loud = concat((has_loud, result))
172-
src_index = concat((src_index, np.full(len(result), i, dtype=np.int32)))
173-
174-
assert len(has_loud) > 0
165+
results.append(result)
166+
src_indexes.append(np.full(len(result), i, dtype=np.int32))
167+
168+
if all(len(result) == 0 for result in results):
169+
if "subtitle" in args.edit:
170+
log.error("No file(s) have the selected subtitle stream.")
171+
if "motion" in args.edit:
172+
log.error("No file(s) have the selected video stream.")
173+
if "audio" in args.edit:
174+
log.error("No file(s) have the selected audio stream.")
175+
176+
for i in range(0, len(results)):
177+
if len(results[i]) == 0:
178+
results[i] = initLevels(sources[i], tb, bar, args.no_cache, log).none()
179+
180+
has_loud = np.concatenate(results)
181+
src_index = np.concatenate(src_indexes)
182+
if len(has_loud) == 0:
183+
log.error("Empty timeline. Nothing to do.")
175184

176185
# Setup for handling custom speeds
177186
speed_index = has_loud.astype(np.uint)

0 commit comments

Comments
 (0)