This project provides a Python script to automatically generate subtitles for audio and video files. It can also translate the subtitles into different languages. The script is optimized for Apple Silicon devices using the MLX framework.
- Fast Transcription: Utilizes
mlx-whisperfor efficient audio transcription on Apple Silicon. - Translation: Can translate subtitles into any target language using local LLMs with
lmstudio. - Subtitle Generation: Creates subtitle files in
.srtformat. - Batch Processing: Can process multiple files at once.
-
Clone the repository:
git clone https://github.com/meteoroh/subtitle-generator-mlx cd subtitle-generator-mlx -
Install uv:
brew install uv
-
Install the dependencies:
uv sync
-
Place your audio or video files in a directory.
-
Modify the
main.pyscript to point to your files and set the desired options:if __name__ == '__main__': # Initialize the generator with a Whisper model and an optional translator model generator = SubtitleGenerator('whisper-large-v3-mlx', 'google/gemma-3-12b') # --- Examples --- # 1. Transcribe a single file and save the transcription output as a JSON file # generator.transcribe('/path/to/your/audio.mp3', source_language='ja', log=True) # 2. Generate translated subtitles for a single file # generator.generate('/path/to/your/audio.mp3', translation=True, source_language='ja', target_language='en', log=True) # 3. Generate subtitles in the original language for a single file # generator.generate('/path/to/your/audio.mp3', source_language='ja') # 4. Generate translated subtitles for all mp4 files in a directory # generator.generate_all('/path/to/your/*.mp4', translation=True, source_language='ja', target_language='en')
-
Run the script:
uv run python main.py
The generated subtitle files will be saved in the
resultsdirectory.
You can configure the transcription options by creating a settings.json file in the root of the project directory. The script will automatically load the settings from this file.
This is optional. If the file does not exist, the script will use the default settings.
Here is an example of a settings.json file:
{
"transcription_options": {
"temperature": 0.0,
"logprob_threshold": -0.8,
"no_speech_threshold": 0.5,
"condition_on_previous_text": false,
"word_timestamps": true,
"hallucination_silence_threshold": 1
}
}Here are the parameters for the methods in the SubtitleGenerator class:
whisper_model(essential): The name of the Whisper model to use for transcription (e.g.,'whisper-large-v3-mlx').translator_model(optional): The name of the translator model to use for translation (e.g.,'google/gemma-3-12b').
path(essential): The path to the audio or video file.source_language(optional): The source language of the audio. If not provided, it will be auto-detected.log(optional): IfTrue, it saves the raw transcription output as a JSON file in theresultsdirectory and sets the whisper model'sverboseoption toTrue, displaying the text being decoded to the console. Defaults toTrue.
path(essential): The path to the audio or video file.translation(optional): Whether to translate the subtitles. Defaults toFalse.source_language(optional): The source language of the audio. If not provided, it will be auto-detected.target_language(essential iftranslation=True): The target language for the translation.log(optional): IfTrue, it saves the raw transcription output as a JSON file in theresultsdirectory and sets the whisper model'sverboseoption toTrue, displaying the text being decoded to the console. Defaults toFalse.
path(essential): A glob pattern for the files to process (e.g.,'/path/to/your/*.webm').translation(optional): Whether to translate the subtitles. Defaults toFalse.source_language(optional): The source language of the audio. If not provided, it will be auto-detected.target_language(essential iftranslation=True): The target language for the translation.log(optional): IfTrue, it saves the raw transcription output as a JSON file in theresultsdirectory and sets the whisper model'sverboseoption toTrue, displaying the text being decoded to the console. Defaults toFalse.
This script uses two types of models:
-
Whisper Model: For transcription. The model is downloaded from the Hugging Face Hub via
mlx-community. The default model iswhisper-large-v3-mlx. You can find more models here. -
Translator Model: For translation. This script uses
lmstudioto run a local LLM for translation. You need to havelmstudioinstalled and the desired model downloaded. The default model isgoogle/gemma-3-12b.
This project is licensed under the MIT License.