-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (65 loc) · 1.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmd
import (
"flag"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"sync"
core "github.com/aminekaabachi/blindtest-crop/core"
"github.com/h2non/filetype"
)
var (
seconds = flag.Int64("seconds", 30, "Number of seconds to crop.")
inputPath = flag.String("input-path", "", "MP3 songs input folder path.")
outputPath = flag.String("output-path", "./output", "MP3 croped songs output path.")
)
func validateInput() (fs.FileInfo, []fs.FileInfo) {
input, err := os.Stat(*inputPath)
if os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "The provided input path '%s' is not valid or does not exist.\n", *inputPath)
os.Exit(1)
}
files, err := ioutil.ReadDir(*inputPath)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
return input, files
}
func validateOutput() fs.FileInfo {
output, err := os.Stat(*outputPath)
if os.IsNotExist(err) {
log.Printf("Output path '%s' doesn't exists.\n", *outputPath)
os.Mkdir(*outputPath, os.ModePerm)
log.Printf("Created the following output path: '%s'.\n", *outputPath)
output, _ = os.Stat(*outputPath)
}
return output
}
func Main() {
flag.Parse()
ex, _ := os.Executable()
base := filepath.Dir(ex)
if *inputPath == "" {
flag.PrintDefaults()
os.Exit(1)
}
input, files := validateInput()
log.Printf("Started cropping all MP3 inside the path '%s'.\n", input.Name())
validateOutput()
var wg sync.WaitGroup
for _, f := range files {
inputPath := path.Join(base, *inputPath, f.Name())
buf, _ := ioutil.ReadFile(inputPath)
outputPath := path.Join(base, *outputPath, f.Name())
if filetype.Is(buf, "mp3") {
wg.Add(1)
go core.Crop(&wg, inputPath, outputPath, *seconds)
wg.Wait()
}
}
}