Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse ADTS header #658

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions mutagen/aac.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,34 @@ def load(self, filething):
def add_tags(self):
raise AACError("doesn't support tags")

@staticmethod
def _is_probably_adts_header(header: bytes) -> bool:
if len(header) < 9:
return False

# Syncword
if not (header[0] == 0xFF):
return False
if not (header[1] & 0xF0 == 0xF0):
return False

# Layer
if not (header[1] & 0b0000_0110 == 0):
return False

# Sampling index
if ((header[2] & 0b0011_1100) >> 2) > 0xC:
return False

return True

@staticmethod
def score(filename, fileobj, header):
filename = filename.lower()
s = endswith(filename, ".aac") or endswith(filename, ".adts") or \
endswith(filename, ".adif")
s += b"ADIF" in header
s += int(AAC._is_probably_adts_header(header))
return s


Expand Down
11 changes: 8 additions & 3 deletions tests/test_aac.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
class TADTS(TestCase):

def setUp(self):
original = os.path.join(DATA_DIR, "empty.aac")
self.filename = get_temp_copy(original)
self.original = os.path.join(DATA_DIR, "empty.aac")
self.filename = get_temp_copy(self.original)

tag = ID3()
tag.add(TIT1(text=[u"a" * 5000], encoding=3))
tag.save(self.filename)

self.aac = AAC(original)
self.aac = AAC(self.original)
self.aac_id3 = AAC(self.filename)

def tearDown(self):
Expand Down Expand Up @@ -52,6 +52,11 @@ def test_pprint(self):
self.assertEqual(self.aac.pprint(), self.aac_id3.pprint())
self.assertTrue("ADTS" in self.aac.pprint())

def test_score_no_ext(self):
with open(self.original, "rb") as fileobj:
header = fileobj.read(128)
self.failUnless(AAC.score("empty", fileobj, header))


class TADIF(TestCase):

Expand Down
Loading