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
82
83
84
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
package main
import (
"flag"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
)
const (
subExt = ".vtt"
outdir = "data/utterances"
ffCmdOut = "data/ff_commands"
timeSep = "-->"
metadataPath = "data/metadata.json"
metadataPathCSV = "data/metadata.tsv"
)
type Utterance struct {
LeftTime string
RightTime string
Text string
OutPath string
FD *FileData
}
type FileData struct {
VttPath string
AudioPath string
AudioBase string
}
func NewFileData(vttPath string) *FileData {
fd := &FileData{
VttPath: vttPath,
AudioPath: strings.Trim(vttPath, subExt),
}
fd.AudioBase = path.Base(fd.AudioPath)
return fd
}
func keysToSlice(req map[string]struct{}) []string {
resp := []string{}
for k := range req {
resp = append(resp, k)
}
return resp
}
func mapToCSV(req map[string]string) [][]string {
resp := [][]string{}
for k, v := range req {
resp = append(resp, []string{k, v})
}
return resp
}
func linesToUtterances(lines []string, fd *FileData) []*Utterance {
resp := []*Utterance{}
for i, line := range lines {
if !strings.Contains(line, timeSep) {
continue
}
// get times
splitted := strings.Split(line, timeSep)
u := &Utterance{
Text: lines[i+1],
LeftTime: strings.TrimSpace(splitted[0]),
RightTime: strings.TrimSpace(splitted[1]),
FD: fd,
}
u.OutPath = fmt.Sprintf("%s/%s_%s_%s.wav", outdir, fd.AudioBase,
u.LeftTime, u.RightTime)
// todo: compare and filter time
if u.LeftTime == u.RightTime {
continue
}
resp = append(resp, u)
}
return resp
}
func buildFFmpegCall(ut *Utterance) string {
return fmt.Sprintf(
`yes no | ffmpeg -i "%s" -ss %s -to %s \
-metadata text_source="%s" \
-ar 22050 "%s"`,
ut.FD.AudioPath, ut.LeftTime, ut.RightTime,
ut.FD.VttPath, ut.OutPath,
)
}
func utterancesToFileTextMap(utterances []*Utterance) map[string]string {
resp := make(map[string]string)
for _, ut := range utterances {
resp[path.Base(ut.OutPath)] = ut.Text
}
return resp
}
func oneFileRun(filepath string) []*Utterance {
fd := NewFileData(filepath)
lines := readLines(fd.VttPath)
utterances := linesToUtterances(lines, fd)
return utterances
}
func dirRun(dirpath string) []*Utterance {
resp := []*Utterance{}
vttFiles := getVttList(dirpath)
for _, vtt := range vttFiles {
utterances := oneFileRun(vtt)
resp = append(resp, utterances...)
}
return resp
}
func getVttList(dirpath string) []string {
resp := []string{}
err := filepath.Walk(dirpath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if strings.Contains(info.Name(), subExt) {
resp = append(resp, path)
}
return nil
})
if err != nil {
fmt.Println(err)
}
return resp
}
func main() {
vttFilepath := flag.String("f", "", "path to a vtt file")
vttDir := flag.String("d", "", "path to a vtt dir")
flag.Parse()
utterances := []*Utterance{}
if vttDir != nil && *vttDir != "" {
utterances = dirRun(*vttDir)
} else if vttFilepath != nil && *vttFilepath != "" {
utterances = oneFileRun(*vttFilepath)
} else {
fmt.Println("no flags provided;")
return
}
fmt.Println("sum of utterances:", len(utterances))
if err := os.MkdirAll(outdir, 0755); err != nil {
panic(err)
}
// filteredUtterances := []*Utterance{}
// === concurrent ===
queue := make(chan *Utterance)
done := make(chan bool, 1)
for _, ut := range utterances {
if _, err := os.Stat(ut.OutPath); os.IsNotExist(err) {
queue <- ut
// if err := cutoutClipAndTranscode(ut); err == nil {
// filteredUtterances = append(filteredUtterances, ut)
// }
}
}
fmt.Println("filled queue, starting workers")
workers := 10
for i := 0; i < workers; i++ {
go worker(queue, i, done)
}
for {
if len(queue) == 0 {
done <- true
close(done)
close(queue)
break
}
time.Sleep(1 * time.Second)
}
newMeta := utterancesToFileTextMap(utterances)
writeCSV(mapToCSV(newMeta))
}
|