-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextract_audio.go
More file actions
153 lines (136 loc) · 3.6 KB
/
extract_audio.go
File metadata and controls
153 lines (136 loc) · 3.6 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
package matroska
import (
"bytes"
"fmt"
"github.com/coding-socks/matroska/internal/vorbis"
"io"
"math/rand/v2"
)
func extractTrackAudio(w io.Writer, s *Scanner, t TrackEntry) error {
switch t.CodecID {
case AudioCodecMP2, AudioCodecMP3:
return extractTrackMPEG(w, s, t)
case AudioCodecVORBIS:
return extractTrackVORBIS(w, s, t)
}
return fmt.Errorf("matroska: unknown audio codec %s", t.CodecID)
}
func extractTrackMPEG(w io.Writer, s *Scanner, t TrackEntry) error {
for s.Next() {
c := s.Cluster()
m := len(c.SimpleBlock) + len(c.BlockGroup)
if m == 0 {
continue
}
for i := range c.SimpleBlock {
block, err := ReadSimpleBlock(c.SimpleBlock[i], c.Timestamp)
if err != nil {
return fmt.Errorf("matroska: could not create block struct: %w", err)
}
if block.TrackNumber() != t.TrackNumber {
continue
}
for _, f := range block.Frames() {
if _, err := io.Copy(w, bytes.NewReader(f)); err != nil {
return err
}
}
}
for i := range c.BlockGroup {
block, err := ReadBlock(c.BlockGroup[i].Block, c.Timestamp)
if err != nil {
return fmt.Errorf("matroska: could not create block struct: %w", err)
}
if block.TrackNumber() != t.TrackNumber {
continue
}
for _, f := range block.Frames() {
if _, err := io.Copy(w, bytes.NewReader(f)); err != nil {
return err
}
}
}
}
return nil
}
// extractTrackVORBIS is based on the Vorbis I specification created by the Xiph.Org Foundation.
// See: https://xiph.org/vorbis/doc/Vorbis_I_spec.pdf
func extractTrackVORBIS(w io.Writer, s *Scanner, track TrackEntry) error {
serialNum := rand.Int32()
vw := vorbis.NewWriter(w, serialNum)
codecPrivate := *track.CodecPrivate
frames := Frames(LacingFlagXiph, codecPrivate)
if len(frames) != 3 {
return fmt.Errorf("matroska: Vorbis audio track requires 3 header pages, got %d", len(frames))
}
ih := frames[0]
if err := vw.WriteIdentHeader(ih); err != nil {
return err
}
iheader, err := vorbis.ParseIdentificationHeader([30]byte(ih))
if err != nil {
return err
}
blockSizes := []uint16{
iheader.Blocksize0,
iheader.Blocksize1,
}
cheader, err := vorbis.ParseCommentHeader(frames[1])
if err != nil {
return err
}
_ = cheader
if err := vw.WriteHeaders(frames[1], frames[2]); err != nil {
return err
}
var (
prevBlockSize uint64 = 0
granpos uint64 = 0
prevFrame []byte //
)
frames = frames[:0] // clear value
for s.Next() {
c := s.Cluster()
if (len(c.SimpleBlock) + len(c.BlockGroup)) == 0 {
continue
}
for i := range c.SimpleBlock {
block, err := ReadSimpleBlock(c.SimpleBlock[i], c.Timestamp)
if err != nil {
return fmt.Errorf("matroska: could not create block struct: %w", err)
}
if block.TrackNumber() != track.TrackNumber {
continue
}
frames = append(frames, block.Frames()...)
}
for i := range c.BlockGroup {
block, err := ReadBlock(c.BlockGroup[i].Block, c.Timestamp)
if err != nil {
return fmt.Errorf("matroska: could not create block struct: %w", err)
}
if block.TrackNumber() != track.TrackNumber {
continue
}
frames = append(frames, block.Frames()...)
}
for _, frame := range frames {
blockSize := uint64(blockSizes[(frame[0]>>1)&1])
if prevFrame != nil {
if err := vw.Segment(prevFrame, granpos, false); err != nil {
return err
}
// We need at least two segment to calculate this
granpos += (blockSize + prevBlockSize) / 4
}
prevBlockSize = blockSize
prevFrame = frame
}
frames = frames[:0]
}
if prevFrame == nil {
return nil
}
// only this element can be the last.
return vw.Segment(prevFrame, granpos, true)
}