From f60be7df2d741b73863df63c8a275d1b9471db58 Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Sat, 25 Feb 2023 15:02:37 +0300 Subject: Feat: add simple slice functionality --- ffmpeg.go | 14 ++++++++++++++ main.go | 36 +++++++++++++++++++++++++++++++++--- workers.go | 12 +++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/ffmpeg.go b/ffmpeg.go index e36c05b..7c5b6fd 100644 --- a/ffmpeg.go +++ b/ffmpeg.go @@ -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 +} diff --git a/main.go b/main.go index aa90be3..fe7ac06 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/workers.go b/workers.go index 03c9072..91f9eaf 100644 --- a/workers.go +++ b/workers.go @@ -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) + } +} -- cgit v1.2.3