Skip to content

Commit

Permalink
More typical MP4 tag mixed-case format
Browse files Browse the repository at this point in the history
  • Loading branch information
gavtroy committed Jan 20, 2024
1 parent 7b0819b commit 131bc30
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
16 changes: 8 additions & 8 deletions r128gain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,16 +481,16 @@ def tag( # noqa: C901
# https://github.com/xbmc/xbmc/blob/9e855967380ef3a5d25718ff2e6db5e3dd2e2829/xbmc/music/tags/TagLoaderTagLib.cpp#L806-L812
if loudness is not None:
assert peak is not None
mf["----:com.apple.iTunes:replaygain_track_gain"] = mutagen.mp4.MP4FreeForm(
mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"] = mutagen.mp4.MP4FreeForm(
f"{RG2_REF_R128_LOUDNESS_DBFS - loudness:.2f} dB".encode()
)
mf["----:com.apple.iTunes:replaygain_track_peak"] = mutagen.mp4.MP4FreeForm(f"{peak:.6f}".encode())
mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"] = mutagen.mp4.MP4FreeForm(f"{peak:.6f}".encode())
if album_loudness is not None:
assert album_peak is not None
mf["----:com.apple.iTunes:replaygain_album_gain"] = mutagen.mp4.MP4FreeForm(
mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN"] = mutagen.mp4.MP4FreeForm(
f"{RG2_REF_R128_LOUDNESS_DBFS - album_loudness:.2f} dB".encode()
)
mf["----:com.apple.iTunes:replaygain_album_peak"] = mutagen.mp4.MP4FreeForm(f"{album_peak:.6f}".encode())
mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK"] = mutagen.mp4.MP4FreeForm(f"{album_peak:.6f}".encode())

else:
raise RuntimeError(f"Unhandled {mf.__class__.__qualname__!r} tag format for file {filepath!r}")
Expand Down Expand Up @@ -534,11 +534,11 @@ def has_loudness_tag(filepath: str) -> Optional[Tuple[bool, bool]]:
album = ("REPLAYGAIN_ALBUM_GAIN" in mf) and ("REPLAYGAIN_ALBUM_PEAK" in mf)

elif isinstance(mf.tags, mutagen.mp4.MP4Tags):
track = ("----:com.apple.iTunes:replaygain_track_gain" in mf) and (
"----:com.apple.iTunes:replaygain_track_peak" in mf
track = ("----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN" in mf) and (
"----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK" in mf
)
album = ("----:com.apple.iTunes:replaygain_album_gain" in mf) and (
"----:com.apple.iTunes:replaygain_album_peak" in mf
album = ("----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN" in mf) and (
"----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK" in mf
)

else:
Expand Down
102 changes: 51 additions & 51 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,20 +362,20 @@ def test_tag(self):

if delete_tags:
mf = mutagen.File(self.m4a_filepath)
self.assertNotIn("----:com.apple.iTunes:replaygain_track_gain", mf)
self.assertNotIn("----:com.apple.iTunes:replaygain_track_peak", mf)
self.assertNotIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN", mf)
self.assertNotIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK", mf)
r128gain.tag(self.m4a_filepath, loudness, peak)
mf = mutagen.File(self.m4a_filepath)
self.assertIsInstance(mf.tags, mutagen.mp4.MP4Tags)
self.assertIn("----:com.apple.iTunes:replaygain_track_gain", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_track_gain"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_track_gain"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"][0]).decode(),
f"{expected_track_gain_rg2:.2f} dB",
)
self.assertIn("----:com.apple.iTunes:replaygain_track_peak", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_track_peak"]), 1)
self.assertEqual(bytes(mf["----:com.apple.iTunes:replaygain_track_peak"][0]).decode(), f"{peak:.6f}")
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"]), 1)
self.assertEqual(bytes(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"][0]).decode(), f"{peak:.6f}")

if delete_tags:
mf = mutagen.File(self.aac_filepath)
Expand Down Expand Up @@ -529,34 +529,34 @@ def test_process(self): # noqa: C901

mf = mutagen.File(self.m4a_filepath)
self.assertIsInstance(mf.tags, mutagen.mp4.MP4Tags)
self.assertIn("----:com.apple.iTunes:replaygain_track_gain", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_track_gain"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_track_gain"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"][0]).decode(),
f"{ref_loudness_rg2 - self.ref_levels[self.m4a_filepath][0]:.2f} dB",
)
self.assertIn("----:com.apple.iTunes:replaygain_track_peak", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_track_peak"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_track_peak"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"][0]).decode(),
f"{self.ref_levels[self.m4a_filepath][1]:.6f}",
)
if album_gain:
self.assertIn("----:com.apple.iTunes:replaygain_album_gain", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_album_gain"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_album_gain"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN"][0]).decode(),
f"{ref_loudness_rg2 - self.ref_levels[r128gain.ALBUM_GAIN_KEY][0]:.2f} dB",
)
self.assertIn("----:com.apple.iTunes:replaygain_album_peak", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_album_peak"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_album_peak"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK"][0]).decode(),
f"{self.ref_levels[self.max_peak_filepath][1]:.6f}",
)
elif i == 0:
self.assertNotIn("----:com.apple.iTunes:replaygain_album_gain", mf)
self.assertNotIn("----:com.apple.iTunes:replaygain_album_peak", mf)
self.assertNotIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN", mf)
self.assertNotIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK", mf)

mf = mutagen.File(self.aac_filepath)
self.assertIsInstance(mf.tags, mutagen.apev2.APEv2)
Expand Down Expand Up @@ -761,35 +761,35 @@ def test_process_recursive(self): # noqa: C901

mf = mutagen.File(self.m4a_filepath)
self.assertIsInstance(mf.tags, mutagen.mp4.MP4Tags)
self.assertIn("----:com.apple.iTunes:replaygain_track_gain", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_track_gain"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_track_gain"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"][0]).decode(),
f"{ref_loudness_rg2 - self.ref_levels[self.m4a_filepath][0]:.2f} dB",
)
self.assertIn("----:com.apple.iTunes:replaygain_track_peak", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_track_peak"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_track_peak"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"][0]).decode(),
f"{self.ref_levels[self.m4a_filepath][1]:.6f}",
)
if album_gain:
self.assertIn("----:com.apple.iTunes:replaygain_album_gain", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_album_gain"]), 1)
self.assertValidGainStr(bytes(mf["----:com.apple.iTunes:replaygain_album_gain"][0]).decode(), 2)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN"]), 1)
self.assertValidGainStr(bytes(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN"][0]).decode(), 2)
self.assertGainStrAlmostEqual(
bytes(mf["----:com.apple.iTunes:replaygain_album_gain"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN"][0]).decode(),
ref_loudness_rg2 - self.ref_levels[r128gain.ALBUM_GAIN_KEY][0],
)
self.assertIn("----:com.apple.iTunes:replaygain_album_peak", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_album_peak"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_album_peak"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK"][0]).decode(),
f"{self.ref_levels[self.max_peak_filepath][1]:.6f}",
)
elif i == 0:
self.assertNotIn("----:com.apple.iTunes:replaygain_album_gain", mf)
self.assertNotIn("----:com.apple.iTunes:replaygain_album_peak", mf)
self.assertNotIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN", mf)
self.assertNotIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK", mf)

mf = mutagen.File(self.aac_filepath)
self.assertIsInstance(mf.tags, mutagen.apev2.APEv2)
Expand Down Expand Up @@ -928,34 +928,34 @@ def test_process_recursive(self): # noqa: C901

mf = mutagen.File(album2_m4a_filepath)
self.assertIsInstance(mf.tags, mutagen.mp4.MP4Tags)
self.assertIn("----:com.apple.iTunes:replaygain_track_gain", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_track_gain"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_track_gain"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_GAIN"][0]).decode(),
f"{ref_loudness_rg2 - self.ref_levels[self.m4a_filepath][0]:.2f} dB",
)
self.assertIn("----:com.apple.iTunes:replaygain_track_peak", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_track_peak"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_track_peak"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_TRACK_PEAK"][0]).decode(),
f"{self.ref_levels[self.m4a_filepath][1]:.6f}",
)
if album_gain:
self.assertIn("----:com.apple.iTunes:replaygain_album_gain", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_album_gain"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_album_gain"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN"][0]).decode(),
f"{ref_loudness_rg2 - ref_levels_dir2[0]:.2f} dB",
)
self.assertIn("----:com.apple.iTunes:replaygain_album_peak", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:replaygain_album_peak"]), 1)
self.assertIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK", mf)
self.assertEqual(len(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK"]), 1)
self.assertEqual(
bytes(mf["----:com.apple.iTunes:replaygain_album_peak"][0]).decode(),
bytes(mf["----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK"][0]).decode(),
f"{ref_levels_dir2[1]:.6f}",
)
elif i == 0:
self.assertNotIn("----:com.apple.iTunes:replaygain_album_gain", mf)
self.assertNotIn("----:com.apple.iTunes:replaygain_album_peak", mf)
self.assertNotIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_GAIN", mf)
self.assertNotIn("----:com.apple.iTunes:REPLAYGAIN_ALBUM_PEAK", mf)

def test_oggopus_output_gain(self):
"""Test opusgain.parse_oggopus_output_gain."""
Expand Down

0 comments on commit 131bc30

Please sign in to comment.