@@ -133,6 +133,22 @@ def ensure_detector(self):
133133 (detector_type , detector_args ) = self .default_detector
134134 self .add_detector (detector_type , detector_args )
135135
136+ def _resolve_min_scene_len (self , command : str , override : str | None ) -> int :
137+ """Resolve the minimum scene length (in frames) for a `detect-*` command, honoring
138+ the `--drop-short-scenes` flag, command-specific config, and global default."""
139+ if self .drop_short_scenes :
140+ return 0
141+ if override is not None :
142+ parsed = self .parse_timecode (override )
143+ assert parsed is not None
144+ return parsed .frame_num
145+ if self .config .is_default (command , "min-scene-len" ):
146+ assert self .min_scene_len is not None
147+ return self .min_scene_len .frame_num
148+ parsed = self .parse_timecode (self .config .get_value (command , "min-scene-len" ))
149+ assert parsed is not None
150+ return parsed .frame_num
151+
136152 def parse_timecode (self , value : str | None , correct_pts : bool = False ) -> FrameTimecode | None :
137153 """Parses a user input string into a FrameTimecode assuming the given framerate. If `value`
138154 is None it will be passed through without processing.
@@ -328,18 +344,7 @@ def get_detect_content_params(
328344 filter_mode : str | None = None ,
329345 ) -> dict [str , ty .Any ]:
330346 """Get a dict containing user options to construct a ContentDetector with."""
331- if self .drop_short_scenes :
332- min_scene_len = 0
333- else :
334- if min_scene_len is None :
335- if self .config .is_default ("detect-content" , "min-scene-len" ):
336- assert self .min_scene_len is not None
337- min_scene_len = self .min_scene_len .frame_num
338- else :
339- min_scene_len = self .config .get_value ("detect-content" , "min-scene-len" )
340- parsed = self .parse_timecode (min_scene_len )
341- assert parsed is not None
342- min_scene_len = parsed .frame_num
347+ min_scene_len_frames = self ._resolve_min_scene_len ("detect-content" , min_scene_len )
343348
344349 if weights is not None :
345350 try :
@@ -354,7 +359,7 @@ def get_detect_content_params(
354359 "weights" : self .config .get_value ("detect-content" , "weights" , weights ),
355360 "kernel_size" : self .config .get_value ("detect-content" , "kernel-size" , kernel_size ),
356361 "luma_only" : luma_only or self .config .get_value ("detect-content" , "luma-only" ),
357- "min_scene_len" : min_scene_len ,
362+ "min_scene_len" : min_scene_len_frames ,
358363 "threshold" : self .config .get_value ("detect-content" , "threshold" , threshold ),
359364 "filter_mode" : self .config .get_value ("detect-content" , "filter-mode" , filter_mode ),
360365 }
@@ -371,18 +376,7 @@ def get_detect_adaptive_params(
371376 ) -> dict [str , ty .Any ]:
372377 """Handle detect-adaptive command options and return args to construct one with."""
373378
374- if self .drop_short_scenes :
375- min_scene_len = 0
376- else :
377- if min_scene_len is None :
378- if self .config .is_default ("detect-adaptive" , "min-scene-len" ):
379- assert self .min_scene_len is not None
380- min_scene_len = self .min_scene_len .frame_num
381- else :
382- min_scene_len = self .config .get_value ("detect-adaptive" , "min-scene-len" )
383- parsed = self .parse_timecode (min_scene_len )
384- assert parsed is not None
385- min_scene_len = parsed .frame_num
379+ min_scene_len_frames = self ._resolve_min_scene_len ("detect-adaptive" , min_scene_len )
386380
387381 if weights is not None :
388382 try :
@@ -400,7 +394,7 @@ def get_detect_adaptive_params(
400394 "min_content_val" : self .config .get_value (
401395 "detect-adaptive" , "min-content-val" , min_content_val
402396 ),
403- "min_scene_len" : min_scene_len ,
397+ "min_scene_len" : min_scene_len_frames ,
404398 "window_width" : self .config .get_value ("detect-adaptive" , "frame-window" , frame_window ),
405399 }
406400
@@ -413,24 +407,13 @@ def get_detect_threshold_params(
413407 ) -> dict [str , ty .Any ]:
414408 """Handle detect-threshold command options and return args to construct one with."""
415409
416- if self .drop_short_scenes :
417- min_scene_len = 0
418- else :
419- if min_scene_len is None :
420- if self .config .is_default ("detect-threshold" , "min-scene-len" ):
421- assert self .min_scene_len is not None
422- min_scene_len = self .min_scene_len .frame_num
423- else :
424- min_scene_len = self .config .get_value ("detect-threshold" , "min-scene-len" )
425- parsed = self .parse_timecode (min_scene_len )
426- assert parsed is not None
427- min_scene_len = parsed .frame_num
410+ min_scene_len_frames = self ._resolve_min_scene_len ("detect-threshold" , min_scene_len )
428411 # TODO(v1.0): add_last_scene cannot be disabled right now.
429412 return {
430413 "add_final_scene" : add_last_scene
431414 or self .config .get_value ("detect-threshold" , "add-last-scene" ),
432415 "fade_bias" : self .config .get_value ("detect-threshold" , "fade-bias" , fade_bias ),
433- "min_scene_len" : min_scene_len ,
416+ "min_scene_len" : min_scene_len_frames ,
434417 "threshold" : self .config .get_value ("detect-threshold" , "threshold" , threshold ),
435418 }
436419
@@ -442,21 +425,10 @@ def get_detect_hist_params(
442425 ) -> dict [str , ty .Any ]:
443426 """Handle detect-hist command options and return args to construct one with."""
444427
445- if self .drop_short_scenes :
446- min_scene_len = 0
447- else :
448- if min_scene_len is None :
449- if self .config .is_default ("detect-hist" , "min-scene-len" ):
450- assert self .min_scene_len is not None
451- min_scene_len = self .min_scene_len .frame_num
452- else :
453- min_scene_len = self .config .get_value ("detect-hist" , "min-scene-len" )
454- parsed = self .parse_timecode (min_scene_len )
455- assert parsed is not None
456- min_scene_len = parsed .frame_num
428+ min_scene_len_frames = self ._resolve_min_scene_len ("detect-hist" , min_scene_len )
457429 return {
458430 "bins" : self .config .get_value ("detect-hist" , "bins" , bins ),
459- "min_scene_len" : min_scene_len ,
431+ "min_scene_len" : min_scene_len_frames ,
460432 "threshold" : self .config .get_value ("detect-hist" , "threshold" , threshold ),
461433 }
462434
@@ -469,21 +441,10 @@ def get_detect_hash_params(
469441 ) -> dict [str , ty .Any ]:
470442 """Handle detect-hash command options and return args to construct one with."""
471443
472- if self .drop_short_scenes :
473- min_scene_len = 0
474- else :
475- if min_scene_len is None :
476- if self .config .is_default ("detect-hash" , "min-scene-len" ):
477- assert self .min_scene_len is not None
478- min_scene_len = self .min_scene_len .frame_num
479- else :
480- min_scene_len = self .config .get_value ("detect-hash" , "min-scene-len" )
481- parsed = self .parse_timecode (min_scene_len )
482- assert parsed is not None
483- min_scene_len = parsed .frame_num
444+ min_scene_len_frames = self ._resolve_min_scene_len ("detect-hash" , min_scene_len )
484445 return {
485446 "lowpass" : self .config .get_value ("detect-hash" , "lowpass" , lowpass ),
486- "min_scene_len" : min_scene_len ,
447+ "min_scene_len" : min_scene_len_frames ,
487448 "size" : self .config .get_value ("detect-hash" , "size" , size ),
488449 "threshold" : self .config .get_value ("detect-hash" , "threshold" , threshold ),
489450 }
0 commit comments