diff --git a/whisper/mlx_whisper/cli.py b/whisper/mlx_whisper/cli.py index ee8212648..06a0f7728 100644 --- a/whisper/mlx_whisper/cli.py +++ b/whisper/mlx_whisper/cli.py @@ -237,16 +237,16 @@ def main(): # receive the contents from stdin rather than read a file audio_obj = audio.load_audio(from_stdin=True) - output_name = output_name or "content" + file_output_name = output_name or "content" else: - output_name = output_name or pathlib.Path(audio_obj).stem + file_output_name = output_name or pathlib.Path(audio_obj).stem try: result = transcribe( audio_obj, path_or_hf_repo=path_or_hf_repo, **args, ) - writer(result, output_name, **writer_args) + writer(result, file_output_name, **writer_args) except Exception as e: traceback.print_exc() print(f"Skipping {audio_obj} due to {type(e).__name__}: {str(e)}") diff --git a/whisper/test_cli.py b/whisper/test_cli.py new file mode 100644 index 000000000..4841846d2 --- /dev/null +++ b/whisper/test_cli.py @@ -0,0 +1,42 @@ +import sys +import tempfile +import unittest +from unittest.mock import Mock, patch + +from mlx_whisper import cli + + +class TestCLI(unittest.TestCase): + def test_multiple_audio_files_get_distinct_output_names(self): + writer = Mock() + transcribe = Mock(return_value={"segments": []}) + + with tempfile.TemporaryDirectory() as output_dir: + argv = [ + "mlx_whisper", + "first.mp3", + "second.mp3", + "--output-dir", + output_dir, + "--verbose", + "False", + ] + with ( + patch.object(cli, "get_writer", return_value=writer), + patch.object(cli, "transcribe", transcribe), + patch.object(sys, "argv", argv), + ): + cli.main() + + self.assertEqual( + [call.args[1] for call in writer.call_args_list], + ["first", "second"], + ) + self.assertEqual( + [call.args[0] for call in transcribe.call_args_list], + ["first.mp3", "second.mp3"], + ) + + +if __name__ == "__main__": + unittest.main()