Skip to content

Commit 008a59b

Browse files
author
Florian Angermeier
committed
types/video: Fix differentiation between Matroska and WebM files
EBML files can be identified further using the EBML Document Type. See: https://www.matroska.org/technical/basics.html https://datatracker.ietf.org/doc/html/rfc8794#section-11.2.6 https://datatracker.ietf.org/doc/html/rfc8794#section-17.2
1 parent 4713574 commit 008a59b

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

filetype/types/video.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,9 @@ def __init__(self):
6767
)
6868

6969
def match(self, buf):
70-
return ((len(buf) > 15 and
71-
buf[0] == 0x1A and buf[1] == 0x45 and
72-
buf[2] == 0xDF and buf[3] == 0xA3 and
73-
buf[4] == 0x93 and buf[5] == 0x42 and
74-
buf[6] == 0x82 and buf[7] == 0x88 and
75-
buf[8] == 0x6D and buf[9] == 0x61 and
76-
buf[10] == 0x74 and buf[11] == 0x72 and
77-
buf[12] == 0x6F and buf[13] == 0x73 and
78-
buf[14] == 0x6B and buf[15] == 0x61) or
79-
(len(buf) > 38 and
80-
buf[31] == 0x6D and buf[32] == 0x61 and
81-
buf[33] == 0x74 and buf[34] == 0x72 and
82-
buf[35] == 0x6f and buf[36] == 0x73 and
83-
buf[37] == 0x6B and buf[38] == 0x61))
70+
contains_ebml_element = buf.startswith(b'\x1A\x45\xDF\xA3')
71+
contains_doctype_element = buf.find(b'\x42\x82\x88matroska') > -1
72+
return contains_ebml_element and contains_doctype_element
8473

8574

8675
class Webm(Type):
@@ -97,12 +86,9 @@ def __init__(self):
9786
)
9887

9988
def match(self, buf):
100-
return (len(buf) > 3 and
101-
buf[0] == 0x1A and
102-
buf[1] == 0x45 and
103-
buf[2] == 0xDF and
104-
buf[3] == 0xA3)
105-
89+
contains_ebml_element = buf.startswith(b'\x1A\x45\xDF\xA3')
90+
contains_doctype_element = buf.find(b'\x42\x82\x84webm') > -1
91+
return contains_ebml_element and contains_doctype_element
10692

10793
class Mov(IsoBmff):
10894
"""

0 commit comments

Comments
 (0)