@@ -114,8 +114,8 @@ func New(r io.Reader) (*MPEG, error) {
114114 return m , nil
115115}
116116
117- // HasHeaders checks whether we have headers on all available streams, and if we can accurately
118- // report the number of video/audio streams, video dimensions, framerate and audio samplerate.
117+ // HasHeaders checks whether we have headers on all available streams, and if we can report the
118+ // number of video/audio streams, video dimensions, framerate and audio samplerate.
119119func (m * MPEG ) HasHeaders () bool {
120120 if ! m .demux .HasHeaders () {
121121 return false
@@ -132,6 +132,25 @@ func (m *MPEG) HasHeaders() bool {
132132 return true
133133}
134134
135+ // Probe probes the MPEG-PS data to find the actual number of video and audio streams within the buffer.
136+ // For certain files (e.g. VideoCD) this can be more accurate than just reading the number of streams from the headers.
137+ // This should only be used when the underlying reader is seekable.
138+ // The necessary probe size is dependent on the files you expect to read. Usually a few hundred KB should be enough to find all streams.
139+ // Use Num{Audio|Video}Streams() afterwards to get the number of streams in the file.
140+ // Returns true if any streams were found within the probe size.
141+ func (m * MPEG ) Probe (probeSize int ) bool {
142+ if ! m .demux .Probe (probeSize ) {
143+ return false
144+ }
145+
146+ // Re-init decoders
147+ m .hasDecoders = false
148+ m .videoPacketType = 0
149+ m .audioPacketType = 0
150+
151+ return m .initDecoders ()
152+ }
153+
135154// Done returns done channel.
136155func (m * MPEG ) Done () chan bool {
137156 return m .done
@@ -451,7 +470,7 @@ func (m *MPEG) SeekFrame(tm time.Duration, seekExact bool) *Frame {
451470 duration := m .demux .Duration (typ )
452471
453472 if tm .Seconds () < 0 {
454- tm = 0
473+ tm = time . Duration ( 0 )
455474 } else if tm .Seconds () > duration {
456475 tm = time .Duration (duration * float64 (time .Second ))
457476 }
@@ -570,33 +589,31 @@ func (m *MPEG) initDecoders() bool {
570589 m .videoPacketType = PacketVideo1
571590 }
572591
573- m .videoBuffer , err = NewBuffer (nil )
574- if err != nil {
575- return false
576- }
592+ if m .videoDecoder == nil {
593+ m .videoBuffer , err = NewBuffer (nil )
594+ if err != nil {
595+ return false
596+ }
577597
578- m .videoBuffer .SetLoadCallback (m .readVideoPacket )
598+ m .videoBuffer .SetLoadCallback (m .readVideoPacket )
599+ m .videoDecoder = NewVideo (m .videoBuffer )
600+ }
579601 }
580602
581603 if m .demux .NumAudioStreams () > 0 {
582604 if m .audioEnabled {
583605 m .audioPacketType = PacketAudio1 + m .audioStreamIndex
584606 }
585607
586- m .audioBuffer , err = NewBuffer (nil )
587- if err != nil {
588- return false
589- }
590-
591- m .audioBuffer .SetLoadCallback (m .readAudioPacket )
592- }
593-
594- if m .videoBuffer != nil {
595- m .videoDecoder = NewVideo (m .videoBuffer )
596- }
608+ if m .audioDecoder == nil {
609+ m .audioBuffer , err = NewBuffer (nil )
610+ if err != nil {
611+ return false
612+ }
597613
598- if m .audioBuffer != nil {
599- m .audioDecoder = NewAudio (m .audioBuffer )
614+ m .audioBuffer .SetLoadCallback (m .readAudioPacket )
615+ m .audioDecoder = NewAudio (m .audioBuffer )
616+ }
600617 }
601618
602619 m .hasDecoders = true
0 commit comments