-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio.go
236 lines (203 loc) · 5.45 KB
/
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package aio
import (
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"regexp"
"syscall"
)
type Audio struct {
filename string // Audio Filename.
samplerate int // Audio Sample Rate in Hz.
channels int // Number of audio channels.
bitrate int // Bitrate for audio encoding.
duration float64 // Duration of audio in seconds.
format string // Format of audio samples.
codec string // Codec used for video encoding.
bps int // Bits per sample.
buffer []byte // Raw audio data.
pipe *io.ReadCloser // Stdout pipe for ffmpeg process.
cmd *exec.Cmd // ffmpeg command.
}
func (audio *Audio) FileName() string {
return audio.filename
}
// Audio Sample Rate in Hz.
func (audio *Audio) SampleRate() int {
return audio.samplerate
}
func (audio *Audio) Channels() int {
return audio.channels
}
func (audio *Audio) Bitrate() int {
return audio.bitrate
}
// Audio Duration in seconds.
func (audio *Audio) Duration() float64 {
return audio.duration
}
func (audio *Audio) Format() string {
switch audio.format {
case "u8", "s8":
return audio.format
default:
return audio.format[:len(audio.format)-2]
}
}
func (audio *Audio) Codec() string {
return audio.codec
}
func (audio *Audio) BitsPerSample() int {
return audio.bps
}
func (audio *Audio) Buffer() []byte {
return audio.buffer
}
func (audio *Audio) Samples() interface{} {
return convertBytesToSamples(audio.buffer, len(audio.buffer)/(audio.bps/8), audio.format)
}
// Sets the buffer to the given byte array. The length of the buffer must be a multiple
// of (bytes per sample * audio channels).
func (audio *Audio) SetBuffer(buffer []byte) error {
if len(buffer)%(audio.bps/8*audio.channels) != 0 {
return fmt.Errorf("buffer size must be multiple of %d", audio.bps/8*audio.channels)
}
audio.buffer = buffer
return nil
}
func NewAudio(filename string, options *Options) (*Audio, error) {
if !exists(filename) {
return nil, fmt.Errorf("video file %s does not exist", filename)
}
// Check if ffmpeg and ffprobe are installed on the users machine.
if err := checkExists("ffmpeg"); err != nil {
return nil, err
}
if err := checkExists("ffprobe"); err != nil {
return nil, err
}
audioData, err := ffprobe(filename, "a")
if err != nil {
return nil, err
}
if len(audioData) == 0 {
return nil, fmt.Errorf("no audio data found in %s", filename)
}
audio := &Audio{filename: filename}
if options == nil {
options = &Options{}
}
if options.Format == "" {
audio.format = createFormat("s16") // s16 default format.
} else {
audio.format = createFormat(options.Format)
}
if err := checkFormat(audio.format); err != nil {
return nil, err
}
bps := int(parse(regexp.MustCompile(`\d{1,2}`).FindString(audio.format))) // Bits per sample.
audio.bps = bps
audio.addAudioData(audioData)
if options.SampleRate != 0 {
audio.samplerate = options.SampleRate
}
if options.Channels != 0 {
audio.channels = options.Channels
}
return audio, nil
}
// Adds audio data to the Audio struct from the ffprobe output.
func (audio *Audio) addAudioData(data map[string]string) {
if samplerate, ok := data["sample_rate"]; ok {
audio.samplerate = int(parse(samplerate))
}
if channels, ok := data["channels"]; ok {
audio.channels = int(parse(channels))
}
if bitrate, ok := data["bit_rate"]; ok {
audio.bitrate = int(parse(bitrate))
}
if duration, ok := data["duration"]; ok {
audio.duration = float64(parse(duration))
}
if codec, ok := data["codec_name"]; ok {
audio.codec = codec
}
}
// Once the user calls Read() for the first time on a Audio struct,
// the ffmpeg command which is used to read the audio is started.
func (audio *Audio) init() error {
// If user exits with Ctrl+C, stop ffmpeg process.
audio.cleanup()
// ffmpeg command to pipe audio data to stdout.
cmd := exec.Command(
"ffmpeg",
"-i", audio.filename,
"-f", audio.format,
"-acodec", fmt.Sprintf("pcm_%s", audio.format),
"-ar", fmt.Sprintf("%d", audio.samplerate),
"-ac", fmt.Sprintf("%d", audio.channels),
"-loglevel", "quiet",
"-",
)
audio.cmd = cmd
pipe, err := cmd.StdoutPipe()
if err != nil {
return err
}
audio.pipe = &pipe
if err := cmd.Start(); err != nil {
return err
}
if audio.buffer == nil {
audio.buffer = make([]byte, audio.samplerate*audio.channels*audio.bps/8)
}
return nil
}
// Reads the next frame from of audio and stores it in the buffer.
// If the last frame has been read, returns false, otherwise true.
func (audio *Audio) Read() bool {
// If cmd is nil, video reading has not been initialized.
if audio.cmd == nil {
if err := audio.init(); err != nil {
return false
}
}
total := 0
for total < len(audio.buffer) {
n, err := (*audio.pipe).Read(audio.buffer[total:])
if err == io.EOF {
audio.Close()
return false
}
total += n
}
return true
}
// Closes the pipe and stops the ffmpeg process.
func (audio *Audio) Close() {
if audio.pipe != nil {
(*audio.pipe).Close()
}
if audio.cmd != nil {
audio.cmd.Wait()
}
}
// Stops the "cmd" process running when the user presses Ctrl+C.
// https://stackoverflow.com/questions/11268943/is-it-possible-to-capture-a-ctrlc-signal-and-run-a-cleanup-function-in-a-defe.
func (audio *Audio) cleanup() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if audio.pipe != nil {
(*audio.pipe).Close()
}
if audio.cmd != nil {
audio.cmd.Process.Kill()
}
os.Exit(1)
}()
}