-
Notifications
You must be signed in to change notification settings - Fork 2
/
trim.rb
81 lines (66 loc) · 1.77 KB
/
trim.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# trims the resulting wav file to talk start and end
#
module Fidelity
module Strategy
class Trim < Base
class << self
def required_executables
%w( sox ffmpeg )
end
end
def input
"#{name}.wav"
end
def backup
"#{name}-untrimmed.wav"
end
def make_backup_cmd
"mv #{input} #{backup}"
end
def file_start
manifest.fragments.map { |f| f.match(/\d+/)[0] }.sort.first.to_i
end
def audit
[ ['Start Recording', file_start, fdt(file_start)],
['Start Signal', manifest.talk_start, fdt(manifest.talk_start)],
['Start Offset (Trim)', start, 'seconds'],
['Stop Signal', manifest.talk_stop, fdt(manifest.talk_stop)],
['Time between Signals', duration, 'seconds'],
['Recording Length', dur(backup), 'seconds']
].each do |info|
puts ' %-20s % 20s % 30s' % info
end
end
def run
make_backup
audit # for debugging only
trim
input
end
def trim_cmd
"sox -V1 #{backup} #{input} trim #{start} #{duration}"
end
# start may never return a negative value
def start
[ manifest.talk_start - file_start, 0 ].max
end
def duration
manifest.talk_stop - manifest.talk_start
end
def outputs
[ input, backup ]
end
private
# format date time
def fdt(timestamp)
DateTime.strptime(timestamp.to_s,'%s')
end
def dur(file)
line = %x[ ffmpeg -i #{file} 2>&1 | grep Duration ]
return 0 if line.empty?
_, h, m, s = line.match(/(\d\d):(\d\d):(\d\d)/).to_a.map { |c| c.to_i }
(h * 60 + m) * 60 + s
end
end
end
end