| 1 |
# Copyright (C) 2008-2010 Adam Olsen |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
|
| 28 |
from xl.metadata._base import BaseFormat, CoverImage |
| 29 |
from mutagen import mp4 |
| 30 |
|
| 31 |
|
| 32 |
class MP4Format(BaseFormat): |
| 33 |
MutagenType = mp4.MP4 |
| 34 |
tag_mapping = { |
| 35 |
# fmt: off |
| 36 |
'title': '\xa9nam', |
| 37 |
'artist': '\xa9ART', |
| 38 |
'albumartist': '\x61ART', |
| 39 |
'album': '\xa9alb', |
| 40 |
'composer': '\xa9wrt', |
| 41 |
'genre': '\xa9gen', |
| 42 |
'lyrics': '\xa9lyr', |
| 43 |
'encodedby': '\xa9too', |
| 44 |
'date': '\xa9day', |
| 45 |
'tracknumber': 'trkn', |
| 46 |
'discnumber': 'disk', |
| 47 |
'copyright': 'cprt', |
| 48 |
'bpm': 'tmpo', |
| 49 |
'grouping': '\xa9grp', |
| 50 |
'comment': '\xa9cmt', |
| 51 |
'originaldate': '----:com.apple.iTunes:ORIGYEAR', |
| 52 |
'cover': 'covr', |
| 53 |
'language': '----:com.apple.iTunes:LANGUAGE', |
| 54 |
'__rating': 'rate', |
| 55 |
# fmt: on |
| 56 |
} |
| 57 |
others = False |
| 58 |
writable = True |
| 59 |
|
| 60 |
def _get_tag(self, f, name): |
| 61 |
if name not in f: |
| 62 |
return [] |
| 63 |
elif name == 'covr': |
| 64 |
ret = [] |
| 65 |
for value in f[name]: |
| 66 |
if value.imageformat == mp4.MP4Cover.FORMAT_PNG: |
| 67 |
mime = 'image/png' |
| 68 |
else: |
| 69 |
mime = 'image/jpeg' |
| 70 |
ret.append(CoverImage(type=None, desc=None, mime=mime, data=value)) |
| 71 |
return ret |
| 72 |
elif name in ['trkn', 'disk']: |
| 73 |
ret = [] |
| 74 |
for value in f[name]: |
| 75 |
ret.append("%d/%d" % (value[0], value[1])) |
| 76 |
return ret |
| 77 |
elif name == 'rate': |
| 78 |
# Rating Stars |
| 79 |
data = int(f['rate'][0]) |
| 80 |
rating = self._rating_to_stars(data) |
| 81 |
return [str(rating)] |
| 82 |
else: |
| 83 |
return [t for t in f[name]] |
| 84 |
|
| 85 |
def _set_tag(self, f, name, value): |
| 86 |
if not isinstance(value, list): |
| 87 |
value = [value] |
| 88 |
if name in ['trkn', 'disk']: |
| 89 |
try: |
| 90 |
f[name] = [] |
| 91 |
for val in value: |
| 92 |
tmp = map(int, val.split('/')) |
| 93 |
f[name].append(tuple(tmp)) |
| 94 |
except (TypeError, ValueError): |
| 95 |
pass |
| 96 |
elif name == 'covr': |
| 97 |
f[name] = [] |
| 98 |
|
| 99 |
for val in value: |
| 100 |
if val.mime == 'image/jpeg': |
| 101 |
f[name].append(mp4.MP4Cover(val.data, mp4.MP4Cover.FORMAT_JPEG)) |
| 102 |
elif val.mime == 'image/png': |
| 103 |
f[name].append(mp4.MP4Cover(val.data, mp4.MP4Cover.FORMAT_JPEG)) |
| 104 |
else: |
| 105 |
raise ValueError( |
| 106 |
'MP4 does not support cover image type %s' % val.type |
| 107 |
) |
| 108 |
elif name == 'tmpo': |
| 109 |
f[name] = [int(v) for v in value] |
| 110 |
elif name == '----:com.apple.iTunes:ORIGYEAR': |
| 111 |
f[name] = [str(v) for v in value] |
| 112 |
elif name == 'rate': |
| 113 |
# Rating Stars |
| 114 |
rating = self._stars_to_rating(int(value[0])) |
| 115 |
f[name] = [str(rating)] |
| 116 |
else: |
| 117 |
f[name] = value |
| 118 |
|
| 119 |
|
| 120 |
# vim: et sts=4 sw=4 |