feat: new option to ignore unknown boxes

This commit is contained in:
Martin Riedl 2024-12-28 12:43:52 +01:00
parent f2a9a75725
commit c96343ad39
Signed by: martinr92
GPG key ID: FB68DA65516A804C
2 changed files with 9 additions and 5 deletions

View file

@ -26,6 +26,7 @@ import (
type Parser struct { type Parser struct {
reader io.Reader reader io.Reader
childBoxDefinitions map[string]map[string]BoxDefinition childBoxDefinitions map[string]map[string]BoxDefinition
IgnoreUnknownBoxes bool
Content []any Content []any
} }
@ -119,7 +120,9 @@ func parseNextBox(parser *Parser, reader io.Reader, currentBoxType string, fileP
box, err = boxTypeDefinition.Parser(parser, filePosition, boxHeaderSize, boxContentBytes) box, err = boxTypeDefinition.Parser(parser, filePosition, boxHeaderSize, boxContentBytes)
} else { } else {
logger.Println("unknown box type", boxType, "at", filePosition, "in", currentBoxType) logger.Println("unknown box type", boxType, "at", filePosition, "in", currentBoxType)
return nil, filePosition, false, errors.New("unknown box type " + boxType + " in " + currentBoxType) if !parser.IgnoreUnknownBoxes {
return nil, filePosition, false, errors.New("unknown box type " + boxType + " in " + currentBoxType)
}
} }
// check for box errors // check for box errors

View file

@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
} }
func TestParser(t *testing.T) { func TestParser(t *testing.T) {
parser, err := testFile(t, "test/fmp4.mp4") parser, err := testFile(t, "test/fmp4.mp4", false)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -46,7 +46,7 @@ func TestParser(t *testing.T) {
} }
func TestParserFragmentedMP4Init(t *testing.T) { func TestParserFragmentedMP4Init(t *testing.T) {
_, err := testFile(t, "test/fmp4_init.mp4") _, err := testFile(t, "test/fmp4_init.mp4", true)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -70,12 +70,12 @@ func validateFileType(t *testing.T, parser *Parser) {
} }
func TestParserInvalidSampleCount(t *testing.T) { func TestParserInvalidSampleCount(t *testing.T) {
if _, err := testFile(t, "test/fmp4-incorrect-sample-count.mp4"); err == nil { if _, err := testFile(t, "test/fmp4-incorrect-sample-count.mp4", false); err == nil {
t.Error("missing error of invalid sample count of track fragment run box") t.Error("missing error of invalid sample count of track fragment run box")
} }
} }
func testFile(t *testing.T, filePath string) (*Parser, error) { func testFile(t *testing.T, filePath string, allowIgnore bool) (*Parser, error) {
// open test file // open test file
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
@ -85,6 +85,7 @@ func testFile(t *testing.T, filePath string) (*Parser, error) {
// create new parser // create new parser
parser := NewParser(file) parser := NewParser(file)
parser.IgnoreUnknownBoxes = allowIgnore
err = parser.Parse() err = parser.Parse()
return parser, err return parser, err
} }