-
Notifications
You must be signed in to change notification settings - Fork 17
/
expoter.go
89 lines (73 loc) · 1.63 KB
/
expoter.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
package godub
import (
"io"
"os"
"bytes"
"github.com/iFaceless/godub/converter"
"github.com/iFaceless/godub/wav"
)
type Exporter struct {
converter *converter.Converter
dst interface{}
}
func NewExporter(dst interface{}) *Exporter {
return &Exporter{converter: converter.NewConverter(nil), dst: dst}
}
func (e *Exporter) Export(segment *AudioSegment) error {
wavBuf := bytes.Buffer{}
err := wav.Encode(&wavBuf, segment.AsWaveAudio())
if err != nil {
return err
}
var w io.Writer
switch dst := e.dst.(type) {
case io.Writer:
w = dst
case string:
f, err := os.Create(dst)
if err != nil {
return err
}
w = f
defer f.Close()
}
if e.converter.DstFormat() == "wav" {
_, err := io.Copy(w, &wavBuf)
return err
} else {
// Otherwise, convert it to the dst format using ffmpeg.
return e.converter.WithWriter(w).Convert(&wavBuf)
}
}
func (e *Exporter) WithCodec(c string) *Exporter {
e.converter.WithCodec(c)
return e
}
func (e *Exporter) WithCover(c string) *Exporter {
e.converter.WithCover(c)
return e
}
func (e *Exporter) WithBitRate(rate int) *Exporter {
e.converter.WithBitRate(rate)
return e
}
func (e *Exporter) WithSampleRate(rate int) *Exporter {
e.converter.WithSampleRate(rate)
return e
}
func (e *Exporter) WithTags(tags map[string]string) *Exporter {
e.converter.WithTags(tags)
return e
}
func (e *Exporter) WithDstFormat(f string) *Exporter {
e.converter.WithDstFormat(f)
return e
}
func (e *Exporter) WithID3TagVersion(v int) *Exporter {
e.converter.WithID3TagVersion(v)
return e
}
func (e *Exporter) WithParams(p ...string) *Exporter {
e.converter.WithParams(p...)
return e
}