Skip to content

Commit 3fb8d8a

Browse files
committed
[cli] Decode enum types directly in config parser
1 parent 45d2727 commit 3fb8d8a

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

scenedetect/_cli/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
CONFIG_MAP,
3535
DEFAULT_JPG_QUALITY,
3636
DEFAULT_WEBP_QUALITY,
37-
TimecodeFormat,
3837
)
3938
from scenedetect._cli.context import USER_CONFIG, CliContext, check_split_video_requirements
4039
from scenedetect.backends import AVAILABLE_BACKENDS
@@ -46,7 +45,6 @@
4645
ThresholdDetector,
4746
)
4847
from scenedetect.platform import get_cv2_imwrite_params, get_system_version_info
49-
from scenedetect.scene_manager import Interpolation
5048

5149
_PROGRAM_VERSION = scenedetect.__version__
5250
"""Used to avoid name conflict with named `scenedetect` command below."""
@@ -1068,7 +1066,7 @@ def list_scenes_command(
10681066
output_dir = ctx.config.get_value("list-scenes", "output", output)
10691067
name_format = ctx.config.get_value("list-scenes", "filename", filename)
10701068
list_scenes_args = {
1071-
"cut_format": TimecodeFormat[ctx.config.get_value("list-scenes", "cut-format").upper()],
1069+
"cut_format": ctx.config.get_value("list-scenes", "cut-format"),
10721070
"display_scenes": ctx.config.get_value("list-scenes", "display-scenes"),
10731071
"display_cuts": ctx.config.get_value("list-scenes", "display-cuts"),
10741072
"scene_list_output": create_file,
@@ -1407,7 +1405,7 @@ def save_images_command(
14071405
scale = ctx.config.get_value("save-images", "scale")
14081406
height = ctx.config.get_value("save-images", "height")
14091407
width = ctx.config.get_value("save-images", "width")
1410-
scale_method = Interpolation[ctx.config.get_value("save-images", "scale-method").upper()]
1408+
scale_method = ctx.config.get_value("save-images", "scale-method")
14111409
quality = (
14121410
(DEFAULT_WEBP_QUALITY if webp else DEFAULT_JPG_QUALITY)
14131411
if ctx.config.is_default("save-images", "quality")

scenedetect/_cli/config.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def format(self, timecode: FrameTimecode) -> str:
268268
"weights": ScoreWeightsValue(ContentDetector.DEFAULT_COMPONENT_WEIGHTS),
269269
},
270270
"detect-content": {
271-
"filter-mode": "merge",
271+
"filter-mode": FlashFilter.Mode.MERGE,
272272
"kernel-size": KernelSizeValue(-1),
273273
"luma-only": False,
274274
"min-scene-len": TimecodeValue(0),
@@ -302,7 +302,7 @@ def format(self, timecode: FrameTimecode) -> str:
302302
"no-images": False,
303303
},
304304
"list-scenes": {
305-
"cut-format": "timecode",
305+
"cut-format": TimecodeFormat.TIMECODE,
306306
"display-cuts": True,
307307
"display-scenes": True,
308308
"filename": "$VIDEO_NAME-Scenes.csv",
@@ -315,7 +315,7 @@ def format(self, timecode: FrameTimecode) -> str:
315315
"backend": "opencv",
316316
"default-detector": "detect-adaptive",
317317
"downscale": 0,
318-
"downscale-method": "linear",
318+
"downscale-method": Interpolation.LINEAR,
319319
"drop-short-scenes": False,
320320
"frame-skip": 0,
321321
"merge-last-scene": False,
@@ -333,7 +333,7 @@ def format(self, timecode: FrameTimecode) -> str:
333333
"output": None,
334334
"quality": RangeValue(_PLACEHOLDER, min_val=0, max_val=100),
335335
"scale": 1.0,
336-
"scale-method": "linear",
336+
"scale-method": Interpolation.LINEAR,
337337
"width": 0,
338338
},
339339
"split-video": {
@@ -442,6 +442,27 @@ def _parse_config(config: ConfigParser) -> Tuple[ConfigDict, List[str]]:
442442
value_type = "number"
443443
out_map[command][option] = config.getfloat(command, option)
444444
continue
445+
elif isinstance(CONFIG_MAP[command][option], Enum):
446+
config_value = (
447+
config.get(command, option).replace("\n", " ").strip().upper()
448+
)
449+
try:
450+
parsed = CONFIG_MAP[command][option].__class__[config_value]
451+
out_map[command][option] = parsed
452+
except TypeError:
453+
errors.append(
454+
"Invalid [%s] value for %s: %s. Must be one of: %s."
455+
% (
456+
command,
457+
option,
458+
config.get(command, option),
459+
", ".join(
460+
str(choice) for choice in CHOICE_MAP[command][option]
461+
),
462+
)
463+
)
464+
continue
465+
445466
except ValueError as _:
446467
errors.append(
447468
"Invalid [%s] value for %s: %s is not a valid %s."

scenedetect/_cli/context.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,8 @@ def handle_options(
286286
except ValueError as ex:
287287
logger.debug(str(ex))
288288
raise click.BadParameter(str(ex), param_hint="downscale factor") from None
289-
scene_manager.interpolation = Interpolation[
290-
self.config.get_value("global", "downscale-method").upper()
291-
]
289+
scene_manager.interpolation = self.config.get_value("global", "downscale-method")
290+
292291
self.scene_manager = scene_manager
293292

294293
#
@@ -328,9 +327,7 @@ def get_detect_content_params(
328327
"luma_only": luma_only or self.config.get_value("detect-content", "luma-only"),
329328
"min_scene_len": min_scene_len,
330329
"threshold": self.config.get_value("detect-content", "threshold", threshold),
331-
"filter_mode": FlashFilter.Mode[
332-
self.config.get_value("detect-content", "filter-mode", filter_mode).upper()
333-
],
330+
"filter_mode": self.config.get_value("detect-content", "filter-mode", filter_mode),
334331
}
335332

336333
def get_detect_adaptive_params(

0 commit comments

Comments
 (0)