-
Notifications
You must be signed in to change notification settings - Fork 0
/
tia-audio.go
171 lines (138 loc) · 3.78 KB
/
tia-audio.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"os"
"fmt"
"io"
"log"
"strings"
"net/http"
"path"
"path/filepath"
"github.com/alexflint/go-arg"
"github.com/jamlib/libaudio/ffmpeg"
"github.com/jamlib/tia-audio/details"
"github.com/jamlib/tia-audio/utils"
)
var ffm ffmpeg.Ffmpeger
// go-args: define app args
type args struct {
Quality string `help:"mp3 quality: 320, V0"`
Dir string `help:"directory where files will be saved"`
Url string `arg:"positional,required" help:"archive.org details url"`
}
// go-args: print app description
func (args) Description() string {
return "\nThe Internet Archive (archive.org) lossless audio downloader, " +
"transcoder and tagger"
}
// go-args: print app version
func (args) Version() string {
return "tia-audio 0.0.4\n"
}
// download file
func download(fileUrl, outPath string) string {
if len(fileUrl) == 0 {
return ""
}
a := strings.Split(fileUrl, "/")
filePath := a[len(a)-1]
fmt.Printf("\nDownloading: %s...\n", filePath)
res, e := http.Get(fileUrl)
if e != nil {
log.Fatal(e)
}
defer res.Body.Close()
file, err := os.Create(path.Join(outPath, filePath))
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = io.Copy(file, res.Body)
if err != nil {
log.Fatal(err)
}
return filePath
}
// download & optimize album artwork
func albumArtwork(imgUrl, outPath string, meta *ffmpeg.Metadata) {
fileName := download(imgUrl, outPath)
if len(fileName) > 0 {
outFile := path.Join(outPath, "folder.jpg")
_, err := ffm.OptimizeAlbumArt(path.Join(outPath, fileName), outFile)
if err != nil {
log.Fatal(err)
}
fmt.Println("Album Art:", outFile)
meta.Artwork = outFile
}
}
// process archive.org details
func process(d *details.Details, args *args) {
meta := ffmpeg.Metadata{
Artist: d.Artist,
Date: d.Date,
// use periods instead of dashes, ie 2018.01.01
Album: fmt.Sprintf("%s %s, %s",
strings.Replace(d.Date, "-", ".", -1), d.Venue, d.Location),
}
fmt.Println("Aritst:", meta.Artist)
fmt.Println("Album:", meta.Album)
// create directory
outPath := path.Join(args.Dir, d.Artist + "/" + d.Date[:4] + "/" + meta.Album)
os.MkdirAll(outPath, 0775)
albumArtwork(d.Artwork, outPath, &meta)
for i := range d.Tracks {
meta.Track = d.Tracks[i].Num
meta.Title = d.Tracks[i].Title
fmt.Println("\nTrack:", meta.Track)
fmt.Println("Title:", meta.Title)
downloadUrl := strings.Replace(d.Url, "/details/", "/download/", -1)
download(downloadUrl + "/" + d.Tracks[i].Source, outPath)
fmt.Printf("Converting '%s' to '%s' mp3...\n", d.Tracks[i].Source, args.Quality)
inFile := path.Join(outPath, d.Tracks[i].Source)
outFile:= path.Join(outPath, utils.SafeFilename(
meta.Track + " - " + meta.Title + ".mp3"))
c := &ffmpeg.Mp3Config{ inFile, args.Quality, outFile, meta, false }
_, err := ffm.ToMp3(c)
if err != nil {
log.Fatal(err)
}
os.Remove(inFile)
}
fmt.Println("\nProcess Completed!\n")
}
// package main entry
func main() {
var args args
arg.MustParse(&args)
// check url
if err := details.InvalidUrl(args.Url); err != nil {
log.Fatal(err)
}
var err error
ffm, err = ffmpeg.New()
if err != nil {
log.Fatal(err)
}
// check output directory
dirStat, err := os.Stat(args.Dir)
if err != nil || !dirStat.IsDir() {
// default to working directory
dir, err := filepath.Abs("./")
if err != nil {
log.Fatal(err)
}
args.Dir = dir
}
// set default mp3 quality
if args.Quality != "V0" && args.Quality != "320" {
args.Quality = "320"
}
fmt.Printf("\nProcessing URL: %s...\n\n", args.Url)
d, err := details.ProcessUrl(args.Url)
if err != nil {
// debug Details{}
log.Fatalf("%s\n\n%#v\n\n", err.Error(), d)
}
process(d, &args)
}