| 1 |
import mutagen.mp4 |
| 2 |
|
| 3 |
from mediafile.utils import Image |
| 4 |
|
| 5 |
from .base import ListStorageStyle, SoundCheckStorageStyleMixin, StorageStyle |
| 6 |
|
| 7 |
|
| 8 |
class MP4StorageStyle(StorageStyle): |
| 9 |
"""A general storage style for MPEG-4 tags.""" |
| 10 |
|
| 11 |
formats = ["MP4"] |
| 12 |
|
| 13 |
def serialize(self, value): |
| 14 |
value = super().serialize(value) |
| 15 |
if self.key.startswith("----:") and isinstance(value, str): |
| 16 |
value = value.encode("utf-8") |
| 17 |
return value |
| 18 |
|
| 19 |
|
| 20 |
class MP4TupleStorageStyle(MP4StorageStyle): |
| 21 |
"""A style for storing values as part of a pair of numbers in an |
| 22 |
MPEG-4 file. |
| 23 |
""" |
| 24 |
|
| 25 |
def __init__(self, key, index=0, **kwargs): |
| 26 |
super().__init__(key, **kwargs) |
| 27 |
self.index = index |
| 28 |
|
| 29 |
def deserialize(self, mutagen_value): |
| 30 |
items = mutagen_value or [] |
| 31 |
packing_length = 2 |
| 32 |
return list(items) + [0] * (packing_length - len(items)) |
| 33 |
|
| 34 |
def get(self, mutagen_file): |
| 35 |
value = super().get(mutagen_file)[self.index] |
| 36 |
if value == 0: |
| 37 |
# The values are always present and saved as integers. So we |
| 38 |
# assume that "0" indicates it is not set. |
| 39 |
return None |
| 40 |
else: |
| 41 |
return value |
| 42 |
|
| 43 |
def set(self, mutagen_file, value): |
| 44 |
if value is None: |
| 45 |
value = 0 |
| 46 |
items = self.deserialize(self.fetch(mutagen_file)) |
| 47 |
items[self.index] = int(value) |
| 48 |
self.store(mutagen_file, items) |
| 49 |
|
| 50 |
def delete(self, mutagen_file): |
| 51 |
if self.index == 0: |
| 52 |
super().delete(mutagen_file) |
| 53 |
else: |
| 54 |
self.set(mutagen_file, None) |
| 55 |
|
| 56 |
|
| 57 |
class MP4ListStorageStyle(ListStorageStyle, MP4StorageStyle): |
| 58 |
pass |
| 59 |
|
| 60 |
|
| 61 |
class MP4SoundCheckStorageStyle(SoundCheckStorageStyleMixin, MP4StorageStyle): |
| 62 |
def __init__(self, key, index=0, **kwargs): |
| 63 |
super().__init__(key, **kwargs) |
| 64 |
self.index = index |
| 65 |
|
| 66 |
|
| 67 |
class MP4BoolStorageStyle(MP4StorageStyle): |
| 68 |
"""A style for booleans in MPEG-4 files. (MPEG-4 has an atom type |
| 69 |
specifically for representing booleans.) |
| 70 |
""" |
| 71 |
|
| 72 |
def get(self, mutagen_file): |
| 73 |
try: |
| 74 |
return mutagen_file[self.key] |
| 75 |
except KeyError: |
| 76 |
return None |
| 77 |
|
| 78 |
def get_list(self, mutagen_file): |
| 79 |
raise NotImplementedError("MP4 bool storage does not support lists") |
| 80 |
|
| 81 |
def set(self, mutagen_file, value): |
| 82 |
mutagen_file[self.key] = value |
| 83 |
|
| 84 |
def set_list(self, mutagen_file, values): |
| 85 |
raise NotImplementedError("MP4 bool storage does not support lists") |
| 86 |
|
| 87 |
|
| 88 |
class MP4ImageStorageStyle(MP4ListStorageStyle): |
| 89 |
"""Store images as MPEG-4 image atoms. Values are `Image` objects.""" |
| 90 |
|
| 91 |
def __init__(self, **kwargs): |
| 92 |
super().__init__(key="covr", **kwargs) |
| 93 |
|
| 94 |
def deserialize(self, data): |
| 95 |
return Image(data) |
| 96 |
|
| 97 |
def serialize(self, image): |
| 98 |
if image.mime_type == "image/png": |
| 99 |
kind = mutagen.mp4.MP4Cover.FORMAT_PNG |
| 100 |
elif image.mime_type == "image/jpeg": |
| 101 |
kind = mutagen.mp4.MP4Cover.FORMAT_JPEG |
| 102 |
else: |
| 103 |
raise ValueError("MP4 files only supports PNG and JPEG images") |
| 104 |
return mutagen.mp4.MP4Cover(image.data, kind) |