forked from bwmarrin/dca
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
582 lines (474 loc) · 12.8 KB
/
main.go
File metadata and controls
582 lines (474 loc) · 12.8 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package main
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"image/jpeg"
"image/png"
"io"
"os"
"os/exec"
"strconv"
"sync"
"layeh.com/gopus"
)
// Define constants
const (
// The current version of the DCA format
FormatVersion int8 = 1
// The name of the DCA distribution
ProgramName string = "dca_for_dphp"
// The current version of the DCA distribution
ProgramVersion string = "0.0.1"
// The URL to the GitHub repository of the DCA distribution
GitHubRepositoryURL string = "https://github.com/davidcole1340/dca"
// Name of the author(s) of the DCA distribution
ProgramAuthor string = "bwmarrin, davidcole1340"
)
// All global variables used within the program
var (
// Buffer for some commands
CmdBuf bytes.Buffer
PngBuf bytes.Buffer
CoverImage string
// Metadata structures
Metadata MetadataStruct
FFprobeData FFprobeMetadata
// Encode or Decode
Mode string
// Magic bytes to write at the start of a DCA file
MagicBytes string = fmt.Sprintf("DCA%d", FormatVersion)
// 1 for mono, 2 for stereo
Channels int
// Must be one of 8000, 12000, 16000, 24000, or 48000.
// Discord only uses 48000 currently.
FrameRate int
// Rates from 1000 to 512000 bits per second are meaningful
// Discord only uses 8000 to 384000 and default is 64000
Bitrate int
// Must be one of voip, audio, or lowdelay.
// DCA defaults to audio which is ideal for music
// Not sure what Discord uses here, probably voip
Application string
// if true, dca sends raw output without any magic bytes or json metadata
RawOutput bool
FrameSize int // uint16 size of each audio frame
MaxBytes int // max size of opus data
Volume int // change audio volume (256=normal)
OpusEncoder *gopus.Encoder
OpusDecoder *gopus.Decoder
InFile string
CoverFormat string = "jpeg"
OutFile string = "pipe:1"
OutBuf []byte
Wait bool
EncodeInputChan chan []int16
EncodeOutputChan chan []byte
DecodeInputChan chan []byte
DecodeOutputChan chan []int16
err error
wg sync.WaitGroup
)
// init configures and parses the command line arguments
func init() {
flag.StringVar(&Mode, "mode", "encode", "encode or decode")
flag.StringVar(&InFile, "i", "pipe:0", "infile")
flag.IntVar(&Volume, "vol", 256, "change audio volume (256=normal)")
flag.IntVar(&Channels, "ac", 2, "audio channels")
flag.IntVar(&FrameRate, "ar", 48000, "audio sampling rate")
flag.IntVar(&FrameSize, "as", 960, "audio frame size can be 960 (20ms), 1920 (40ms), or 2880 (60ms)")
flag.IntVar(&Bitrate, "ab", 64, "audio encoding bitrate in kb/s can be 1 - 512")
flag.BoolVar(&RawOutput, "raw", false, "Raw opus output (no metadata or magic bytes)")
flag.StringVar(&Application, "aa", "audio", "audio application can be voip, audio, or lowdelay")
flag.StringVar(&CoverFormat, "cf", "jpeg", "format the cover art will be encoded with")
flag.BoolVar(&Wait, "w", false, "don't exit when the encoding has finished")
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
flag.Parse()
MaxBytes = (FrameSize * Channels) * 2 // max size of opus data
}
// very simple program that wraps ffmpeg and outputs raw opus data frames
// with a uint16 header for each frame with the frame length in bytes
func main() {
//////////////////////////////////////////////////////////////////////////
// BLOCK : Basic setup and validation
//////////////////////////////////////////////////////////////////////////
// If only one argument provided assume it's a filename.
if len(os.Args) == 2 {
InFile = os.Args[1]
}
// If reading from a file, verify it exists.
if InFile != "pipe:0" {
if _, err := os.Stat(InFile); os.IsNotExist(err) {
fmt.Println("error: infile does not exist")
flag.Usage()
return
}
}
// If reading from pipe, make sure pipe is open
if InFile == "pipe:0" {
fi, err := os.Stdin.Stat()
if err != nil {
fmt.Println(err)
return
}
if (fi.Mode() & os.ModeCharDevice) == 0 {
} else {
fmt.Println("error: stdin is not a pipe.")
flag.Usage()
return
}
}
//////////////////////////////////////////////////////////////////////////
// BLOCK : Create chans, buffers, and encoder for use
//////////////////////////////////////////////////////////////////////////
// create an opusEncoder to use
OpusEncoder, err = gopus.NewEncoder(FrameRate, Channels, gopus.Audio)
if err != nil {
fmt.Println("NewEncoder Error:", err)
return
}
// set opus encoding options
// OpusEncoder.SetVbr(true) // bool
if Bitrate < 1 || Bitrate > 512 {
Bitrate = 64 // Set to Discord default
}
OpusEncoder.SetBitrate(Bitrate * 1000)
switch Application {
case "voip":
OpusEncoder.SetApplication(gopus.Voip)
case "audio":
OpusEncoder.SetApplication(gopus.Audio)
case "lowdelay":
OpusEncoder.SetApplication(gopus.RestrictedLowDelay)
default:
OpusEncoder.SetApplication(gopus.Audio)
}
// create an opusDecoder to use
OpusDecoder, err = gopus.NewDecoder(FrameRate, Channels)
if err != nil {
fmt.Println("NewDecoder Error:", err)
return
}
EncodeOutputChan = make(chan []byte, 10)
EncodeInputChan = make(chan []int16, 10)
DecodeOutputChan = make(chan []int16, 10)
DecodeInputChan = make(chan []byte, 10)
if RawOutput == false && Mode == "encode" {
// Setup the metadata
Metadata = MetadataStruct{
Dca: &DCAMetadata{
Version: FormatVersion,
Tool: &DCAToolMetadata{
Name: ProgramName,
Version: ProgramVersion,
Url: GitHubRepositoryURL,
Author: ProgramAuthor,
},
},
SongInfo: &SongMetadata{},
Origin: &OriginMetadata{},
Opus: &OpusMetadata{
Bitrate: Bitrate * 1000,
SampleRate: FrameRate,
Application: Application,
FrameSize: FrameSize,
Channels: Channels,
},
Extra: &ExtraMetadata{},
}
_ = Metadata
// get ffprobe data
if InFile != "pipe:0" {
ffprobe := exec.Command("ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", InFile)
ffprobe.Stdout = &CmdBuf
err = ffprobe.Start()
if err != nil {
fmt.Println("RunStart Error:", err)
return
}
err = ffprobe.Wait()
if err != nil {
fmt.Println("FFprobe Error:", err)
return
}
err = json.Unmarshal(CmdBuf.Bytes(), &FFprobeData)
if err != nil {
fmt.Println("Erorr unmarshaling the FFprobe JSON:", err)
return
}
bitrateInt, err := strconv.Atoi(FFprobeData.Format.Bitrate)
if err != nil {
fmt.Println("Could not convert bitrate to int:", err)
return
}
if FFprobeData.Format.Tags != nil {
Metadata.SongInfo = &SongMetadata{
Title: FFprobeData.Format.Tags.Title,
Artist: FFprobeData.Format.Tags.Artist,
Album: FFprobeData.Format.Tags.Album,
Genre: FFprobeData.Format.Tags.Genre,
Comments: "", // change later?
}
}
Metadata.Origin = &OriginMetadata{
Source: "file",
Bitrate: bitrateInt,
Channels: Channels,
Encoding: FFprobeData.Format.FormatLongName,
}
CmdBuf.Reset()
// get cover art
cover := exec.Command("ffmpeg", "-loglevel", "0", "-i", InFile, "-f", "singlejpeg", "pipe:1")
cover.Stdout = &CmdBuf
err = cover.Start()
if err != nil {
fmt.Println("RunStart Error:", err)
return
}
err = cover.Wait()
if err == nil {
buf := bytes.NewBufferString(CmdBuf.String())
if CoverFormat == "png" {
img, err := jpeg.Decode(buf)
if err == nil { // silently drop it, no image
err = png.Encode(&PngBuf, img)
if err == nil {
CoverImage = base64.StdEncoding.EncodeToString(PngBuf.Bytes())
}
}
} else {
CoverImage = base64.StdEncoding.EncodeToString(CmdBuf.Bytes())
}
Metadata.SongInfo.Cover = &CoverImage
}
CmdBuf.Reset()
PngBuf.Reset()
} else {
Metadata.Origin = &OriginMetadata{
Source: "pipe",
Channels: Channels,
Encoding: "pcm16/s16le",
}
}
}
//////////////////////////////////////////////////////////////////////////
// BLOCK : Start reader and writer workers
//////////////////////////////////////////////////////////////////////////
wg.Add(3)
if Mode == "encode" {
go encodeReader()
go encoder()
go encodeWriter()
} else {
go decodeReader()
go decoder()
go decodeWriter()
}
// wait for above goroutines to finish, then exit.
wg.Wait()
// if the wait flag is set, don't exit
for Wait {
}
}
// encodeReader reads from the input
func encodeReader() {
defer func() {
close(EncodeInputChan)
wg.Done()
}()
// read from file
if InFile != "pipe:0" {
// Create a shell command "object" to run.
ffmpeg := exec.Command("ffmpeg", "-i", InFile, "-vol", strconv.Itoa(Volume), "-f", "s16le", "-ar", strconv.Itoa(FrameRate), "-ac", strconv.Itoa(Channels), "pipe:1")
stdout, err := ffmpeg.StdoutPipe()
if err != nil {
fmt.Println("StdoutPipe Error:", err)
return
}
// Starts the ffmpeg command
err = ffmpeg.Start()
if err != nil {
fmt.Println("RunStart Error:", err)
return
}
for {
// read data from ffmpeg stdout
InBuf := make([]int16, FrameSize*Channels)
err = binary.Read(stdout, binary.LittleEndian, &InBuf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return
}
if err != nil {
fmt.Println("error reading from ffmpeg stdout :", err)
return
}
// write pcm data to the EncodeInputChan
EncodeInputChan <- InBuf
}
}
// read input from stdin pipe
if InFile == "pipe:0" {
// 16KB input buffer
rbuf := bufio.NewReaderSize(os.Stdin, 16384)
for {
// read data from stdin
InBuf := make([]int16, FrameSize*Channels)
err = binary.Read(rbuf, binary.LittleEndian, &InBuf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return
}
if err != nil {
fmt.Println("error reading from ffmpeg stdout :", err)
return
}
// write pcm data to the EncodeInputChan
EncodeInputChan <- InBuf
}
}
}
// encoder listens on the EncodeInputChan and encodes provided PCM16 data
// to opus, then sends the encoded data to the EncodeOutputChan
func encoder() {
defer func() {
close(EncodeOutputChan)
wg.Done()
}()
for {
pcm, ok := <-EncodeInputChan
if !ok {
// if chan closed, exit
return
}
// try encoding pcm frame with Opus
opus, err := OpusEncoder.Encode(pcm, FrameSize, MaxBytes)
if err != nil {
fmt.Println("Encoding Error:", err)
return
}
// write opus data to EncodeOutputChan
EncodeOutputChan <- opus
}
}
// encodeWriter listens on the EncodeOutputChan and writes the output to stdout pipe
// TODO: Add support for writing directly to a file
func encodeWriter() {
defer wg.Done()
var opuslen int16
var jsonlen int32
// 16KB output buffer
wbuf := bufio.NewWriterSize(os.Stdout, 16384)
defer wbuf.Flush()
if RawOutput == false {
// write the magic bytes
fmt.Print(MagicBytes)
// encode and write json length
json, err := json.Marshal(Metadata)
if err != nil {
fmt.Println("Failed to encode the Metadata JSON:", err)
return
}
jsonlen = int32(len(json))
err = binary.Write(wbuf, binary.LittleEndian, &jsonlen)
if err != nil {
fmt.Println("error writing output: ", err)
return
}
// write the actual json
wbuf.Write(json)
}
for {
opus, ok := <-EncodeOutputChan
if !ok {
// if chan closed, exit
return
}
// write header
opuslen = int16(len(opus))
err = binary.Write(wbuf, binary.LittleEndian, &opuslen)
if err != nil {
fmt.Println("error writing output: ", err)
return
}
// write opus data to stdout
err = binary.Write(wbuf, binary.LittleEndian, &opus)
if err != nil {
fmt.Println("error writing output: ", err)
return
}
}
}
func decodeReader() {
defer func() {
close(DecodeInputChan)
wg.Done()
}()
// 16KB input buffer
rbuf := bufio.NewReaderSize(os.Stdin, 16384)
var opuslen uint16
for {
// read "header" from dca
err = binary.Read(rbuf, binary.LittleEndian, &opuslen)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return
}
if err != nil {
fmt.Println("error reading from stdin:", err)
return
}
// read data from stdin
InBuf := make([]byte, opuslen)
err = binary.Read(rbuf, binary.LittleEndian, &InBuf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return
}
if err != nil {
fmt.Println("error reading from stdin:", err)
return
}
DecodeInputChan <- InBuf
}
}
func decoder() {
defer func() {
close(DecodeOutputChan)
wg.Done()
}()
for {
opus, ok := <-DecodeInputChan
if !ok {
return
}
// try decoding opus frame with Opus
pcm, err := OpusDecoder.Decode(opus, FrameSize, false)
if err != nil {
fmt.Println("Decoding Error:", err)
return
}
DecodeOutputChan <- pcm
}
}
func decodeWriter() {
defer wg.Done()
// 16KB output buffer
wbuf := bufio.NewWriterSize(os.Stdout, 16384)
defer wbuf.Flush()
for {
pcm, ok := <-DecodeOutputChan
if !ok {
return
}
// write pcm
err = binary.Write(wbuf, binary.LittleEndian, &pcm)
if err != nil {
fmt.Println("error writing output:", err)
return
}
}
}