main.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // takes a filename (and count of flines in it, cause i'm lazy)
  2. // converts each line in it with ffmpeg, balanced onto the number of cores in the system
  3. package main
  4. import (
  5. "bufio"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "github.com/cheggaaa/pb"
  14. "github.com/cryptix/go/logging"
  15. )
  16. var log = logging.Logger("convert")
  17. func main() {
  18. if len(os.Args) != 3 {
  19. log.Fatal("Usage: convert <files.lst> <count>")
  20. }
  21. inputf, err := os.Open(os.Args[1])
  22. logging.CheckFatal(err)
  23. defer inputf.Close()
  24. var wg sync.WaitGroup
  25. jobs := make(chan string)
  26. for i := 0; i < runtime.NumCPU(); i++ {
  27. wg.Add(1)
  28. go worker(jobs, &wg)
  29. }
  30. wmaScanner := bufio.NewScanner(inputf)
  31. total, err := strconv.Atoi(os.Args[2])
  32. logging.CheckFatal(err)
  33. bar := pb.StartNew(total)
  34. for wmaScanner.Scan() {
  35. fname := wmaScanner.Text()
  36. if !strings.Contains(fname, "/wav/") {
  37. jobs <- fname
  38. bar.Increment()
  39. }
  40. }
  41. logging.CheckFatal(wmaScanner.Err())
  42. close(jobs)
  43. wg.Wait()
  44. bar.Finish()
  45. }
  46. func worker(jobs <-chan string, wg *sync.WaitGroup) {
  47. for job := range jobs {
  48. dir, file := filepath.Split(job)
  49. os.Mkdir(filepath.Join(dir, "wav"), 0700)
  50. // log.Info("converting", file)
  51. cmd := exec.Command("ffmpeg", "-y", "-i", file, "-acodec", "aac", "-strict", "-2", "-b:a", "192k", strings.Replace(file, ".wav", ".m4a", 1))
  52. cmd.Dir = dir
  53. out, err := cmd.CombinedOutput()
  54. if err != nil {
  55. log.Error(string(out))
  56. log.Fatal(err)
  57. }
  58. logging.CheckFatal(os.Rename(job, filepath.Join(dir, "wav", file)))
  59. }
  60. wg.Done()
  61. }