Skip to content

Commit

Permalink
Merge pull request #26 from wolveix/add-missing-stream-types
Browse files Browse the repository at this point in the history
Added two missing data stream constants & added to test
  • Loading branch information
vansante authored Aug 20, 2021
2 parents 9f17430 + fff3452 commit ec434fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
14 changes: 12 additions & 2 deletions ffprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,24 @@ func validateData(t *testing.T, data *ProbeData) {
t.Errorf("It does not have a subtitle stream.")
}

stream = data.StreamType(StreamData)
if len(stream) != 0 {
t.Errorf("It does not have a data stream.")
}

stream = data.StreamType(StreamAttachment)
if len(stream) != 0 {
t.Errorf("It does not have an attachment stream.")
}

stream = data.StreamType(StreamAny)
if len(stream) != 2 {
t.Errorf("It should have two streams.")
}

// test Format.Duration
dration := data.Format.Duration()
if dration.Seconds() != 5.312 {
duration := data.Format.Duration()
if duration.Seconds() != 5.312 {
t.Errorf("this video is 5.312s.")
}
// test Format.StartTime
Expand Down
42 changes: 22 additions & 20 deletions probedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const (
StreamAudio StreamType = "audio"
// StreamSubtitle is a subtitle stream
StreamSubtitle StreamType = "subtitle"
// StreamData is a data stream
StreamData StreamType = "data"
// StreamAttachment is an attachment stream
StreamAttachment StreamType = "attachment"
)

// ProbeData is the root json data structure returned by an ffprobe.
Expand Down Expand Up @@ -142,39 +146,37 @@ func (p *ProbeData) StreamType(streamType StreamType) (streams []Stream) {

// FirstVideoStream returns the first video stream found
func (p *ProbeData) FirstVideoStream() *Stream {
for _, s := range p.Streams {
if s == nil {
continue
}
if s.CodecType == string(StreamVideo) {
return s
}
}
return nil
return p.firstStream(StreamVideo)
}

// FirstAudioStream returns the first audio stream found
func (p *ProbeData) FirstAudioStream() *Stream {
for _, s := range p.Streams {
if s == nil {
continue
}
if s.CodecType == string(StreamAudio) {
return s
}
}
return nil
return p.firstStream(StreamAudio)
}

// FirstSubtitleStream returns the first subtitle stream found
func (p *ProbeData) FirstSubtitleStream() *Stream {
return p.firstStream(StreamSubtitle)
}

// FirstDataStream returns the first data stream found
func (p *ProbeData) FirstDataStream() *Stream {
return p.firstStream(StreamData)
}

// FirstAttachmentStream returns the first attachment stream found
func (p *ProbeData) FirstAttachmentStream() *Stream {
return p.firstStream(StreamAttachment)
}

func (p *ProbeData) firstStream(streamType StreamType) *Stream {
for _, s := range p.Streams {
if s == nil {
continue
}
if s.CodecType == string(StreamSubtitle) {
if s.CodecType == string(streamType) {
return s
}
}
return nil
}
}

0 comments on commit ec434fe

Please sign in to comment.