-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (46 loc) · 1.94 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import os
import time
import tkinter.filedialog
import torch
import whisper
def select_file():
file_types = [("Audio Files", "*.wav *.mp3 *.m4a *.ogg *.flac"), ("Video Files", "*.mp4 *.mkv *.mov *.avi *.flv"), ("All Files", "*.*")]
file_path = tkinter.filedialog.askopenfilename(title="Select an audio or video file", filetypes=file_types)
return file_path
def transcribe_file(file_path):
# GPUが利用可能かどうかの確認
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
# Whisperモデルのロード
model_name = "large" # tiny, base, small, medium, large, large-v2, large-v3
model = whisper.load_model(model_name, device=device)
# タイマー計測開始
t0 = time.perf_counter()
# 音声ファイルの文字起こし
result = model.transcribe(file_path)
# タイマー計測終了
t1 = time.perf_counter()
print(f"Transcribed in {t1-t0:.02f}s")
# 文字起こし結果の出力
print(result["text"])
# 保存先のフォルダ
output_folder = "./output"
os.makedirs(output_folder, exist_ok=True)
# ファイル名
file_name = os.path.splitext(os.path.basename(file_path))[0]
# 結果をJSONファイルに保存
json_output_path = f"./output/{file_name}_transcription_{model_name}.json"
with open(json_output_path, "w", encoding="utf-8") as json_file:
json.dump(result, json_file, ensure_ascii=False, indent=4)
# テキスト部分をTXTファイルに保存
txt_output_path = f"./output/{file_name}_transcription_{model_name}.txt"
with open(txt_output_path, "w", encoding="utf-8") as txt_file:
txt_file.write(str(result["text"]))
print(f"Transcription result saved as {json_output_path} and {txt_output_path}")
if __name__ == "__main__":
file_path = select_file()
if file_path:
transcribe_file(file_path)
else:
print("No file selected")