Skip to content

Instantly share code, notes, and snippets.

@yasinuygun
Last active January 6, 2024 08:43
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasinuygun/a5aaa24af9f5531872e4c6c863fd793f to your computer and use it in GitHub Desktop.
Save yasinuygun/a5aaa24af9f5531872e4c6c863fd793f to your computer and use it in GitHub Desktop.
Whisper Subtitle Generator
import sys
import whisper
from whisper.utils import write_srt
def run(input_path: str, output_path: str) -> None:
model = whisper.load_model("base")
result = model.transcribe(input_path)
with open(output_path, "w", encoding="utf-8") as srt_file:
write_srt(result["segments"], file=srt_file)
def main() -> None:
if len(sys.argv) != 3:
print(
"Error: Invalid number of arguments.\n"
"Usage: python whisper_subtitle_generator.py <input-path> <output-path>\n"
"Example: python whisper_subtitle_generator.py 'video.mp4' 'subtitle.srt'"
)
sys.exit(1)
run(input_path=sys.argv[1], output_path=sys.argv[2])
if __name__ == "__main__":
main()
@yasinuygun
Copy link
Author

Install the following dependencies to make this work.

brew install ffmpeg
pip install git+https://github.com/openai/whisper.git

@gavink97
Copy link

gavink97 commented Jan 6, 2024

write_srt api is outdated, use get_writer instead.

import whisper
from whisper.utils import get_writer

whisper = whisper.load_model("tiny.en", device="cpu")
result = whisper.transcribe(audio, language="en", fp16=False)
writer = get_writer("srt", output_dir)
writer(result, audio)

Here's an article for further details

@yasinuygun
Copy link
Author

yasinuygun commented Jan 6, 2024

Thanks @gavink97, yes it is outdated. One can either apply your suggestion, or install the version of the old commit.

pip install git+https://github.com/openai/whisper.git@0b5dcfdef7ec04250b76e13f1630e32b0935ce76

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment