Last active
May 8, 2026 07:00
-
-
Save taroyanaka/4863aa879746baaa7341e70a9d3d56f6 to your computer and use it in GitHub Desktop.
python .\faster_whisper_test_gen_srt.py (同じディレクトリのinput.mp4が読み込まれる) https://gist.github.com/taroyanaka/952513581d39065509bb39be3889b2c7
This file contains hidden or 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
| ######## whisperのモデルダウンロード ######## | |
| # from faster_whisper import WhisperModel | |
| # | |
| # model = WhisperModel("large-v3") | |
| # | |
| # print("モデルDL完了") | |
| ############################################# | |
| from faster_whisper import WhisperModel | |
| # ========================= | |
| # Whisper | |
| # ========================= | |
| model = WhisperModel( | |
| "large-v3", | |
| device="cpu", | |
| compute_type="int8" | |
| ) | |
| segments, info = model.transcribe( | |
| "input.mp4", | |
| language="ja" | |
| ) | |
| # generator を全部読み切る | |
| segments = list(segments) | |
| print(f"検出言語: {info.language}") | |
| for segment in segments: | |
| print( | |
| f"[{segment.start:.2f}s - " | |
| f"{segment.end:.2f}s] " | |
| f"{segment.text}" | |
| ) | |
| print("文字起こし完了") | |
| # ========================= | |
| # SRT 時間変換 | |
| # ========================= | |
| def format_srt_time(seconds): | |
| hours = int(seconds // 3600) | |
| minutes = int( | |
| (seconds % 3600) // 60 | |
| ) | |
| secs = int(seconds % 60) | |
| milliseconds = int( | |
| (seconds - int(seconds)) * 1000 | |
| ) | |
| return ( | |
| f"{hours:02}:" | |
| f"{minutes:02}:" | |
| f"{secs:02}," | |
| f"{milliseconds:03}" | |
| ) | |
| # ========================= | |
| # SRT 出力 | |
| # ========================= | |
| output_srt = "subtitle.srt" | |
| with open( | |
| output_srt, | |
| "w", | |
| encoding="utf-8" | |
| ) as f: | |
| index = 1 | |
| for segment in segments: | |
| text = segment.text.strip() | |
| if not text: | |
| continue | |
| start_time = format_srt_time( | |
| segment.start | |
| ) | |
| end_time = format_srt_time( | |
| segment.end | |
| ) | |
| f.write(f"{index}\n") | |
| f.write( | |
| f"{start_time} --> " | |
| f"{end_time}\n" | |
| ) | |
| f.write(f"{text}\n\n") | |
| index += 1 | |
| print( | |
| f"SRTファイル出力完了: " | |
| f"{output_srt}" | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment