From aedca76587982d5e1c9f5042eb7b3fb138667784 Mon Sep 17 00:00:00 2001 From: Colin P. Mccabe Date: Wed, 19 Dec 2018 16:10:09 -0800 Subject: [PATCH] Some improvements to audiobooker --- Makefile | 7 +++++-- audiobooker.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b6bab15..a5aeef5 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ CFLAGS=-Wall -O2 -all: bytor errno_speak show_default_sockopts simple_time vimstart hexconv +all: audiobooker bytor errno_speak show_default_sockopts simple_time vimstart hexconv + +audiobooker: + go build audiobooker.go bytor: go build bytor.go @@ -16,4 +19,4 @@ vimstart: vimstart.o hexconv: hexconv.o clean: - rm -rf errno_speak show_default_sockopts simple_time vimstart hexconv *.o + rm -rf bytor errno_speak show_default_sockopts simple_time vimstart hexconv *.o diff --git a/audiobooker.go b/audiobooker.go index c330c89..7116e3f 100644 --- a/audiobooker.go +++ b/audiobooker.go @@ -7,10 +7,12 @@ import ( "path/filepath" "io/ioutil" "fmt" + "math" "os" "sort" "path" "strings" + "strconv" ) const MP3_SUFFIX = ".mp3" @@ -120,6 +122,42 @@ func (out *OutDir) CopyFile(src string) error { return err } +func (out *OutDir) GetNumDigitsNeeded() int { + i := math.Log10(float64(out.nextIndex)) + j := math.Floor(i) + if j < i { + return 1 + int(j) + } else { + return int(j) + } +} + +func (out *OutDir) RenameOutputFiles() error { + numDigitsNeeded := out.GetNumDigitsNeeded() + fs := "%0" + strconv.Itoa(numDigitsNeeded) + "d" + MP3_SUFFIX + d, err := os.Open(out.outPath); if err != nil { + return fmt.Errorf("Unable to open directory %s: %s", out.outPath, err.Error()) + } + defer d.Close() + names, err := d.Readdirnames(0); if err != nil && err != io.EOF { + return fmt.Errorf("Unable to readdir %s: %s", out.outPath, err.Error()) + } + for i := range names { + index := -1 + _, err = fmt.Sscanf(names[i], "%d", &index); if err != nil { + return fmt.Errorf("Unable to parse file name %s", names[i]) + } + newName := fmt.Sprintf(fs, index) + src := path.Join(out.outPath, names[i]) + dst := path.Join(out.outPath, newName) + err = os.Rename(src, dst) + if err != nil { + return fmt.Errorf("Failed to rename %s to %s: %s", src, dst, err.Error()) + } + } + return nil +} + type InContext struct { inPath string // The original input file path. tempDir string // The path to the temporary directory. @@ -225,5 +263,10 @@ func main() { fmt.Printf("Split %s into %d file(s)\n", inPaths[i], len(paths)) totalFiles += int64(len(paths)) } + err = outDir.RenameOutputFiles() + if err != nil { + fmt.Printf("Error renaming output files in %s: %s", outDir.outPath, err.Error()) + os.Exit(1) + } fmt.Printf("Processed %d total file(s)\n", totalFiles) } -- 1.6.6.rc1.39.g9a42