1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
# Copyright 2013 Christoph Reiter
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
import os
import shutil
from io import BytesIO
import mutagen
from tests import TestCase, get_data_path, mkstemp
from quodlibet.formats.mp4 import MP4File
from quodlibet.formats._image import EmbeddedImage
import mutagen.mp4
from .helper import get_temp_copy
class TMP4File(TestCase):
def setUp(self):
self.f = get_temp_copy(get_data_path("test.m4a"))
self.song = MP4File(self.f)
def tearDown(self):
os.unlink(self.f)
def _assert_tag_supported(self, tag, value="SomeTestValue"):
self.song[tag] = value
self.song.write()
self.song.reload()
self.assertEqual(self.song(tag), value)
def test_format(self):
self.assertEqual(self.song("~format"), "MPEG-4")
def test_codec(self):
self.assertEqual(self.song("~codec"), "AAC LC")
def test_encoding(self):
self.assertEqual(self.song("~encoding"), "FAAC 1.24")
def test_mb_release_track_id(self):
tag = mutagen.mp4.MP4(self.f)
tag["----:com.apple.iTunes:MusicBrainz Release Track Id"] = [b"foo"]
tag.save()
song = MP4File(self.f)
self.assertEqual(song("musicbrainz_releasetrackid"), "foo")
song["musicbrainz_releasetrackid"] = "bla"
song.write()
tag = mutagen.mp4.MP4(self.f)
self.assertEqual(
tag["----:com.apple.iTunes:MusicBrainz Release Track Id"], [b"bla"]
)
def test_basic(self):
self._assert_tag_supported("title")
self._assert_tag_supported("artist")
self._assert_tag_supported("albumartist")
self._assert_tag_supported("album")
self._assert_tag_supported("genre")
self._assert_tag_supported("date")
def test_basic_numeric(self):
self._assert_tag_supported("tracknumber", "12")
self._assert_tag_supported("discnumber", "1")
self._assert_tag_supported("bpm", "132")
def test_less_common_tags(self):
self._assert_tag_supported("discsubtitle")
self._assert_tag_supported("mood")
self._assert_tag_supported("conductor")
self._assert_tag_supported("description")
def test_replaygain_tags(self):
self._assert_tag_supported("replaygain_album_gain", "-5.67 dB")
self._assert_tag_supported("replaygain_album_peak", "1.0")
self._assert_tag_supported("replaygain_track_gain", "-5.67 dB")
self._assert_tag_supported("replaygain_track_peak", "1.0")
self._assert_tag_supported("replaygain_reference_loudness", "89 dB")
def test_length(self):
self.assertAlmostEqual(self.song("~#length"), 3.7079, 3)
def test_bitrate(self):
self.assertEqual(self.song("~#bitrate"), 2)
def test_channels(self):
assert self.song("~#channels") == 2
def test_samplerate(self):
assert self.song("~#samplerate") == 44100
def test_bitdepth(self):
assert self.song("~#bitdepth") == 16
def test_bpm_rounds(self):
self.song["bpm"] = "98.76"
self.song.write()
self.song.reload()
self.assertEqual(self.song("bpm"), "99")
self.assertEqual(self.song("~#bpm"), 99)
def test_empty_disk_trkn(self):
for key in ["trkn", "disk"]:
tag = mutagen.mp4.MP4(self.f)
tag[key] = []
tag.save()
tag = mutagen.mp4.MP4(self.f)
assert tag[key] == []
self.song.reload()
def test_write(self):
self.song.write()
def test_can_change(self):
assert self.song.can_change("title")
assert not self.song.can_change("foobar")
assert "albumartist" in self.song.can_change()
def test_invalid(self):
path = get_data_path("empty.xm")
assert os.path.exists(path)
self.assertRaises(Exception, MP4File, path)
def test_get_image(self):
image = self.song.get_primary_image()
assert image
self.assertEqual(image.mime_type, "image/png")
def test_get_images(self):
images = self.song.get_images()
assert images and len(images) == 2
def test_get_image_non(self):
tag = mutagen.mp4.MP4(self.f)
tag.pop("covr", None)
tag.save()
self.song.reload()
assert not self.song.get_primary_image()
def test_clear_images(self):
assert self.song.valid()
assert self.song.has_images
self.song.clear_images()
assert not self.song.has_images
assert not self.song.get_primary_image()
tag = mutagen.mp4.MP4(self.f)
assert "covr" not in tag
def test_set_image(self):
assert self.song.has_images
fileobj = BytesIO(b"foo")
image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8)
self.song.set_image(image)
image = self.song.get_primary_image()
assert image
self.assertEqual(image.read(), b"foo")
assert self.song.has_images
def test_can_change_images(self):
assert self.song.can_change_images
def test_can_multiple_values(self):
self.assertEqual(self.song.can_multiple_values(), [])
assert not self.song.can_multiple_values("artist")
def test_m4b_support(self):
path = get_data_path("test.m4a")
fd, filename = mkstemp(suffix="m4b")
os.close(fd)
shutil.copy(path, filename)
self.song = MP4File(filename)
assert self.song("~format") == "MPEG-4"
self._assert_tag_supported("title")
|