Last active
January 6, 2024 08:43
-
-
Save yasinuygun/a5aaa24af9f5531872e4c6c863fd793f to your computer and use it in GitHub Desktop.
Whisper Subtitle Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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)
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
Install the following dependencies to make this work.