-
-
Save hyuki/6180f025c4da2349d7723dcb9faf5543 to your computer and use it in GitHub Desktop.
speech.rb - OpenAIのAPIを使ってテキストファイルを音声MP3に変換するRubyスクリプト
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
# ◆Ruby OpenAI | |
# https://github.com/alexrudall/ruby-openai | |
# $ gem install ruby-openai | |
# | |
# ◆Text to speech - OpenAI API (voice options) | |
# https://platform.openai.com/docs/guides/text-to-speech/voice-options | |
# | |
# ◆API keys | |
# https://platform.openai.com/api-keys | |
# $ grep OPENAI_API_KEY ~/.bash_profile | |
# export OPENAI_API_KEY='sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
# | |
# ◆Billing | |
# https://platform.openai.com/account/billing/overview | |
require "openai" | |
if ARGV.size != 3 | |
abort "Usage: ruby speech.rb voice input.txt output.mp3" | |
end | |
voice = ARGV[0] | |
input_txt = ARGV[1] | |
output_mp3 = ARGV[2] | |
input = File.read(input_txt) | |
# 引数の表示 | |
puts "voice: #{voice}" | |
puts "input:\n#{input}" | |
puts "mp3: #{output_mp3}" | |
# クライアントの生成 | |
client = OpenAI::Client.new(access_token: ENV["OPENAI_API_KEY"]) | |
# 音声合成 | |
response = client.audio.speech( | |
parameters: { | |
model: "tts-1", | |
input: input, | |
voice: voice, | |
} | |
) | |
# 結果の書き込み | |
File.binwrite(output_mp3, response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment