Class: StepsRecorderAnalyser

Inherits:
Object
  • Object
show all
Includes:
WavTool
Defined in:
lib/sra2019.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WavTool

#ogg_to_wav, #wav_concat, #wav_silence

Constructor Details

#initialize(s, debug: false, savepath: '/tmp', title: 'Untitled', working_dir: '/tmp', pollyspeech: {access_key: nil, secret_key: nil, voice_id: 'Amy', cache_filepath: '/tmp/pollyspeech/cache'}) ⇒ StepsRecorderAnalyser

Returns a new instance of StepsRecorderAnalyser.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sra2019.rb', line 85

def initialize(s, debug: false, savepath: '/tmp', title: 'Untitled', 
               working_dir: '/tmp', pollyspeech: {access_key: nil, 
               secret_key: nil, voice_id: 'Amy', 
               cache_filepath: '/tmp/pollyspeech/cache'})

  @savepath, @title, @working_dir, @debug = savepath, title, 
      working_dir, debug
  
  raw_content, type = RXFHelper.read(s)
  
  content = if type == :file and File.extname(s) == '.zip' then
    Zip::ZipFile.new(s).instance_eval {read(to_a[0].name)}
  else
    raw_content
  end

  puts ('content: ' + content.inspect).debug if @debug
  
  @actions = parse_report content
  @all_steps = parse_steps  content  
  @doc = build @all_steps
  steps = @all_steps.select {|x| x[:user_comment]}
  @steps = steps.any? ? steps : @all_steps
  
  # find the duration for each step
  @steps.each.with_index do |x, i|
    
    x.duration = if i < @steps.length - 1 then
      @steps[i+1].time - x.time 
    else
      @duration - x.time 
    end
    
  end

  @pollyspeech = PollySpeech.new(pollyspeech) if pollyspeech[:access_key]
  
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



81
82
83
# File 'lib/sra2019.rb', line 81

def duration
  @duration
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



81
82
83
# File 'lib/sra2019.rb', line 81

def start_time
  @start_time
end

#stepsObject

Returns the value of attribute steps.



82
83
84
# File 'lib/sra2019.rb', line 82

def steps
  @steps
end

Instance Method Details

#add_audio_track(audio_file, video_file, target_video) ⇒ Object

adds the audio track to the video file



126
127
128
129
130
131
132
133
134
# File 'lib/sra2019.rb', line 126

def add_audio_track(audio_file, video_file, target_video)
  
  if block_given? then
    yield(audio_file, video_file, target_video)
  else
    `ffmpeg -i #{video_file} -i #{audio_file} -codec copy -shortest #{target_video}`
  end
  
end

#generate_audio(wav: true) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/sra2019.rb', line 138

def generate_audio(wav: true)
  
  return nil unless @pollyspeech
  
  @steps.each.with_index do |x, i|
    
    puts 'x.desc: ' + x.desc.inspect if @debug
    filename = "voice#{i+1}.ogg"
    
    x.audio = filename
    file = File.join(@working_dir, filename)
    @pollyspeech.tts(x.desc.force_encoding('UTF-8'), file)
    
    x.audio_duration = OggInfo.open(file) {|ogg| ogg.length.to_i }
    x.silence_duration = x.duration - x.audio_duration
    
    if wav then
      
      silent_file = File.join(@working_dir, "silence#{(i+1).to_s}.wav")
      wav_silence silent_file, duration: x.silence_duration        
      ogg_to_wav File.join(@working_dir, "voice#{i+1}.ogg")            
      
    end
    
    sleep 0.6
    
  end
  
  if wav then
    
    files = @steps.length.times.flat_map do |n|
      [
        File.join(@working_dir, "voice#{n+1}.wav"), 
        File.join(@working_dir, "silence#{n+1}.wav")
      ]
    end
    
    wav_concat files, File.join(@working_dir, 'audio.wav')
  end
  
end

#import(s) ⇒ Object



180
181
182
183
184
185
# File 'lib/sra2019.rb', line 180

def import(s)
  
  @dx = Dynarex.new
  @dx.import s
  
end

#tidy!Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/sra2019.rb', line 187

def tidy!()

  verbose_level = 0

  @steps.each do |x|

    x.desc.gsub!(/\s*\([^\)]+\)\s*/,'')
    x.desc.sub!(/ in "\w+"$/,'')
    x.desc.sub!(/"User account for [^"]+"/,'the User account icon.')
    
    if x.desc =~ /User left click/ and verbose_level == 0 then

      x.desc.sub!(/User left click/, 'Using the mouse, left click')
      verbose_level = 1

    elsif x.desc =~ /User left click/ and verbose_level == 1

      x.desc.sub!(/User left click/, 'Left click')
      verbose_level = 2

    elsif x.desc =~ /User left click/ and verbose_level == 2

      x.desc.sub!(/User left click/, 'Click')
      verbose_level = 3

    elsif x.desc =~ /User left click/ and verbose_level == 3

      x.desc.sub!(/User left click on/, 'Select')

    else
      verbose_level = 0
    end

  end
  
end

