Module: SpeechToText::AmazonS2T

Includes:
Util
Defined in:
lib/speech_to_text/amazon.rb

Class Method Summary collapse

Methods included from Util

captions_json, recording_json, seconds_to_timestamp, video_to_audio, write_to_webvtt

Class Method Details

.checkstatus(transcription_job_name) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/speech_to_text/amazon.rb', line 47

def self.checkstatus(transcription_job_name)
  client = Aws::TranscribeService::Client.new
  resp = client.get_transcription_job({
    transcription_job_name: transcription_job_name
  })

  status = resp['transcription_job']['transcription_job_status']
  return status
end

.create_amazon_array(data) ⇒ Object

rubocop:disable Metrics/AbcSize



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/speech_to_text/amazon.rb', line 73

def self.create_amazon_array(data) # rubocop:disable Metrics/AbcSize
  if data.nil?
    puts "no json data found"
    return
  end

  i = 0
  myarray = []
  while (i < data['results']['items'].length)
    unless data['results']['items'][i]['start_time'].nil?
      myarray.push(data['results']['items'][i]['start_time'].to_f)
      myarray.push(data['results']['items'][i]['end_time'].to_f)
      myarray.push(data['results']['items'][i]['alternatives'][0]['content'])
    end
    i = i + 1
  end
  return myarray
end

.create_job(transcription_job_name, language_code, audio_format, s3_audio_uri) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/speech_to_text/amazon.rb', line 35

def self.create_job(transcription_job_name, language_code, audio_format, s3_audio_uri)
  client = Aws::TranscribeService::Client.new
  resp = client.start_transcription_job({
    transcription_job_name: transcription_job_name,
    language_code: language_code,
    media_format: audio_format,
    media: {
      media_file_uri: s3_audio_uri
    }
  })
end

.delete_audio(bucket_name, s3_audio_name, transcription_job_name) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/speech_to_text/amazon.rb', line 92

def self.delete_audio(bucket_name, s3_audio_name, transcription_job_name)
  s3 = Aws::S3::Resource.new
  obj = s3.bucket(bucket_name).object(s3_audio_name)
  obj.delete

  client = Aws::TranscribeService::Client.new
  resp = client.delete_transcription_job({
    transcription_job_name: transcription_job_name
  })
end

.get_words(transcription_job_name, json_file) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/speech_to_text/amazon.rb', line 57

def self.get_words(transcription_job_name, json_file)
  client = Aws::TranscribeService::Client.new
  resp = client.get_transcription_job({
    transcription_job_name: transcription_job_name
  })

  uri = resp['transcription_job']['transcript']['transcript_file_uri']
  File.open(json_file, 'wb') do |file|
     file.write open(uri).read
  end

  file = File.open(json_file,'r')
  data = JSON.load file
  return data
end

.set_credentials(aws_key, aws_secret) ⇒ Object



22
23
24
25
26
27
# File 'lib/speech_to_text/amazon.rb', line 22

def self.set_credentials(aws_key, aws_secret)
  Aws.config.update({
    region: 'us-east-2',
    credentials: Aws::Credentials.new(aws_key, aws_secret)
  })
end

.upload_audio(bucket_name, s3_audio_name, audio_file) ⇒ Object



29
30
31
32
33
# File 'lib/speech_to_text/amazon.rb', line 29

def self.upload_audio(bucket_name, s3_audio_name, audio_file)
  s3 = Aws::S3::Resource.new
  obj = s3.bucket(bucket_name).object(s3_audio_name)
  obj.upload_file(audio_file)
end