diff options
-rw-r--r-- | ffmpeg.go | 14 | ||||
-rw-r--r-- | main.go | 36 | ||||
-rw-r--r-- | workers.go | 12 |
3 files changed, 58 insertions, 4 deletions
@@ -18,3 +18,17 @@ func cutoutClipAndTranscode(ut *Utterance) error { }).OverWriteOutput().ErrorToStdOut().Run() return err } + +func cutOnEqualParts(filepath, segment string) error { + err := ffmpeg.Input(filepath). + Output(filepath+"%03d.opus", + ffmpeg.KwArgs{ + "c": "copy", + "map": 0, + "segment_time": segment, + "f": "segment", + "reset_timestamps": 1, + }). + OverWriteOutput().ErrorToStdOut().Run() + return err +} @@ -17,6 +17,7 @@ const ( timeSep = "-->" metadataPath = "data/metadata.json" metadataPathCSV = "data/metadata.tsv" + segmentSize = "00:08:00" ) type Utterance struct { @@ -135,7 +136,7 @@ func oneFileRun(filepath string) []*Utterance { func dirRun(dirpath string) []*Utterance { resp := []*Utterance{} - vttFiles := getVttList(dirpath) + vttFiles := getFileList(dirpath, subExt) for _, vtt := range vttFiles { utterances := oneFileRun(vtt) resp = append(resp, utterances...) @@ -143,7 +144,7 @@ func dirRun(dirpath string) []*Utterance { return resp } -func getVttList(dirpath string) []string { +func getFileList(dirpath string, filter string) []string { resp := []string{} err := filepath.Walk(dirpath, func(path string, info os.FileInfo, err error) error { @@ -151,7 +152,7 @@ func getVttList(dirpath string) []string { fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err) return err } - if strings.Contains(info.Name(), subExt) { + if strings.HasSuffix(info.Name(), filter) { resp = append(resp, path) } return nil @@ -162,9 +163,35 @@ func getVttList(dirpath string) []string { return resp } +func equalSlice(dirpath string) { + auFiles := getFileList(dirpath, "opus") + + fQueue := make(chan string, len(auFiles)) + for _, fpath := range auFiles { + fQueue <- fpath + } + + workers := 3 + 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") + sliceAudioDir := flag.String("slice-audio-dir", "", + "for equal segmentation only without subs") flag.Parse() utterances := []*Utterance{} @@ -172,6 +199,9 @@ func main() { utterances = dirRun(*vttDir) } else if vttFilepath != nil && *vttFilepath != "" { utterances = oneFileRun(*vttFilepath) + } else if sliceAudioDir != nil && *sliceAudioDir != "" { + equalSlice(*sliceAudioDir) + return } else { fmt.Println("no flags provided;") return @@ -9,7 +9,6 @@ func worker(queue chan *Utterance, done chan bool, worknumber int, geshaft chan return } select { - case ut := <-queue: if err := cutoutClipAndTranscode(ut); err == nil { geshaft <- ut @@ -21,3 +20,14 @@ func worker(queue chan *Utterance, done chan bool, worknumber int, geshaft chan } } } + +func cutterQueue(fQueue chan string, workerID int) { + for { + if len(fQueue) == 0 { + fmt.Println("empty queue, number", workerID) + return + } + fpath := <-fQueue + cutOnEqualParts(fpath, segmentSize) + } +} |