#to_dxObject

Returns a Dynarex object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/sra2019.rb', line 226

def to_dx()
  
  @dx = Dynarex.new 'instructions/instruction(step, imgsrc, description)'
  @dx.summary[:rawdoc_type] = 'rowx'
  
  @steps.each.with_index do |h, i|
    
    @dx.create step: i+1, imgsrc: "screenshot#{i+1}.jpg", 
        description: h[:user_comment] || h[:desc]
    
  end
  
  @dx
  
end

#to_html(filepath = File.join(@savepath, 'sra' + Time.now.to_i.to_s)) ⇒ Object

Writes the steps to an HTML file



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/sra2019.rb', line 251

def to_html(filepath=File.join(@savepath, 'sra' + Time.now.to_i.to_s))
  
  # save the image files to a file directory.
  # name the sub-directory using the date and time?

  imgpath = File.join(filepath, 'images')
  csspath = File.join(filepath, 'css')
  
  [filepath, imgpath, csspath].each {|x|  FileUtils.mkdir x}

  @steps.each.with_index do |x, i|
    
    File.open(File.join(imgpath, "screenshot#{i}.jpg" ), 'wb') \
      {|f| f.write(x[:screenshot]) }

  end
  

  rows = @steps.map.with_index do |x, i|

li = "
    li
      markdown:
        #{x[:user_comment]}
        
        ![](images/screenshot#{i}.jpg)
"         
  
  end

@sliml=<<EOF
html
head
  title #{@title}: #{@steps.length} Steps (with Pictures)    
  link rel='stylesheet' type='text/css' href='css/layout.css' media='screen,  projection, tv'
  link rel='stylesheet' type='text/css' href='css/style.css' media='screen,  projection, tv'
  link rel='stylesheet' type='text/css' href='css/print.css' media='print'
body
  h1 #{@title}

  ol
    #{rows.join("\n").lstrip}
EOF

  html = Rexle.new(Hlt.new(@sliml).to_html)\
      .root.element('html').xml pretty: true
  File.write File.join(filepath, 'index.html'), html

  %w(layout style print).each \
      {|x| File.write File.join(csspath, "%s.css" % x), ''}
  
  'saved'
  
end

#to_kbml(options = {}) ⇒ Object



242
243
244
245
246
# File 'lib/sra2019.rb', line 242

def to_kbml(options={})

  @doc.xml options

end

#to_pdf(pdf_file = File.join(@savepath, 'sra' + Time.now.to_i.to_s, @title.gsub(/ /,'-') + '.pdf')) ⇒ Object

Writes the steps to a PDF file



351
352
353
354
355
356
357
358
359
360
361
# File 'lib/sra2019.rb', line 351

def to_pdf(pdf_file=File.join(@savepath, 'sra' + Time.now.to_i.to_s, 
                            @title.gsub(/ /,'-') + '.pdf'))
  
  dir = File.dirname(pdf_file)
  html_file = File.join(dir, 'index.html')
  
  to_html(dir)
  pdf = WickedPdf.new.pdf_from_html_file(html_file)
  File.write pdf_file, pdf
  
end

#to_slimlObject



306
307
308
# File 'lib/sra2019.rb', line 306

def to_sliml()
  @sliml
end

#to_srt(offset = 0) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/sra2019.rb', line 310

def to_srt(offset=0)

  lines = to_subtitles(offset).strip.lines.map.with_index do |x, i|

    raw_times, subtitle = x.split(/ /,2)
    
    start_time, end_time = raw_times.split('-',2)
    times = [("%02d:%02d:%02d" % ([0, 0 ] + start_time.split(/\D/)\
                                  .map(&:to_i)).reverse.take(3).reverse), \
             '-->', \
            ("%02d:%02d:%02d" % ([0, 0 ] + end_time.split(/\D/).map(&:to_i))\
             .reverse.take(3).reverse)].join(' ')

    [i+1, times, subtitle].join("\n")

  end

  lines.join("\n")    
  
end

#to_subtitles(offset = 0) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/sra2019.rb', line 331

def to_subtitles(offset=0)
  
  raw_times = @all_steps.map(&:time)
  raw_times << raw_times.last + 10

  times = raw_times.each_cons(2).map do |x|

    x.map do |sec|
      a = Subunit.new(units={minutes:60}, seconds: sec+offset).to_h.to_a
      a.map {|x|"%d%s" % [x[1], x[0][0]] }.join('')
    end.join('-')
    
  end
  
  times.zip(@all_steps.map(&:desc)).map {|x| x.join(' ')}.join("\n")
                        
end

#to_zipObject

Compresses the HTML file directory to a ZIP file



365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/sra2019.rb', line 365

def to_zip()
  
  project = 'sra' + Time.now.to_i.to_s
  newdir = File.join(@savepath, project)
  zipfile = newdir + '.zip'
  
  to_html(File.join(newdir))    
  #to_pdf(File.join(newdir, @title.gsub(/ /,'-') + '.pdf'))    

  Archive::Zip.archive(zipfile, newdir)
  
  zipfile
  
end