Created
August 1, 2023 13:20
-
-
Save 7m4mon/daf4088fc561149573dee91152aee127 to your computer and use it in GitHub Desktop.
openai_speech_to_text.py
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
# -*- coding: utf-8 -*- | |
# 参照元:https://di-acc2.com/programming/python/25061/ | |
# に、テキストファイルの保存を追加しただけ。 | |
import openai, os | |
openai.organization = "org-xxxxxxxxxxxxxxxxxxxxxxxx" | |
openai.api_key = "sk-zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" | |
# 音声ファイルのパスを指定 mp3, mp4, wav, mpeg, mpga(25MB以下) | |
filepath = "./20230731_203001-1.wav" | |
# 保存するテキストファイルの名前 | |
resultTxtFilename = filepath + ".txt" | |
# 音声文字起こし関数(引数:音声ファイルパス) | |
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 | |
# 関数実行 | |
result = speech_to_text(filepath) | |
print(result) | |
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