Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to force the language #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion auto_subtitle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import argparse
import warnings
import tempfile
from whisper.tokenizer import LANGUAGES
from .utils import filename, str2bool, write_srt


Expand All @@ -22,6 +23,8 @@ def main():
help="only generate the .srt file and not create overlayed video")
parser.add_argument("--verbose", type=str2bool, default=False,
help="whether to print out the progress and debug messages")
parser.add_argument("--language", type=str,
help=f"force the use of a chosen language: {list(LANGUAGES.keys())} {list(LANGUAGES.values())})")

parser.add_argument("--task", type=str, default="transcribe", choices=[
"transcribe", "translate"], help="whether to perform X->X speech recognition ('transcribe') or X->English translation ('translate')")
Expand All @@ -31,8 +34,18 @@ def main():
output_dir: str = args.pop("output_dir")
output_srt: bool = args.pop("output_srt")
srt_only: bool = args.pop("srt_only")
language: str = args.pop("language")
os.makedirs(output_dir, exist_ok=True)

if language is not None:
if language not in LANGUAGES:
raise Exception(
f'whisper: error: argument --language: invalid choice: {language} (choose from {list(LANGUAGES.keys())}) {list(LANGUAGES.values())}')
else:
warnings.warn(
f"You have forced the use of the {language} language.")
args["language"] = language

if model_name.endswith(".en"):
warnings.warn(
f"{model_name} is an English-only model, forcing English detection.")
Expand All @@ -41,7 +54,8 @@ def main():
model = whisper.load_model(model_name)
audios = get_audio(args.pop("video"))
subtitles = get_subtitles(
audios, output_srt or srt_only, output_dir, lambda audio_path: model.transcribe(audio_path, **args)
audios, output_srt or srt_only, output_dir, lambda audio_path: model.transcribe(
audio_path, **args)
)

if srt_only:
Expand Down