Skip to content

Instantly share code, notes, and snippets.

@kobaltz
Last active November 2, 2018 23:39
Show Gist options
  • Save kobaltz/591fe58663ee9801637cc0ccf0ece975 to your computer and use it in GitHub Desktop.
Save kobaltz/591fe58663ee9801637cc0ccf0ece975 to your computer and use it in GitHub Desktop.
Watson Speech to Text Conversion
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'dotenv/load'
TYPE_OF_FILE_TO_CONVERT = '*.mp4'
# Sign up for an account and confirm your email
# https://console.bluemix.net/registration/
# Generate a Speech to Text App
# https://console.bluemix.net/catalog/services/speech-to-text
# Copy the API Key and the Service URL to these Environment Variables
WATSON_API_KEY = ENV['WATSON_API_KEY']
WATSON_SERVICE_URL = ENV['WATSON_SERVICE_URL']
# i.e., for DC - WATSON_SERVICE_URL = 'https://gateway-wdc.watsonplatform.net/speech-to-text/api/v1/recognize'
# Install some tools first if you need to convert video to mp3
# brew install ffmpeg
# brew install lame
# Place this file in the directory that you have files to convert.
# Make this file executable
# chmod +x convert.rb
# Let er rip!
# ./convert.rb
def convert
Dir.glob(TYPE_OF_FILE_TO_CONVERT) do |file|
@file = file
# This step can be commented out if your file is already an MP3
convert_file_to_mp3(file)
convert_mp3_to_text
end
end
def convert_file_to_mp3(input)
`ffmpeg -i #{input} -b:a 320K -vn #{mp3_output} -y`
end
def convert_mp3_to_text
generate_text_file(make_api_request)
end
def make_api_request
uri = URI.parse(WATSON_SERVICE_URL)
request = Net::HTTP::Post.new(uri)
request.basic_auth('apikey', WATSON_API_KEY)
request.content_type = 'audio/mp3'
request.body = File.read(mp3_output)
req_options = { use_ssl: uri.scheme == 'https' }
Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
end
def generate_text_file(response)
body = response.body
json = JSON.parse(body)
text = json.dig('results').to_a.map { |e| e.dig('alternatives') }.to_a.flatten.map { |d| d.dig('transcript') }.to_a.join(' ').gsub(' ', ' ')
File.open(text_output, 'w') do |file|
file.write text
end
end
def basename
File.basename(@file, '.*')
end
def mp3_output
basename + '.mp3'
end
def text_output
basename + '.txt'
end
convert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment