4545import os
4646import subprocess as sp
4747from collections .abc import Iterable , Sequence
48+ from functools import cache
4849from pathlib import Path
4950from re import match
5051from typing import Any
@@ -122,6 +123,18 @@ def get_file_info(
122123 return json .loads (sp .check_output (cmds ).decode ()) # noqa: S603
123124
124125
126+ @cache
127+ def _verify_mkvmerge_cached (mkvmerge_path_tuple : tuple [str , ...]) -> bool :
128+ """Internal cached function to verify mkvmerge availability."""
129+ try :
130+ mkvmerge_command = list (mkvmerge_path_tuple )
131+ mkvmerge_command .append ("-V" )
132+ output = sp .check_output (mkvmerge_command ).decode () # noqa: S603
133+ except (sp .CalledProcessError , FileNotFoundError ):
134+ return False
135+ return bool (match ("mkvmerge.*" , output ))
136+
137+
125138def verify_mkvmerge (
126139 mkvmerge_path : str | os .PathLike | Iterable [str ] = "mkvmerge" ,
127140) -> bool :
@@ -137,13 +150,10 @@ def verify_mkvmerge(
137150 bool
138151 True if mkvmerge is available at the specified path, False otherwise.
139152 """
140- try :
141- mkvmerge_command = list (prepare_mkvtoolnix_path (mkvmerge_path ))
142- mkvmerge_command .append ("-V" )
143- output = sp .check_output (mkvmerge_command ).decode () # noqa: S603
144- except (sp .CalledProcessError , FileNotFoundError ):
145- return False
146- return bool (match ("mkvmerge.*" , output ))
153+ mkvmerge_command = prepare_mkvtoolnix_path (mkvmerge_path )
154+ if isinstance (mkvmerge_command , list ):
155+ mkvmerge_command = tuple (mkvmerge_command )
156+ return _verify_mkvmerge_cached (mkvmerge_command )
147157
148158
149159def verify_matroska (
0 commit comments