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

Sample update to enable variable length subtitle #66

Open
wants to merge 1 commit 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
23 changes: 14 additions & 9 deletions auto_subtitle/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import ffmpeg
import whisper
from whisper.utils import get_writer
import argparse
import warnings
import tempfile
from .utils import filename, str2bool, write_srt
from utils import filename, str2bool, write_srt


def main():
Expand Down Expand Up @@ -48,7 +49,7 @@ 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, model.transcribe
)

if srt_only:
Expand All @@ -61,7 +62,6 @@ def main():

video = ffmpeg.input(path)
audio = video.audio

ffmpeg.concat(
video.filter('subtitles', srt_path, force_style="OutlineColour=&H40000000,BorderStyle=3"), audio, v=1, a=1
).output(out_path).run(quiet=True, overwrite_output=True)
Expand Down Expand Up @@ -92,20 +92,25 @@ def get_subtitles(audio_paths: list, output_srt: bool, output_dir: str, transcri
subtitles_path = {}

for path, audio_path in audio_paths.items():
srt_path = output_dir if output_srt else tempfile.gettempdir()
srt_path = output_dir if output_srt else output_dir
srt_path = os.path.join(srt_path, f"{filename(path)}.srt")

print(
f"Generating subtitles for {filename(path)}... This might take a while."
)

print(f"Output {srt_path}")

warnings.filterwarnings("ignore")
result = transcribe(audio_path)
result = transcribe(audio_path, initial_prompt="prompt", word_timestamps=True)
warnings.filterwarnings("default")

with open(srt_path, "w", encoding="utf-8") as srt:
write_srt(result["segments"], file=srt)

word_options = {
"highlight_words": False,
"max_line_count": 1,
"max_line_width": 1
}
srt_writer = get_writer("srt", output_dir)
srt_writer(result, srt_path, word_options)
subtitles_path[path] = srt_path

return subtitles_path
Expand Down