From df498c756b1cb0e3f045bc88396223272b713a4c Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Fri, 17 Feb 2023 12:14:21 +0600 Subject: Feat: add time check and test --- main.go | 41 +++++++++++++++++++++++++++++-------- main_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ workers.go | 13 ++++++------ 3 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 main_test.go diff --git a/main.go b/main.go index 81d4a6a..5f738aa 100644 --- a/main.go +++ b/main.go @@ -76,8 +76,7 @@ func linesToUtterances(lines []string, fd *FileData) []*Utterance { u.OutPath = fmt.Sprintf("%s/%s_%s_%s.opus", outdir, fd.AudioBase, u.LeftTime, u.RightTime) - // todo: compare and filter time - if u.LeftTime == u.RightTime { + if !adequateTimes(u.LeftTime, u.RightTime) { continue } resp = append(resp, u) @@ -86,6 +85,27 @@ func linesToUtterances(lines []string, fd *FileData) []*Utterance { 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 buildFFmpegCall(ut *Utterance) string { return fmt.Sprintf( `yes no | ffmpeg -i "%s" -ss %s -to %s \ @@ -163,11 +183,11 @@ func main() { panic(err) } - // filteredUtterances := []*Utterance{} + filteredUtterances := []*Utterance{} // === concurrent === queue := make(chan *Utterance, len(utterances)) - done := make(chan bool, 1) + geshaft := make(chan *Utterance, len(utterances)) for _, ut := range utterances { if _, err := os.Stat(ut.OutPath); os.IsNotExist(err) { @@ -175,6 +195,8 @@ func main() { // if err := cutoutClipAndTranscode(ut); err == nil { // filteredUtterances = append(filteredUtterances, ut) // } + } else if err == nil { + geshaft <- ut } } @@ -182,20 +204,23 @@ func main() { workers := 100 for i := 0; i < workers; i++ { - go worker(queue, i, done) + go worker(queue, i, geshaft) } for { if len(queue) == 0 { // give time for workers to finish time.Sleep(3 * time.Second) - done <- true - close(done) close(queue) break } time.Sleep(1 * time.Second) } - newMeta := utterancesToFileTextMap(utterances) + + for ut := range geshaft { + filteredUtterances = append(filteredUtterances, ut) + } + close(geshaft) + newMeta := utterancesToFileTextMap(filteredUtterances) writeCSV(mapToCSV(newMeta)) } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..e494dc6 --- /dev/null +++ b/main_test.go @@ -0,0 +1,67 @@ +package main + +import ( + "fmt" + "testing" +) + +func TestFullyIncludes(t *testing.T) { + cases := []struct { + LeftTime string + RightTime string + Want bool + Description string + }{ + { + LeftTime: "06:12.240", + RightTime: "06:16.040", + Want: true, + Description: "right time is bigger", + }, + { + LeftTime: "06:18.240", + RightTime: "06:16.040", + Want: false, + Description: "left time is bigger: inadequate", + }, + { + LeftTime: "06:18.240", + RightTime: "06:18.240", + Want: false, + Description: "left == right: inadequate", + }, + { + LeftTime: "01:00:08.000", + RightTime: "59:37.800", + Want: false, + Description: "with hours: left time is bigger: inadequate", + }, + { + LeftTime: "59:37.800", + RightTime: "01:00:08.000", + Want: true, + Description: "with hours: right time is bigger", + }, + { + LeftTime: "01:00:04.900", + RightTime: "01:02:08.000", + Want: true, + Description: "with hours: right time is bigger", + }, + { + LeftTime: "01:02:08.000", + RightTime: "01:00:04.900", + Want: false, + Description: "with hours: left time is bigger", + }, + } + for i, tc := range cases { + t.Run(fmt.Sprintf("run: #%d; %q", i, tc.Description), func(t *testing.T) { + got := adequateTimes(tc.LeftTime, tc.RightTime) + if got != tc.Want { + t.Errorf("want: %v; got: %v", tc.Want, got) + } + }) + } + +} diff --git a/workers.go b/workers.go index bbd5223..6fa4835 100644 --- a/workers.go +++ b/workers.go @@ -2,14 +2,15 @@ package main import "fmt" -func worker(queue chan *Utterance, worknumber int, done chan bool) { +func worker(queue chan *Utterance, worknumber int, geshaft chan *Utterance) { for { - select { - case ut := <-queue: - cutoutClipAndTranscode(ut) - case <-done: - fmt.Println("worker stoped, number", worknumber) + if len(queue) == 0 { + fmt.Println("empty queue, number", worknumber) return } + ut := <-queue + if err := cutoutClipAndTranscode(ut); err == nil { + geshaft <- ut + } } } -- cgit v1.2.3