-
Notifications
You must be signed in to change notification settings - Fork 1
/
audiowriter.go
215 lines (184 loc) · 5 KB
/
audiowriter.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
package aio
import (
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"syscall"
)
type AudioWriter struct {
filename string // Output filename.
streamfile string // Extra stream data filename.
samplerate int // Audio Sample Rate in Hz.
channels int // Number of audio channels.
bitrate int // Bitrate for audio encoding.
format string // Format of audio samples.
codec string // Codec used for video encoding.
pipe io.WriteCloser // Stdout pipe of ffmpeg process.
cmd *exec.Cmd // ffmpeg command.
}
func (writer *AudioWriter) FileName() string {
return writer.filename
}
// File used to fill in extra stream data.
func (writer *AudioWriter) StreamFile() string {
return writer.streamfile
}
// Audio Sample Rate in Hz.
func (writer *AudioWriter) SampleRate() int {
return writer.samplerate
}
func (writer *AudioWriter) Channels() int {
return writer.channels
}
// Audio Bitrate in bits/s.
func (writer *AudioWriter) Bitrate() int {
return writer.bitrate
}
func (writer *AudioWriter) Format() string {
switch writer.format {
case "u8", "s8":
return writer.format
default:
return writer.format[:len(writer.format)-2]
}
}
func (writer *AudioWriter) Codec() string {
return writer.codec
}
func NewAudioWriter(filename string, options *Options) (*AudioWriter, error) {
// Check if ffmpeg is installed on the users machine.
if err := installed("ffmpeg"); err != nil {
return nil, err
}
if options == nil {
options = &Options{}
}
writer := &AudioWriter{
filename: filename,
streamfile: options.StreamFile,
bitrate: options.Bitrate,
codec: options.Codec,
}
writer.samplerate = 44100 // 44100 Hz sampling rate by default.
if options.SampleRate != 0 {
writer.samplerate = options.SampleRate
}
writer.channels = 2 // Stereo by default.
if options.Channels != 0 {
writer.channels = options.Channels
}
if options.Format == "" {
writer.format = createFormat("s16") // s16 default format.
} else {
writer.format = createFormat(options.Format)
if err := checkFormat(writer.format); err != nil {
return nil, err
}
}
if options.StreamFile != "" {
if !exists(options.StreamFile) {
return nil, fmt.Errorf("file %s does not exist", options.StreamFile)
}
writer.streamfile = options.StreamFile
}
return writer, nil
}
// Once the user calls Write() for the first time on a AudioWriter struct,
// the ffmpeg command which is used to write to the audio file is started.
func (writer *AudioWriter) init() error {
// If user exits with Ctrl+C, stop ffmpeg process.
writer.cleanup()
// ffmpeg command to write to audio file. Takes in bytes from Stdin and encodes them.
command := []string{
"-y", // overwrite output file if it exists.
"-loglevel", "quiet",
"-f", writer.format,
"-ar", fmt.Sprintf("%d", writer.samplerate),
"-ac", fmt.Sprintf("%d", writer.channels),
"-i", "-", // The input comes from stdin.
}
// Assumes "writer.file" is a container format.
if writer.streamfile != "" {
command = append(
command,
"-i", writer.streamfile,
"-map", "0:a:0",
"-map", "1:v?", // Add Video streams if present.
"-c:v", "copy",
"-map", "1:s?", // Add Subtitle streams if present.
"-c:s", "copy",
"-map", "1:d?", // Add Data streams if present.
"-c:d", "copy",
"-map", "1:t?", // Add Attachments streams if present.
"-c:t", "copy",
"-shortest", // Cut longest streams to match audio duration.
)
}
if writer.codec != "" {
command = append(command, "-acodec", writer.codec)
}
if writer.bitrate > 0 {
command = append(command, "-ab", fmt.Sprintf("%d", writer.bitrate))
}
command = append(command, writer.filename)
cmd := exec.Command("ffmpeg", command...)
writer.cmd = cmd
pipe, err := cmd.StdinPipe()
if err != nil {
return err
}
writer.pipe = pipe
if err := cmd.Start(); err != nil {
return err
}
return nil
}
// Writes the given samples to the audio file.
func (writer *AudioWriter) Write(samples interface{}) error {
buffer := samplesToBytes(samples)
if buffer == nil {
return fmt.Errorf("invalid sample data type")
}
// If cmd is nil, audio writing has not been set up.
if writer.cmd == nil {
if err := writer.init(); err != nil {
return err
}
}
total := 0
for total < len(buffer) {
n, err := writer.pipe.Write(buffer[total:])
if err != nil {
return err
}
total += n
}
return nil
}
// Closes the pipe and stops the ffmpeg process.
func (writer *AudioWriter) Close() {
if writer.pipe != nil {
writer.pipe.Close()
}
if writer.cmd != nil {
writer.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 (writer *AudioWriter) cleanup() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if writer.pipe != nil {
writer.pipe.Close()
}
if writer.cmd != nil {
writer.cmd.Process.Kill()
}
os.Exit(1)
}()
}