From a059a72f498dc9b124c2c7729a02aea3fa6101db Mon Sep 17 00:00:00 2001 From: Leandro Moreira Date: Mon, 13 May 2024 23:01:51 -0300 Subject: [PATCH] search for first h264 video stream --- astiav_test.go | 26 ++++++++++++++++++++++---- bit_stream_filter_context_test.go | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/astiav_test.go b/astiav_test.go index 0e284ee..3d02683 100644 --- a/astiav_test.go +++ b/astiav_test.go @@ -155,7 +155,7 @@ func (h *helper) inputFirstPacket(name string) (pkt *Packet, err error) { return } -func (h *helper) inputFirstPacketWithBitStreamFilter(name string, fn string) (pkt *Packet, err error) { +func (h *helper) inputFirstVideoPacketWithBitStreamFilter(name string, fn string) (pkt *Packet, err error) { var fc *FormatContext if fc, err = h.inputFormatContext(name); err != nil { err = fmt.Errorf("astiav_test: getting input format context failed") @@ -168,14 +168,32 @@ func (h *helper) inputFirstPacketWithBitStreamFilter(name string, fn string) (pk return } h.closer.Add(pkt.Free) + var foundVideo bool - if err = fc.ReadFrame(pkt); err != nil { - err = fmt.Errorf("astiav_test: reading frame failed: %w", err) + for { + if err = fc.ReadFrame(pkt); err != nil { + err = fmt.Errorf("astiav_test: reading frame failed: %w", err) + return + } + + for _, s := range fc.Streams() { + if s.Index() == pkt.StreamIndex() && s.CodecParameters().CodecID() == CodecIDH264 { + foundVideo = true + break + } + } + if foundVideo { + break + } + } + + if !foundVideo { + err = fmt.Errorf("astiav_test: there must be an h264 stream") + pkt = nil return } var bsfc *BitStreamFilterContext - bsfc, err = h.bitStreamFilterContext(fc, pkt.StreamIndex(), fn) if err != nil { pkt = nil diff --git a/bit_stream_filter_context_test.go b/bit_stream_filter_context_test.go index b032565..af88c6e 100644 --- a/bit_stream_filter_context_test.go +++ b/bit_stream_filter_context_test.go @@ -34,7 +34,7 @@ func TestBitStreamFilterContext(t *testing.T) { // video.mp4 bit stream h264 format is avcc pkt1, err := globalHelper.inputFirstPacket("video.mp4") - pkt1Bsf, errBsf := globalHelper.inputFirstPacketWithBitStreamFilter("video.mp4", "h264_mp4toannexb") + pkt1Bsf, errBsf := globalHelper.inputFirstVideoPacketWithBitStreamFilter("video.mp4", "h264_mp4toannexb") require.NoError(t, err) require.NoError(t, errBsf)