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

Enable saving subtitles as .txt file #72

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
14 changes: 12 additions & 2 deletions auto_subtitle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def main():
default=".", help="directory to save the outputs")
parser.add_argument("--output_srt", type=str2bool, default=False,
help="whether to output the .srt file along with the video files")
parser.add_argument("--output_txt", type=str2bool, default=False,
help="whether to save the subtitles in a txt file")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would read better?

Suggested change
help="whether to save the subtitles in a txt file")
help="whether to also output the subtitles as a .txt file")

.....the existing description implies that a .txt file is generated instead of other outputs, which isn't the case I note from reading the code.

parser.add_argument("--srt_only", type=str2bool, default=False,
help="only generate the .srt file and not create overlayed video")
parser.add_argument("--verbose", type=str2bool, default=False,
Expand All @@ -32,6 +34,7 @@ def main():
model_name: str = args.pop("model")
output_dir: str = args.pop("output_dir")
output_srt: bool = args.pop("output_srt")
output_txt: bool = args.pop("output_txt")
srt_only: bool = args.pop("srt_only")
language: str = args.pop("language")

Expand All @@ -48,7 +51,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_txt, output_dir, lambda audio_path: model.transcribe(audio_path, **args)
)

if srt_only:
Expand Down Expand Up @@ -88,7 +91,7 @@ def get_audio(paths):
return audio_paths


def get_subtitles(audio_paths: list, output_srt: bool, output_dir: str, transcribe: callable):
def get_subtitles(audio_paths: list, output_srt: bool, output_txt: bool, output_dir: str, transcribe: callable):
subtitles_path = {}

for path, audio_path in audio_paths.items():
Expand All @@ -108,6 +111,13 @@ def get_subtitles(audio_paths: list, output_srt: bool, output_dir: str, transcri

subtitles_path[path] = srt_path

if output_txt:
text_path = os.path.join(output_dir, f"{filename(path)}.txt")
with open(text_path, "w", encoding="utf-8") as text_file:
for segment in result["segments"]:
print(segment["text"], file=text_file)
print(f"Saving subtitles to text file: {text_path}")

return subtitles_path


Expand Down