Improve magic producer about support mjpeg with trash on start

This commit is contained in:
Alex X
2023-11-18 17:08:57 +03:00
parent 43449e7b08
commit 742cb7699b
+12 -1
View File
@@ -44,5 +44,16 @@ func Open(r io.Reader) (core.Producer, error) {
return mpegts.Open(rd)
}
return nil, errors.New("magic: unsupported header: " + hex.EncodeToString(b))
// support MJPEG with trash on start
// https://github.com/AlexxIT/go2rtc/issues/747
if b, err = rd.Peek(4096); err != nil {
return nil, err
}
if i := bytes.Index(b, []byte{0xFF, 0xD8, 0xFF, 0xDB}); i > 0 {
_, _ = io.ReadFull(rd, make([]byte, i))
return mjpeg.Open(rd)
}
return nil, errors.New("magic: unsupported header: " + hex.EncodeToString(b[:4]))
}