Class: Wco::Newspartial

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paperclip, Mongoid::Paranoia, Mongoid::Timestamps, Utils
Defined in:
app/models/wco/newspartial.rb

Constant Summary collapse

PAGE_PARAM_NAME =
'newspartials_page'

Instance Method Summary collapse

Methods included from Utils

#export, included

Instance Method Details

#configObject



54
55
56
# File 'app/models/wco/newspartial.rb', line 54

def config
  @config ||= JSON.parse self[:config_json]
end

#duration_msObject



57
58
59
# File 'app/models/wco/newspartial.rb', line 57

def duration_ms
  duration = config['vtimes'].last.to_i + config['vdurations'].last.to_i rescue 0
end

#generate_speechObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/wco/newspartial.rb', line 61

def generate_speech
  out = HTTParty.post( "#{HEAD_TTS_ORIGIN}/v1/synthesize",
    headers: {
      'Content-Type' => 'application/json',
      'Accept' => 'application/json',
    }, body: {
      input: body,
      voice: "af_bella",
      language: "en-us",
      audioEncoding: "wav",
    }.to_json
  );
  out = out.body
  puts! out, 'out'

  self[:config_json] = out
  tmp = JSON.parse( out )

  decoded_audio = Base64.decode64( tmp['audio'] )
  temp_file = Tempfile.new(['speech', '.wav'])
  temp_file.binmode
  temp_file.write(decoded_audio)
  temp_file.rewind
  self.audio = temp_file
  temp_file.close
  temp_file.unlink
  tmp.delete('audio')

  self[:speech_json] = tmp.to_json

  self.save
  return out
end

#generate_videoObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/wco/newspartial.rb', line 95

def generate_video
  cmd = "cd #{ISHLIB3JS_ROOT} ;
    node ./src/talking_head/example_puppeteer_wired.js \
      --api_key=#{SIMPLE_API_KEY} \
      --api_secret=#{SIMPLE_API_SECRET} \
      --wco_origin=#{WCO_ORIGIN} \
      --newspartial_id=#{self[:id]} ";

  puts "+++ cmd:"
  puts cmd
  begin
    out = `#{cmd}`
  rescue err
    puts! err, 'err'
  end
  puts! out, 'out'
  return out
end

#videoObject



114
115
116
# File 'app/models/wco/newspartial.rb', line 114

def video
  Wco::Video.where( newspartial_id: self[:id] ).first
end