Skip to content

Instantly share code, notes, and snippets.

@7m4mon
Created December 13, 2023 11:50
Show Gist options
  • Save 7m4mon/a75e57f8d3320f601488df352dc68f64 to your computer and use it in GitHub Desktop.
Save 7m4mon/a75e57f8d3320f601488df352dc68f64 to your computer and use it in GitHub Desktop.
フォルダの中のwavファイルを一括で文字起こしする。
# -*- coding: utf-8 -*-
# 参照元:https://di-acc2.com/programming/python/25061/
# フォルダの中のwavファイルを一括で文字起こしする。
import openai, os
openai.organization = "org-xxxxxxxxxxxxxxxxxxxxxxxx"
openai.api_key = "sk-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
import glob
# 音声文字起こし関数(引数:音声ファイルパス)
def speech_to_text(filepath):
# ファイルサイズを確認 25MB以下
if os.path.getsize(filepath) > 25000000:
print("file size over")
return
# ファイルを開く
audio_file= open(filepath, "rb")
# Speech to Text変換
response = openai.Audio.transcribe(model = "whisper-1", # Speech-to-Textモデル
file = audio_file, # オーディオファイル
)
# 変換後のテキスト出力
return response.text
files = glob.glob("C:/tmp/*.wav")
for file in files:
# 音声ファイルのパスを指定 mp3, mp4, wav, mpeg, mpga(25MB以下)
print(file)
result = speech_to_text(file)
print(result)
# 保存するテキストファイルの名前
resultTxtFilename = file + ".txt"
with open(resultTxtFilename, 'w') as f:
f.write(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment