summaryrefslogtreecommitdiff
path: root/main.go
blob: 4cfa197319bbc6dbcd645861b371824dd56b6513 (plain)
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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main

import (
	"crypto/md5"
	"flag"
	"fmt"
	"os"
	"path"
	"path/filepath"
	"strings"
	"time"
)

const (
	subExt          = ".vtt"
	ffCmdOut        = "data/ff_commands"
	timeSep         = "-->"
	metadataPath    = "data/metadata.json"
	metadataPathCSV = "data/metadata.tsv"
	segmentSize     = "00:08:00"
)

var (
	outdir = "/mnt/nvme/data"
)

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 hashStr(req string) string {
	return fmt.Sprintf("%x", md5.Sum([]byte(req)))
}

func fpathToOutname(req string) string {
	basename := filepath.Base(req)
	h := hashStr(basename)
	return path.Join(outdir, h+".opus")
}

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.opus", outdir, fd.AudioBase,
			u.LeftTime, u.RightTime)

		if !adequateTimes(u.LeftTime, u.RightTime) {
			continue
		}
		resp = append(resp, u)

	}
	return resp
}

func adequateTimes(left, right string) bool {
	if len(left) == len("06:18.240") {
		left = "00:" + left
	}
	if len(right) == len("06:18.240") {
		right = "00:" + right
	}
	lts, err := time.Parse("15:04:05.000", left)
	if err != nil {
		fmt.Println(left, right, err)
	}
	rts, err := time.Parse("15:04:05.000", right)
	if err != nil {
		fmt.Println(left, right, err)
	}
	if rts.After(lts) {
		return true
	}
	return false
}

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 := getFileList(dirpath, subExt)
	for _, vtt := range vttFiles {
		utterances := oneFileRun(vtt)
		resp = append(resp, utterances...)
	}
	return resp
}

func getFileList(dirpath string, filter 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.HasSuffix(info.Name(), filter) {
				resp = append(resp, path)
			}
			return nil
		})
	if err != nil {
		fmt.Println(err)
	}
	return resp
}

func equalSliceRun(dirpath string) {
	auFiles := getFileList(dirpath, "opus")

	fQueue := make(chan string, len(auFiles))
	for _, fpath := range auFiles {
		fQueue <- fpath
	}

	workers := 20
	for i := 0; i < workers; i++ {
		go cutterQueue(fQueue, i)
	}

	for {
		if len(fQueue) == 0 {
			fmt.Println("empty queue: work is done")
			break
		}
		time.Sleep(1 * time.Second)
	}
	close(fQueue)
	fmt.Println("queue closed")
}

func main() {
	vttFilepath := flag.String("f", "", "path to a vtt file")
	vttDir := flag.String("d", "", "path to a vtt dir")
	outDirArg := flag.String("o", "", "output dir")
	sliceAudioDir := flag.String("slice-audio-dir", "",
		"for equal segmentation only without subs")
	flag.Parse()

	if outDirArg != nil && *outDirArg != "" {
		outdir = *outDirArg
	}

	utterances := []*Utterance{}
	if vttDir != nil && *vttDir != "" {
		utterances = dirRun(*vttDir)
	} else if vttFilepath != nil && *vttFilepath != "" {
		utterances = oneFileRun(*vttFilepath)
	} else if sliceAudioDir != nil && *sliceAudioDir != "" {
		equalSliceRun(*sliceAudioDir)
		return
	} 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, len(utterances))
	geshaft := make(chan *Utterance, len(utterances))
	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)
			// }
		} else if err == nil {
			geshaft <- ut
		}
	}

	fmt.Println("filled queue, starting workers")

	workers := 100
	for i := 0; i < workers; i++ {
		go worker(queue, done, i, geshaft)
	}

	for {
		if len(queue) == 0 {
			fmt.Println("empty queue: sending true to done")
			done <- true
			// give time for workers to finish
			fmt.Println("sleeping for few seconds")
			time.Sleep(5 * time.Second)
			fmt.Println("closing queue")
			close(queue)
			fmt.Println("breaking from the loop")
			break
		}
		time.Sleep(1 * time.Second)
	}

	close(geshaft)
	for ut := range geshaft {
		filteredUtterances = append(filteredUtterances, ut)
	}
	newMeta := utterancesToFileTextMap(filteredUtterances)
	writeCSV(mapToCSV(newMeta))
}