From bc8e70836e9a49854363f8754ce9458317481eb4 Mon Sep 17 00:00:00 2001 From: Andrey Buzin Date: Mon, 16 Nov 2026 13:24:10 -0800 Subject: [PATCH] Detect M4A bytes as audio/mp4 Match the M4A major brand after the ftyp marker at its actual offset. Generic file parts and direct audio detection now identify M4A data as audio/mp4 instead of video/mp4 or an unknown audio format. --- src/ai/types/media.py | 18 +++++++++++++++++- tests/types/test_media.py | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ai/types/media.py b/src/ai/types/media.py index 4e8a2080..dd262da7 100644 --- a/src/ai/types/media.py +++ b/src/ai/types/media.py @@ -196,7 +196,23 @@ def infer_media_type(url: str) -> str: ("audio/ogg", (0x4F, 0x67, 0x67, 0x53)), ("audio/flac", (0x66, 0x4C, 0x61, 0x43)), ("audio/aac", (0x40, 0x15, 0x00, 0x00)), - ("audio/mp4", (0x66, 0x74, 0x79, 0x70)), + ( + "audio/mp4", + ( + None, + None, + None, + None, + 0x66, + 0x74, + 0x79, + 0x70, + 0x4D, + 0x34, + 0x41, + 0x20, + ), + ), ("audio/webm", (0x1A, 0x45, 0xDF, 0xA3)), ] diff --git a/tests/types/test_media.py b/tests/types/test_media.py index ca50206c..3961d582 100644 --- a/tests/types/test_media.py +++ b/tests/types/test_media.py @@ -4,6 +4,7 @@ import base64 +import ai from ai.types import media _RIFF_WEBP = bytes( @@ -107,3 +108,13 @@ def test_detect_video_media_type_base64() -> None: def test_detect_video_media_type_rejects_image() -> None: assert media.detect_video_media_type(_JPEG) is None + + +def test_m4a_bytes_are_detected_as_audio() -> None: + data = b"\x00\x00\x00\x20ftypM4A \x00\x00\x00\x00M4A mp42isom" + assert ai.file_part(data).media_type == "audio/mp4" + assert media.detect_audio_media_type(data) == "audio/mp4" + assert ( + media.detect_audio_media_type(base64.b64encode(data).decode()) + == "audio/mp4" + )