diff --git a/README.rst b/README.rst index 0c7db69..3612dce 100644 --- a/README.rst +++ b/README.rst @@ -77,6 +77,7 @@ Image - **psd** - ``image/vnd.adobe.photoshop`` - **ico** - ``image/x-icon`` - **heic** - ``image/heic`` +- **avif** - ``image/avif`` Video ^^^^^ diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index b3c0327..79399a8 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -29,6 +29,7 @@ image.Ico(), image.Heic(), image.Dcm(), + image.Avif(), ) # Supported video types diff --git a/filetype/types/image.py b/filetype/types/image.py index 39cb630..2d4d269 100644 --- a/filetype/types/image.py +++ b/filetype/types/image.py @@ -356,3 +356,28 @@ def __init__(self): def match(self, buf): return buf[:10] == bytearray([0x67, 0x69, 0x6d, 0x70, 0x20, 0x78, 0x63, 0x66, 0x20, 0x76]) + + +class Avif(IsoBmff): + """ + Implements the AVIF image type matcher. + """ + MIME = 'image/avif' + EXTENSION = 'avif' + + def __init__(self): + super(Avif, self).__init__( + mime=Avif.MIME, + extension=Avif.EXTENSION + ) + + def match(self, buf): + if not self._is_isobmff(buf): + return False + + major_brand, minor_version, compatible_brands = self._get_ftyp(buf) + if major_brand == 'avif': + return True + if major_brand in ['mif1', 'msf1'] and 'avif' in compatible_brands: + return True + return False diff --git a/tests/fixtures/sample.avif b/tests/fixtures/sample.avif new file mode 100644 index 0000000..f3a841b Binary files /dev/null and b/tests/fixtures/sample.avif differ diff --git a/tests/test_types.py b/tests/test_types.py index 0919aa3..dc5967c 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -44,6 +44,12 @@ def test_guess_heic(self): self.assertEqual(kind.mime, 'image/heic') self.assertEqual(kind.extension, 'heic') + def test_guess_avif(self): + kind = filetype.guess(FIXTURES + '/sample.avif') + self.assertTrue(kind is not None) + self.assertEqual(kind.mime, 'image/avif') + self.assertEqual(kind.extension, 'avif') + def test_guess_mp4(self): kind = filetype.guess(FIXTURES + '/sample.mp4') self.assertTrue(kind is not None)