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.



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
123
124
125
126
127
128
# File 'lib/sra2019.rb', line 91

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.



87
88
89
# File 'lib/sra2019.rb', line 87

def duration
  @duration
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



87
88
89
# File 'lib/sra2019.rb', line 87

def start_time
  @start_time
end

#stepsObject

Returns the value of attribute steps.



88
89
90
# File 'lib/sra2019.rb', line 88

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 mp4 in avi out



133
134
135
136
137
138
139
140
141
# File 'lib/sra2019.rb', line 133

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} -y`
  end
  
end

#add_subtitles(source, destination) ⇒ Object

mp4 in mp4 out



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/sra2019.rb', line 145

def add_subtitles(source, destination)
  
  
  subtitles = File.join(@working_dir, 's.srt')
  File.write subtitles, to_srt()
  
  if block_given? then
    yield(source, subtitles, destination)
  else
    `ffmpeg -i #{source} -i #{subtitles} -c copy -c:s mov_text #{destination} -y`
  end
  
end

#build_video(source, destination) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/sra2019.rb', line 159

def build_video(source, destination)
  
  dir = File.dirname(source)
  file = File.basename(source)
  
  tidy!
  
  vid2 = File.join(dir, file.sub(/\.mp4$/,'b\0'))
  trim_video source, vid2
  
  vid3 = File.join(dir, file.sub(/\.mp4$/,'c.avi'))

  generate_audio
  add_audio_track File.join(@working_dir, 'audio.wav'), vid2, vid3

  
  vid4 = File.join(dir, file.sub(/\.avi$/,'d\0'))
  resize_video vid3, vid4
  
  vid5 = File.join(dir, file.sub(/\.mp4$/,'e\0'))
  transcode_video(vid4, vid5)
  add_subtitles(vid5, destination)    
  
end

#generate_audio(wav: true) ⇒ Object



185
186
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/sra2019.rb', line 185

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 }
    
    if @debug then
      puts ('x.duration: ' + x.duration.inspect).debug
      puts ('x.audio_duration: ' + x.audio_duration.inspect).debug
    end
    
    duration = x.duration - x.audio_duration
    x.silence_duration = duration >= 0 ? duration : 0
    
    if wav then
      
      silent_file = File.join(@working_dir, "silence#{(i+1).to_s}.wav")
      puts 'x.silence_duration: ' + x.silence_duration.inspect if @debug
      wav_silence silent_file, duration: x.silence_duration        
      ogg_to_wav File.join(@working_dir, "voice#{i+1}.ogg")            
      
    end
    
    sleep 0.02
    
  end
  
  if wav then
    
    intro = File.join(@working_dir, 'intro.wav')
    wav_silence intro
    
    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
    
    files.prepend intro
    
    wav_concat files, File.join(@working_dir, 'audio.wav')
  end
  
end

#import(s) ⇒ Object



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

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

#remove_steps(a) ⇒ Object



247
248
249
250
# File 'lib/sra2019.rb', line 247

def remove_steps(a)
  a.each {|n| @steps[n] = nil }
  @steps.compact!    
end

#resize_video(source, destination) ⇒ Object

avi in avi out



253
254
255
# File 'lib/sra2019.rb', line 253

def resize_video(source, destination)
  `ffmpeg -i #{source} -vf scale="720:-1" #{destination} -y`
end

#tidy!Object



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
# File 'lib/sra2019.rb', line 257

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



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/sra2019.rb', line 317

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



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/sra2019.rb', line 342

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



333
334
335
336
337
# File 'lib/sra2019.rb', line 333

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



442
443
444
445
446
447
448
449
450
451
452
# File 'lib/sra2019.rb', line 442

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



397
398
399
# File 'lib/sra2019.rb', line 397

def to_sliml()
  @sliml
end

#to_srt(offset = - 2))) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/sra2019.rb', line 401

def to_srt(offset=-(@steps.first.time - 2))

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

    raw_times, subtitle = x.split(/ /,2)
    puts ('raw_times: ' + raw_times.inspect).debug if @debug
    start_time, end_time = raw_times.split('-',2)
    times = [("%02d:%02d:%02d,000" % ([0, 0 ] + start_time.split(/\D/)\
                                  .map(&:to_i)).reverse.take(3).reverse), \
             '-->', \
            ("%02d:%02d:%02d,000" % ([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 = - 2))) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/sra2019.rb', line 422

def to_subtitles(offset=-(@steps.first.time - 2))
  
  raw_times = @steps.map {|x| [x.time, x.time + x.audio_duration + 1]} 
  

  times = raw_times.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(@steps.map(&:desc)).map {|x| x.join(' ')}.join("\n")
                        
end

#to_zipObject

Compresses the HTML file directory to a ZIP file



456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/sra2019.rb', line 456

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

#transcode_video(avi, mp4) ⇒ Object



294
295
296
297
298
299
300
301
302
# File 'lib/sra2019.rb', line 294

def transcode_video(avi, mp4)
  
  if block_given? then
    yield(avi, mp4)
  else
    `ffmpeg -i #{avi} #{mp4} -y`
  end
  
end

#trim_video(video, newvideo) ⇒ Object



304
305
306
307
308
309
310
311
312
313
# File 'lib/sra2019.rb', line 304

def trim_video(video, newvideo)
  
  start = @steps.first.time - 4
  t1, t2 = [start, @steps.last.time - 2 ].map do |step|
    "%02d:%02d:%02d" % (step.to_hms.reverse + [0,0]).take(3).reverse
  end
  
  `ffmpeg -i #{video} -ss #{t1} -t #{t2} -async 1 #{newvideo} -y`
  
end