21
21
22
22
if TYPE_CHECKING :
23
23
from collections .abc import Callable
24
- from typing import Any , NoReturn
24
+ from typing import Any , NoReturn , TypeGuard
25
25
26
26
from numpy .typing import NDArray
27
27
@@ -510,15 +510,16 @@ def p_slice(
510
510
return seq [start :end :step ]
511
511
512
512
513
+ def is_boolarr_f (v : object ) -> TypeGuard [np .ndarray ]:
514
+ return isinstance (v , np .ndarray ) and v .dtype .kind == "b"
515
+
516
+
513
517
is_iterable = Contract (
514
518
"iterable?" ,
515
519
lambda v : type (v ) in {str , range , list , tuple , dict , Quoted }
516
520
or isinstance (v , np .ndarray ),
517
521
)
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 )
522
523
523
524
524
525
def raise_ (msg : str | Exception ) -> NoReturn :
@@ -569,8 +570,6 @@ def edit_audio(
569
570
raise MyError ("Can't use `audio` if there's no input media" )
570
571
571
572
levels = cast (Levels , env ["@levels" ])
572
- strict = levels .strict
573
-
574
573
stream_data : NDArray [np .bool_ ] | None = None
575
574
if stream == Sym ("all" ):
576
575
stream_range = range (0 , len (levels .container .streams .audio ))
@@ -585,17 +584,15 @@ def edit_audio(
585
584
stream_data = audio_list
586
585
else :
587
586
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_ )
590
589
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_ )
594
592
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
599
596
600
597
601
598
def edit_motion (
@@ -607,18 +604,18 @@ def edit_motion(
607
604
if "@levels" not in env :
608
605
raise MyError ("Can't use `motion` if there's no input media" )
609
606
610
- levels = env ["@levels" ]
607
+ levels = cast ( Levels , env ["@levels" ])
611
608
try :
612
609
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_ )
615
612
616
613
617
614
def edit_subtitle (pattern , stream = 0 , ** kwargs ):
618
615
if "@levels" not in env :
619
616
raise MyError ("Can't use `subtitle` if there's no input media" )
620
617
621
- levels = env ["@levels" ]
618
+ levels = cast ( Levels , env ["@levels" ])
622
619
if "ignore-case" not in kwargs :
623
620
kwargs ["ignore-case" ] = False
624
621
if "max-count" not in kwargs :
@@ -627,8 +624,8 @@ def edit_subtitle(pattern, stream=0, **kwargs):
627
624
max_count = kwargs ["max-count" ]
628
625
try :
629
626
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_ )
632
629
633
630
634
631
class StackTraceManager :
0 commit comments