Skip to content

Commit 84fa524

Browse files
authored
Merge pull request #4 from tonytony2020/supportAllTrackType
supports parsing matroska MKV file and all track types
2 parents fbcc5c7 + ae39bc6 commit 84fa524

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
.DS_Store
1+
# temporaries
2+
.*
23

4+
# binaries
5+
*.bin
6+
*.exe

parser.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ import (
1212
"github.com/ebml-go/ebml"
1313
)
1414

15+
type TrackType uint8 // ffmpeg define this in 8 bits(uint8 in Go)
16+
17+
const (
18+
TrackTypeNone TrackType = 0x0
19+
TrackTypeVideo TrackType = 0x1
20+
TrackTypeAudio TrackType = 0x2
21+
TrackTypeComplex TrackType = 0x3
22+
TrackTypeLogo TrackType = 0x10
23+
TrackTypeSubtitle TrackType = 0x11
24+
TrackTypeButtons TrackType = 0x12
25+
TrackTypeControl TrackType = 0x20
26+
TrackTypeMetadata TrackType = 0x21
27+
)
28+
1529
type WebM struct {
1630
Header `ebml:"1a45dfa3"`
1731
Segment `ebml:"18538067"`
@@ -82,11 +96,15 @@ func (t *TrackEntry) GetDefaultDuration() time.Duration {
8296
}
8397

8498
func (t *TrackEntry) IsVideo() bool {
85-
return t.TrackType == 1
99+
return TrackType(t.TrackType) == TrackTypeVideo
86100
}
87101

88102
func (t *TrackEntry) IsAudio() bool {
89-
return t.TrackType == 2
103+
return TrackType(t.TrackType) == TrackTypeAudio
104+
}
105+
106+
func (t *TrackEntry) IsSubtitle() bool {
107+
return TrackType(t.TrackType) == TrackTypeSubtitle
90108
}
91109

92110
type Video struct {
@@ -128,11 +146,18 @@ type SegmentInformation struct {
128146
WritingApp string `ebml:"5741"`
129147
}
130148

149+
// return duration in seconds
131150
func (s *SegmentInformation) GetDuration() time.Duration {
132151
return time.Second * time.Duration(
133152
s.Duration*float64(s.TimecodeScale)/1000000000)
134153
}
135154

155+
// return duration in milliseconds
156+
func (s *SegmentInformation) GetDurationMs() time.Duration {
157+
return time.Millisecond * time.Duration(
158+
s.Duration*float64(s.TimecodeScale)/1000000)
159+
}
160+
136161
type Cluster struct {
137162
simpleBlock []byte `ebml:"A3" ebmlstop:"1"`
138163
Timecode uint `ebml:"E7"`
@@ -178,8 +203,8 @@ func Parse(r io.ReadSeeker, m *WebM) (wr *Reader, err error) {
178203
if err == nil {
179204
err = e.Unmarshal(m)
180205
dt := m.Header.DocType
181-
if dt != "webm" && dt != "webm\000" {
182-
err = errors.New("Not a WebM file")
206+
if dt != "webm" && dt != "webm\000" && dt != "matroska" {
207+
err = errors.New("Not a WebM or matroska file")
183208
}
184209
if err != nil && err.Error() == "Reached payload" {
185210
segment := err.(ebml.ReachedPayloadError).Element

0 commit comments

Comments
 (0)