diff options
author | Grail Finder <wohilas@gmail.com> | 2023-02-16 16:20:56 +0600 |
---|---|---|
committer | Grail Finder <wohilas@gmail.com> | 2023-02-16 16:20:56 +0600 |
commit | f7fd6d7e5877f96c605137ae185626ac6f85eebc (patch) | |
tree | b60ada208fd085fdb9789cfcb4ae74de1c384fc8 | |
parent | 84a3dd05d1623d61e694d3f763c3121be3b9b43c (diff) |
Refactor: move io funcitons in separate file
-rw-r--r-- | io_helpers.go | 82 | ||||
-rw-r--r-- | main.go | 81 | ||||
-rw-r--r-- | readme.md | 1 |
3 files changed, 85 insertions, 79 deletions
diff --git a/io_helpers.go b/io_helpers.go new file mode 100644 index 0000000..3bf9600 --- /dev/null +++ b/io_helpers.go @@ -0,0 +1,82 @@ +package main + +import ( + "bufio" + "encoding/csv" + "encoding/json" + "fmt" + "io/ioutil" + "os" +) + +func readLines(filepath string) []string { + file, err := os.Open(filepath) + if err != nil { + panic(err) + } + defer file.Close() + + resp := []string{} + scanner := bufio.NewScanner(file) + for scanner.Scan() { + resp = append(resp, scanner.Text()) + } + + if err := scanner.Err(); err != nil { + panic(err) + } + return resp +} + +// writeLines writes the lines to the given file. +func writeLines(lines []string, path string) error { + file, err := os.Create(path) + if err != nil { + return err + } + defer file.Close() + + w := bufio.NewWriter(file) + for _, line := range lines { + fmt.Fprintln(w, line) + } + return w.Flush() +} + +func readJson(filepath string) map[string]string { + data := make(map[string]string) + plan, err := ioutil.ReadFile(filepath) + if err != nil { + return data + } + err = json.Unmarshal(plan, &data) + if err != nil { + panic(err) + } + return data +} + +func writeJson(data map[string]string) { + metadataJson, _ := json.MarshalIndent(data, "", " ") + err := ioutil.WriteFile(metadataPath, metadataJson, 0644) + if err != nil { + panic(err) + } +} + +func writeCSV(data [][]string) { + f, err := os.Create(metadataPathCSV) + defer f.Close() + + if err != nil { + panic(err) + } + + w := csv.NewWriter(f) + w.Comma = '\t' + defer w.Flush() + + if err := w.WriteAll(data); err != nil { + panic(err) + } +} @@ -1,18 +1,15 @@ package main import ( - "bufio" - "encoding/csv" - "encoding/json" "flag" "fmt" - "io/ioutil" "os" "path" "path/filepath" "strings" ) +// TODO: to config file const ( subExt = ".vtt" outdir = "data/utterances" @@ -76,7 +73,7 @@ func linesToUtterances(lines []string, fd *FileData) []*Utterance { RightTime: strings.TrimSpace(splitted[1]), FD: fd, } - u.OutPath = fmt.Sprintf("%s/%s_%s_%s.wav", outdir, fd.AudioBase, + u.OutPath = fmt.Sprintf("%s/%s_%s_%s.opus", outdir, fd.AudioBase, u.LeftTime, u.RightTime) if u.LeftTime == u.RightTime { @@ -88,80 +85,6 @@ func linesToUtterances(lines []string, fd *FileData) []*Utterance { return resp } -func readLines(filepath string) []string { - file, err := os.Open(filepath) - if err != nil { - panic(err) - } - defer file.Close() - - resp := []string{} - scanner := bufio.NewScanner(file) - for scanner.Scan() { - resp = append(resp, scanner.Text()) - } - - if err := scanner.Err(); err != nil { - panic(err) - } - return resp -} - -// writeLines writes the lines to the given file. -func writeLines(lines []string, path string) error { - file, err := os.Create(path) - // file, err := os.OpenFile(path, - // os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666) - if err != nil { - return err - } - defer file.Close() - - w := bufio.NewWriter(file) - for _, line := range lines { - fmt.Fprintln(w, line) - } - return w.Flush() -} - -func readJson(filepath string) map[string]string { - data := make(map[string]string) - plan, err := ioutil.ReadFile(filepath) - if err != nil { - return data - } - err = json.Unmarshal(plan, &data) - if err != nil { - panic(err) - } - return data -} - -func writeJson(data map[string]string) { - metadataJson, _ := json.MarshalIndent(data, "", " ") - err := ioutil.WriteFile(metadataPath, metadataJson, 0644) - if err != nil { - panic(err) - } -} - -func writeCSV(data [][]string) { - f, err := os.Create(metadataPathCSV) - defer f.Close() - - if err != nil { - panic(err) - } - - w := csv.NewWriter(f) - w.Comma = '\t' - defer w.Flush() - - if err := w.WriteAll(data); err != nil { - panic(err) - } -} - func buildFFmpegCall(ut *Utterance) string { return fmt.Sprintf( `yes no | ffmpeg -i "%s" -ss %s -to %s \ @@ -6,3 +6,4 @@ - add config file; move constance to config file; - ffmpeg call should be unique; - ffmpeg commands are slow, split to clips inside of this module; +- wav take a lot of space; no need for conversion, pytorch supports opus (convert to opus); |