/mutagen/id3.py

https://github.com/amahi/pytivo · Python · 2080 lines · 1815 code · 99 blank · 166 comment · 278 complexity · e76361874a5520350b0f584814fc4cf1 MD5 · raw file

  1. # id3 support for mutagen
  2. # Copyright (C) 2005 Michael Urman
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of version 2 of the GNU General Public License as
  6. # published by the Free Software Foundation.
  7. #
  8. # $Id: id3.py 4285 2008-09-06 08:01:31Z piman $
  9. """ID3v2 reading and writing.
  10. This is based off of the following references:
  11. http://www.id3.org/id3v2.4.0-structure.txt
  12. http://www.id3.org/id3v2.4.0-frames.txt
  13. http://www.id3.org/id3v2.3.0.html
  14. http://www.id3.org/id3v2-00.txt
  15. http://www.id3.org/id3v1.html
  16. Its largest deviation from the above (versions 2.3 and 2.2) is that it
  17. will not interpret the / characters as a separator, and will almost
  18. always accept null separators to generate multi-valued text frames.
  19. Because ID3 frame structure differs between frame types, each frame is
  20. implemented as a different class (e.g. TIT2 as mutagen.id3.TIT2). Each
  21. frame's documentation contains a list of its attributes.
  22. Since this file's documentation is a little unwieldy, you are probably
  23. interested in the 'ID3' class to start with.
  24. """
  25. __all__ = ['ID3', 'ID3FileType', 'Frames', 'Open', 'delete']
  26. import struct
  27. from struct import unpack, pack, error as StructError
  28. from zlib import error as zlibError
  29. from warnings import warn
  30. import mutagen
  31. from mutagen._util import insert_bytes, delete_bytes, DictProxy
  32. class error(Exception): pass
  33. class ID3NoHeaderError(error, ValueError): pass
  34. class ID3BadUnsynchData(error, ValueError): pass
  35. class ID3BadCompressedData(error, ValueError): pass
  36. class ID3TagError(error, ValueError): pass
  37. class ID3UnsupportedVersionError(error, NotImplementedError): pass
  38. class ID3EncryptionUnsupportedError(error, NotImplementedError): pass
  39. class ID3JunkFrameError(error, ValueError): pass
  40. class ID3Warning(error, UserWarning): pass
  41. def is_valid_frame_id(frame_id):
  42. return frame_id.isalnum() and frame_id.isupper()
  43. class ID3(DictProxy, mutagen.Metadata):
  44. """A file with an ID3v2 tag.
  45. Attributes:
  46. version -- ID3 tag version as a tuple
  47. unknown_frames -- raw frame data of any unknown frames found
  48. size -- the total size of the ID3 tag, including the header
  49. """
  50. PEDANTIC = True
  51. version = (2, 4, 0)
  52. filename = None
  53. size = 0
  54. __flags = 0
  55. __readbytes = 0
  56. __crc = None
  57. def __init__(self, *args, **kwargs):
  58. self.unknown_frames = []
  59. super(ID3, self).__init__(*args, **kwargs)
  60. def __fullread(self, size):
  61. try:
  62. if size < 0:
  63. raise ValueError('Requested bytes (%s) less than zero' % size)
  64. if size > self.__filesize:
  65. raise EOFError('Requested %#x of %#x (%s)' %
  66. (long(size), long(self.__filesize), self.filename))
  67. except AttributeError: pass
  68. data = self.__fileobj.read(size)
  69. if len(data) != size: raise EOFError
  70. self.__readbytes += size
  71. return data
  72. def load(self, filename, known_frames=None, translate=True):
  73. """Load tags from a filename.
  74. Keyword arguments:
  75. filename -- filename to load tag data from
  76. known_frames -- dict mapping frame IDs to Frame objects
  77. translate -- Update all tags to ID3v2.4 internally. Mutagen is
  78. only capable of writing ID3v2.4 tags, so if you
  79. intend to save, this must be true.
  80. Example of loading a custom frame:
  81. my_frames = dict(mutagen.id3.Frames)
  82. class XMYF(Frame): ...
  83. my_frames["XMYF"] = XMYF
  84. mutagen.id3.ID3(filename, known_frames=my_frames)
  85. """
  86. from os.path import getsize
  87. self.filename = filename
  88. self.__known_frames = known_frames
  89. self.__fileobj = open(filename, 'rb')
  90. self.__filesize = getsize(filename)
  91. try:
  92. try:
  93. self.__load_header()
  94. except EOFError:
  95. self.size = 0
  96. raise ID3NoHeaderError("%s: too small (%d bytes)" %(
  97. filename, self.__filesize))
  98. except (ID3NoHeaderError, ID3UnsupportedVersionError), err:
  99. self.size = 0
  100. import sys
  101. stack = sys.exc_info()[2]
  102. try: self.__fileobj.seek(-128, 2)
  103. except EnvironmentError: raise err, None, stack
  104. else:
  105. frames = ParseID3v1(self.__fileobj.read(128))
  106. if frames is not None:
  107. self.version = (1, 1)
  108. map(self.add, frames.values())
  109. else: raise err, None, stack
  110. else:
  111. frames = self.__known_frames
  112. if frames is None:
  113. if (2,3,0) <= self.version: frames = Frames
  114. elif (2,2,0) <= self.version: frames = Frames_2_2
  115. data = self.__fullread(self.size - 10)
  116. for frame in self.__read_frames(data, frames=frames):
  117. if isinstance(frame, Frame): self.add(frame)
  118. else: self.unknown_frames.append(frame)
  119. finally:
  120. self.__fileobj.close()
  121. del self.__fileobj
  122. del self.__filesize
  123. if translate:
  124. self.update_to_v24()
  125. def getall(self, key):
  126. """Return all frames with a given name (the list may be empty).
  127. This is best explained by examples:
  128. id3.getall('TIT2') == [id3['TIT2']]
  129. id3.getall('TTTT') == []
  130. id3.getall('TXXX') == [TXXX(desc='woo', text='bar'),
  131. TXXX(desc='baz', text='quuuux'), ...]
  132. Since this is based on the frame's HashKey, which is
  133. colon-separated, you can use it to do things like
  134. getall('COMM:MusicMatch') or getall('TXXX:QuodLibet:').
  135. """
  136. if key in self: return [self[key]]
  137. else:
  138. key = key + ":"
  139. return [v for s,v in self.items() if s.startswith(key)]
  140. def delall(self, key):
  141. """Delete all tags of a given kind; see getall."""
  142. if key in self: del(self[key])
  143. else:
  144. key = key + ":"
  145. for k in filter(lambda s: s.startswith(key), self.keys()):
  146. del(self[k])
  147. def setall(self, key, values):
  148. """Delete frames of the given type and add frames in 'values'."""
  149. self.delall(key)
  150. for tag in values:
  151. self[tag.HashKey] = tag
  152. def pprint(self):
  153. """Return tags in a human-readable format.
  154. "Human-readable" is used loosely here. The format is intended
  155. to mirror that used for Vorbis or APEv2 output, e.g.
  156. TIT2=My Title
  157. However, ID3 frames can have multiple keys:
  158. POPM=user@example.org=3 128/255
  159. """
  160. frames = list(map(Frame.pprint, self.values()))
  161. frames.sort()
  162. return "\n".join(frames)
  163. def loaded_frame(self, tag):
  164. """Deprecated; use the add method."""
  165. # turn 2.2 into 2.3/2.4 tags
  166. if len(type(tag).__name__) == 3: tag = type(tag).__base__(tag)
  167. self[tag.HashKey] = tag
  168. # add = loaded_frame (and vice versa) break applications that
  169. # expect to be able to override loaded_frame (e.g. Quod Libet),
  170. # as does making loaded_frame call add.
  171. def add(self, frame):
  172. """Add a frame to the tag."""
  173. return self.loaded_frame(frame)
  174. def __load_header(self):
  175. fn = self.filename
  176. data = self.__fullread(10)
  177. id3, vmaj, vrev, flags, size = unpack('>3sBBB4s', data)
  178. self.__flags = flags
  179. self.size = BitPaddedInt(size) + 10
  180. self.version = (2, vmaj, vrev)
  181. if id3 != 'ID3':
  182. raise ID3NoHeaderError("'%s' doesn't start with an ID3 tag" % fn)
  183. if vmaj not in [2, 3, 4]:
  184. raise ID3UnsupportedVersionError("'%s' ID3v2.%d not supported"
  185. % (fn, vmaj))
  186. if self.PEDANTIC:
  187. if (2,4,0) <= self.version and (flags & 0x0f):
  188. raise ValueError("'%s' has invalid flags %#02x" % (fn, flags))
  189. elif (2,3,0) <= self.version < (2,4,0) and (flags & 0x1f):
  190. raise ValueError("'%s' has invalid flags %#02x" % (fn, flags))
  191. if self.f_extended:
  192. extsize = self.__fullread(4)
  193. if extsize in Frames:
  194. # Some tagger sets the extended header flag but
  195. # doesn't write an extended header; in this case, the
  196. # ID3 data follows immediately. Since no extended
  197. # header is going to be long enough to actually match
  198. # a frame, and if it's *not* a frame we're going to be
  199. # completely lost anyway, this seems to be the most
  200. # correct check.
  201. # http://code.google.com/p/quodlibet/issues/detail?id=126
  202. self.__flags ^= 0x40
  203. self.__extsize = 0
  204. self.__fileobj.seek(-4, 1)
  205. self.__readbytes -= 4
  206. elif self.version >= (2,4,0):
  207. # "Where the 'Extended header size' is the size of the whole
  208. # extended header, stored as a 32 bit synchsafe integer."
  209. self.__extsize = BitPaddedInt(extsize) - 4
  210. else:
  211. # "Where the 'Extended header size', currently 6 or 10 bytes,
  212. # excludes itself."
  213. self.__extsize = unpack('>L', extsize)[0]
  214. if self.__extsize:
  215. self.__extdata = self.__fullread(self.__extsize)
  216. else:
  217. self.__extdata = ""
  218. def __determine_bpi(self, data, frames, EMPTY="\x00" * 10):
  219. if self.version < (2, 4, 0):
  220. return int
  221. # have to special case whether to use bitpaddedints here
  222. # spec says to use them, but iTunes has it wrong
  223. # count number of tags found as BitPaddedInt and how far past
  224. o = 0
  225. asbpi = 0
  226. while o < len(data) - 10:
  227. part = data[o:o + 10]
  228. if part == EMPTY:
  229. bpioff = -((len(data) - o) % 10)
  230. break
  231. name, size, flags = unpack('>4sLH', part)
  232. size = BitPaddedInt(size)
  233. o += 10 + size
  234. if name in frames:
  235. asbpi += 1
  236. else:
  237. bpioff = o - len(data)
  238. # count number of tags found as int and how far past
  239. o = 0
  240. asint = 0
  241. while o < len(data) - 10:
  242. part = data[o:o + 10]
  243. if part == EMPTY:
  244. intoff = -((len(data) - o) % 10)
  245. break
  246. name, size, flags = unpack('>4sLH', part)
  247. o += 10 + size
  248. if name in frames:
  249. asint += 1
  250. else:
  251. intoff = o - len(data)
  252. # if more tags as int, or equal and bpi is past and int is not
  253. if asint > asbpi or (asint == asbpi and (bpioff >= 1 and intoff <= 1)):
  254. return int
  255. return BitPaddedInt
  256. def __read_frames(self, data, frames):
  257. if self.version < (2,4,0) and self.f_unsynch:
  258. try: data = unsynch.decode(data)
  259. except ValueError: pass
  260. if (2,3,0) <= self.version:
  261. bpi = self.__determine_bpi(data, frames)
  262. while data:
  263. header = data[:10]
  264. try: name, size, flags = unpack('>4sLH', header)
  265. except struct.error: return # not enough header
  266. if name.strip('\x00') == '': return
  267. size = bpi(size)
  268. framedata = data[10:10+size]
  269. data = data[10+size:]
  270. if size == 0: continue # drop empty frames
  271. try: tag = frames[name]
  272. except KeyError:
  273. if is_valid_frame_id(name): yield header + framedata
  274. else:
  275. try: yield self.__load_framedata(tag, flags, framedata)
  276. except NotImplementedError: yield header + framedata
  277. except ID3JunkFrameError: pass
  278. elif (2,2,0) <= self.version:
  279. while data:
  280. header = data[0:6]
  281. try: name, size = unpack('>3s3s', header)
  282. except struct.error: return # not enough header
  283. size, = struct.unpack('>L', '\x00'+size)
  284. if name.strip('\x00') == '': return
  285. framedata = data[6:6+size]
  286. data = data[6+size:]
  287. if size == 0: continue # drop empty frames
  288. try: tag = frames[name]
  289. except KeyError:
  290. if is_valid_frame_id(name): yield header + framedata
  291. else:
  292. try: yield self.__load_framedata(tag, 0, framedata)
  293. except NotImplementedError: yield header + framedata
  294. except ID3JunkFrameError: pass
  295. def __load_framedata(self, tag, flags, framedata):
  296. return tag.fromData(self, flags, framedata)
  297. f_unsynch = property(lambda s: bool(s.__flags & 0x80))
  298. f_extended = property(lambda s: bool(s.__flags & 0x40))
  299. f_experimental = property(lambda s: bool(s.__flags & 0x20))
  300. f_footer = property(lambda s: bool(s.__flags & 0x10))
  301. #f_crc = property(lambda s: bool(s.__extflags & 0x8000))
  302. def save(self, filename=None, v1=1):
  303. """Save changes to a file.
  304. If no filename is given, the one most recently loaded is used.
  305. Keyword arguments:
  306. v1 -- if 0, ID3v1 tags will be removed
  307. if 1, ID3v1 tags will be updated but not added
  308. if 2, ID3v1 tags will be created and/or updated
  309. The lack of a way to update only an ID3v1 tag is intentional.
  310. """
  311. # Sort frames by 'importance'
  312. order = ["TIT2", "TPE1", "TRCK", "TALB", "TPOS", "TDRC", "TCON"]
  313. order = dict(zip(order, range(len(order))))
  314. last = len(order)
  315. frames = self.items()
  316. frames.sort(lambda a, b: cmp(order.get(a[0][:4], last),
  317. order.get(b[0][:4], last)))
  318. framedata = [self.__save_frame(frame) for (key, frame) in frames]
  319. framedata.extend([data for data in self.unknown_frames
  320. if len(data) > 10])
  321. if not framedata:
  322. try:
  323. self.delete(filename)
  324. except EnvironmentError, err:
  325. from errno import ENOENT
  326. if err.errno != ENOENT: raise
  327. return
  328. framedata = ''.join(framedata)
  329. framesize = len(framedata)
  330. if filename is None: filename = self.filename
  331. try: f = open(filename, 'rb+')
  332. except IOError, err:
  333. from errno import ENOENT
  334. if err.errno != ENOENT: raise
  335. f = open(filename, 'ab') # create, then reopen
  336. f = open(filename, 'rb+')
  337. try:
  338. idata = f.read(10)
  339. try: id3, vmaj, vrev, flags, insize = unpack('>3sBBB4s', idata)
  340. except struct.error: id3, insize = '', 0
  341. insize = BitPaddedInt(insize)
  342. if id3 != 'ID3': insize = -10
  343. if insize >= framesize: outsize = insize
  344. else: outsize = (framesize + 1023) & ~0x3FF
  345. framedata += '\x00' * (outsize - framesize)
  346. framesize = BitPaddedInt.to_str(outsize, width=4)
  347. flags = 0
  348. header = pack('>3sBBB4s', 'ID3', 4, 0, flags, framesize)
  349. data = header + framedata
  350. if (insize < outsize):
  351. insert_bytes(f, outsize-insize, insize+10)
  352. f.seek(0)
  353. f.write(data)
  354. try:
  355. f.seek(-128, 2)
  356. except IOError, err:
  357. # If the file is too small, that's OK - it just means
  358. # we're certain it doesn't have a v1 tag.
  359. from errno import EINVAL
  360. if err.errno != EINVAL:
  361. # If we failed to see for some other reason, bail out.
  362. raise
  363. # Since we're sure this isn't a v1 tag, don't read it.
  364. f.seek(0, 2)
  365. data = f.read(128)
  366. try:
  367. idx = data.index("TAG")
  368. except ValueError:
  369. offset = 0
  370. has_v1 = False
  371. else:
  372. offset = idx - len(data)
  373. has_v1 = True
  374. f.seek(offset, 2)
  375. if v1 == 1 and has_v1 or v1 == 2:
  376. f.write(MakeID3v1(self))
  377. else:
  378. f.truncate()
  379. finally:
  380. f.close()
  381. def delete(self, filename=None, delete_v1=True, delete_v2=True):
  382. """Remove tags from a file.
  383. If no filename is given, the one most recently loaded is used.
  384. Keyword arguments:
  385. delete_v1 -- delete any ID3v1 tag
  386. delete_v2 -- delete any ID3v2 tag
  387. """
  388. if filename is None:
  389. filename = self.filename
  390. delete(filename, delete_v1, delete_v2)
  391. self.clear()
  392. def __save_frame(self, frame):
  393. flags = 0
  394. if self.PEDANTIC and isinstance(frame, TextFrame):
  395. if len(str(frame)) == 0: return ''
  396. framedata = frame._writeData()
  397. usize = len(framedata)
  398. if usize > 2048:
  399. # Disabled as this causes iTunes and other programs
  400. # to fail to find these frames, which usually includes
  401. # e.g. APIC.
  402. #framedata = BitPaddedInt.to_str(usize) + framedata.encode('zlib')
  403. #flags |= Frame.FLAG24_COMPRESS | Frame.FLAG24_DATALEN
  404. pass
  405. datasize = BitPaddedInt.to_str(len(framedata), width=4)
  406. header = pack('>4s4sH', type(frame).__name__, datasize, flags)
  407. return header + framedata
  408. def update_to_v24(self):
  409. """Convert older tags into an ID3v2.4 tag.
  410. This updates old ID3v2 frames to ID3v2.4 ones (e.g. TYER to
  411. TDRC). If you intend to save tags, you must call this function
  412. at some point; it is called by default when loading the tag.
  413. """
  414. if self.version < (2,3,0): del self.unknown_frames[:]
  415. # unsafe to write
  416. # TDAT, TYER, and TIME have been turned into TDRC.
  417. try:
  418. if str(self.get("TYER", "")).strip("\x00"):
  419. date = str(self.pop("TYER"))
  420. if str(self.get("TDAT", "")).strip("\x00"):
  421. dat = str(self.pop("TDAT"))
  422. date = "%s-%s-%s" % (date, dat[2:], dat[:2])
  423. if str(self.get("TIME", "")).strip("\x00"):
  424. time = str(self.pop("TIME"))
  425. date += "T%s:%s:00" % (time[:2], time[2:])
  426. if "TDRC" not in self:
  427. self.add(TDRC(encoding=0, text=date))
  428. except UnicodeDecodeError:
  429. # Old ID3 tags have *lots* of Unicode problems, so if TYER
  430. # is bad, just chuck the frames.
  431. pass
  432. # TORY can be the first part of a TDOR.
  433. if "TORY" in self:
  434. f = self.pop("TORY")
  435. if "TDOR" not in self:
  436. try:
  437. self.add(TDOR(encoding=0, text=str(f)))
  438. except UnicodeDecodeError:
  439. pass
  440. # IPLS is now TIPL.
  441. if "IPLS" in self:
  442. f = self.pop("IPLS")
  443. if "TIPL" not in self:
  444. self.add(TIPL(encoding=f.encoding, people=f.people))
  445. if "TCON" in self:
  446. # Get rid of "(xx)Foobr" format.
  447. self["TCON"].genres = self["TCON"].genres
  448. if self.version < (2, 3):
  449. # ID3v2.2 PIC frames are slightly different.
  450. pics = self.getall("APIC")
  451. mimes = { "PNG": "image/png", "JPG": "image/jpeg" }
  452. self.delall("APIC")
  453. for pic in pics:
  454. newpic = APIC(
  455. encoding=pic.encoding, mime=mimes.get(pic.mime, pic.mime),
  456. type=pic.type, desc=pic.desc, data=pic.data)
  457. self.add(newpic)
  458. # ID3v2.2 LNK frames are just way too different to upgrade.
  459. self.delall("LINK")
  460. # These can't be trivially translated to any ID3v2.4 tags, or
  461. # should have been removed already.
  462. for key in ["RVAD", "EQUA", "TRDA", "TSIZ", "TDAT", "TIME", "CRM"]:
  463. if key in self: del(self[key])
  464. def delete(filename, delete_v1=True, delete_v2=True):
  465. """Remove tags from a file.
  466. Keyword arguments:
  467. delete_v1 -- delete any ID3v1 tag
  468. delete_v2 -- delete any ID3v2 tag
  469. """
  470. f = open(filename, 'rb+')
  471. if delete_v1:
  472. try:
  473. f.seek(-128, 2)
  474. except IOError: pass
  475. else:
  476. if f.read(3) == "TAG":
  477. f.seek(-128, 2)
  478. f.truncate()
  479. # technically an insize=0 tag is invalid, but we delete it anyway
  480. # (primarily because we used to write it)
  481. if delete_v2:
  482. f.seek(0, 0)
  483. idata = f.read(10)
  484. try: id3, vmaj, vrev, flags, insize = unpack('>3sBBB4s', idata)
  485. except struct.error: id3, insize = '', -1
  486. insize = BitPaddedInt(insize)
  487. if id3 == 'ID3' and insize >= 0:
  488. delete_bytes(f, insize + 10, 0)
  489. class BitPaddedInt(int):
  490. def __new__(cls, value, bits=7, bigendian=True):
  491. "Strips 8-bits bits out of every byte"
  492. mask = (1<<(bits))-1
  493. if isinstance(value, (int, long)):
  494. bytes = []
  495. while value:
  496. bytes.append(value & ((1<<bits)-1))
  497. value = value >> 8
  498. if isinstance(value, str):
  499. bytes = [ord(byte) & mask for byte in value]
  500. if bigendian: bytes.reverse()
  501. numeric_value = 0
  502. for shift, byte in zip(range(0, len(bytes)*bits, bits), bytes):
  503. numeric_value += byte << shift
  504. if isinstance(numeric_value, long):
  505. self = long.__new__(BitPaddedLong, numeric_value)
  506. else:
  507. self = int.__new__(BitPaddedInt, numeric_value)
  508. self.bits = bits
  509. self.bigendian = bigendian
  510. return self
  511. def as_str(value, bits=7, bigendian=True, width=4):
  512. bits = getattr(value, 'bits', bits)
  513. bigendian = getattr(value, 'bigendian', bigendian)
  514. value = int(value)
  515. mask = (1<<bits)-1
  516. bytes = []
  517. while value:
  518. bytes.append(value & mask)
  519. value = value >> bits
  520. # PCNT and POPM use growing integers of at least 4 bytes as counters.
  521. if width == -1: width = max(4, len(bytes))
  522. if len(bytes) > width:
  523. raise ValueError, 'Value too wide (%d bytes)' % len(bytes)
  524. else: bytes.extend([0] * (width-len(bytes)))
  525. if bigendian: bytes.reverse()
  526. return ''.join(map(chr, bytes))
  527. to_str = staticmethod(as_str)
  528. class BitPaddedLong(long):
  529. def as_str(value, bits=7, bigendian=True, width=4):
  530. return BitPaddedInt.to_str(value, bits, bigendian, width)
  531. to_str = staticmethod(as_str)
  532. class unsynch(object):
  533. def decode(value):
  534. output = []
  535. safe = True
  536. append = output.append
  537. for val in value:
  538. if safe:
  539. append(val)
  540. safe = val != '\xFF'
  541. else:
  542. if val >= '\xE0': raise ValueError('invalid sync-safe string')
  543. elif val != '\x00': append(val)
  544. safe = True
  545. if not safe: raise ValueError('string ended unsafe')
  546. return ''.join(output)
  547. decode = staticmethod(decode)
  548. def encode(value):
  549. output = []
  550. safe = True
  551. append = output.append
  552. for val in value:
  553. if safe:
  554. append(val)
  555. if val == '\xFF': safe = False
  556. elif val == '\x00' or val >= '\xE0':
  557. append('\x00')
  558. append(val)
  559. safe = val != '\xFF'
  560. else:
  561. append(val)
  562. safe = True
  563. if not safe: append('\x00')
  564. return ''.join(output)
  565. encode = staticmethod(encode)
  566. class Spec(object):
  567. def __init__(self, name): self.name = name
  568. def __hash__(self): raise TypeError("Spec objects are unhashable")
  569. class ByteSpec(Spec):
  570. def read(self, frame, data): return ord(data[0]), data[1:]
  571. def write(self, frame, value): return chr(value)
  572. def validate(self, frame, value): return value
  573. class IntegerSpec(Spec):
  574. def read(self, frame, data):
  575. return int(BitPaddedInt(data, bits=8)), ''
  576. def write(self, frame, value):
  577. return BitPaddedInt.to_str(value, bits=8, width=-1)
  578. def validate(self, frame, value):
  579. return value
  580. class SizedIntegerSpec(Spec):
  581. def __init__(self, name, size):
  582. self.name, self.__sz = name, size
  583. def read(self, frame, data):
  584. return int(BitPaddedInt(data[:self.__sz], bits=8)), data[self.__sz:]
  585. def write(self, frame, value):
  586. return BitPaddedInt.to_str(value, bits=8, width=self.__sz)
  587. def validate(self, frame, value):
  588. return value
  589. class EncodingSpec(ByteSpec):
  590. def read(self, frame, data):
  591. enc, data = super(EncodingSpec, self).read(frame, data)
  592. if enc < 16: return enc, data
  593. else: return 0, chr(enc)+data
  594. def validate(self, frame, value):
  595. if 0 <= value <= 3: return value
  596. if value is None: return None
  597. raise ValueError, 'Invalid Encoding: %r' % value
  598. class StringSpec(Spec):
  599. def __init__(self, name, length):
  600. super(StringSpec, self).__init__(name)
  601. self.len = length
  602. def read(s, frame, data): return data[:s.len], data[s.len:]
  603. def write(s, frame, value):
  604. if value is None: return '\x00' * s.len
  605. else: return (str(value) + '\x00' * s.len)[:s.len]
  606. def validate(s, frame, value):
  607. if value is None: return None
  608. if isinstance(value, basestring) and len(value) == s.len: return value
  609. raise ValueError, 'Invalid StringSpec[%d] data: %r' % (s.len, value)
  610. class BinaryDataSpec(Spec):
  611. def read(self, frame, data): return data, ''
  612. def write(self, frame, value): return str(value)
  613. def validate(self, frame, value): return str(value)
  614. class EncodedTextSpec(Spec):
  615. # Okay, seriously. This is private and defined explicitly and
  616. # completely by the ID3 specification. You can't just add
  617. # encodings here however you want.
  618. _encodings = ( ('latin1', '\x00'), ('utf16', '\x00\x00'),
  619. ('utf_16_be', '\x00\x00'), ('utf8', '\x00') )
  620. def read(self, frame, data):
  621. enc, term = self._encodings[frame.encoding]
  622. ret = ''
  623. if len(term) == 1:
  624. if term in data:
  625. data, ret = data.split(term, 1)
  626. else:
  627. offset = -1
  628. try:
  629. while True:
  630. offset = data.index(term, offset+1)
  631. if offset & 1: continue
  632. data, ret = data[0:offset], data[offset+2:]; break
  633. except ValueError: pass
  634. if len(data) < len(term): return u'', ret
  635. return data.decode(enc), ret
  636. def write(self, frame, value):
  637. enc, term = self._encodings[frame.encoding]
  638. return value.encode(enc) + term
  639. def validate(self, frame, value): return unicode(value)
  640. class MultiSpec(Spec):
  641. def __init__(self, name, *specs, **kw):
  642. super(MultiSpec, self).__init__(name)
  643. self.specs = specs
  644. self.sep = kw.get('sep')
  645. def read(self, frame, data):
  646. values = []
  647. while data:
  648. record = []
  649. for spec in self.specs:
  650. value, data = spec.read(frame, data)
  651. record.append(value)
  652. if len(self.specs) != 1: values.append(record)
  653. else: values.append(record[0])
  654. return values, data
  655. def write(self, frame, value):
  656. data = []
  657. if len(self.specs) == 1:
  658. for v in value:
  659. data.append(self.specs[0].write(frame, v))
  660. else:
  661. for record in value:
  662. for v, s in zip(record, self.specs):
  663. data.append(s.write(frame, v))
  664. return ''.join(data)
  665. def validate(self, frame, value):
  666. if value is None: return []
  667. if self.sep and isinstance(value, basestring):
  668. value = value.split(self.sep)
  669. if isinstance(value, list):
  670. if len(self.specs) == 1:
  671. return [self.specs[0].validate(frame, v) for v in value]
  672. else:
  673. return [
  674. [s.validate(frame, v) for (v,s) in zip(val, self.specs)]
  675. for val in value ]
  676. raise ValueError, 'Invalid MultiSpec data: %r' % value
  677. class EncodedNumericTextSpec(EncodedTextSpec): pass
  678. class EncodedNumericPartTextSpec(EncodedTextSpec): pass
  679. class Latin1TextSpec(EncodedTextSpec):
  680. def read(self, frame, data):
  681. if '\x00' in data: data, ret = data.split('\x00',1)
  682. else: ret = ''
  683. return data.decode('latin1'), ret
  684. def write(self, data, value):
  685. return value.encode('latin1') + '\x00'
  686. def validate(self, frame, value): return unicode(value)
  687. class ID3TimeStamp(object):
  688. """A time stamp in ID3v2 format.
  689. This is a restricted form of the ISO 8601 standard; time stamps
  690. take the form of:
  691. YYYY-MM-DD HH:MM:SS
  692. Or some partial form (YYYY-MM-DD HH, YYYY, etc.).
  693. The 'text' attribute contains the raw text data of the time stamp.
  694. """
  695. import re
  696. def __init__(self, text):
  697. if isinstance(text, ID3TimeStamp): text = text.text
  698. self.text = text
  699. __formats = ['%04d'] + ['%02d'] * 5
  700. __seps = ['-', '-', ' ', ':', ':', 'x']
  701. def get_text(self):
  702. parts = [self.year, self.month, self.day,
  703. self.hour, self.minute, self.second]
  704. pieces = []
  705. for i, part in enumerate(iter(iter(parts).next, None)):
  706. pieces.append(self.__formats[i]%part + self.__seps[i])
  707. return u''.join(pieces)[:-1]
  708. def set_text(self, text, splitre=re.compile('[-T:/.]|\s+')):
  709. year, month, day, hour, minute, second = \
  710. splitre.split(text + ':::::')[:6]
  711. for a in 'year month day hour minute second'.split():
  712. try: v = int(locals()[a])
  713. except ValueError: v = None
  714. setattr(self, a, v)
  715. text = property(get_text, set_text, doc="ID3v2.4 date and time.")
  716. def __str__(self): return self.text
  717. def __repr__(self): return repr(self.text)
  718. def __cmp__(self, other): return cmp(self.text, other.text)
  719. __hash__ = object.__hash__
  720. def encode(self, *args): return self.text.encode(*args)
  721. class TimeStampSpec(EncodedTextSpec):
  722. def read(self, frame, data):
  723. value, data = super(TimeStampSpec, self).read(frame, data)
  724. return self.validate(frame, value), data
  725. def write(self, frame, data):
  726. return super(TimeStampSpec, self).write(frame,
  727. data.text.replace(' ', 'T'))
  728. def validate(self, frame, value):
  729. try: return ID3TimeStamp(value)
  730. except TypeError: raise ValueError, "Invalid ID3TimeStamp: %r" % value
  731. class ChannelSpec(ByteSpec):
  732. (OTHER, MASTER, FRONTRIGHT, FRONTLEFT, BACKRIGHT, BACKLEFT, FRONTCENTRE,
  733. BACKCENTRE, SUBWOOFER) = range(9)
  734. class VolumeAdjustmentSpec(Spec):
  735. def read(self, frame, data):
  736. value, = unpack('>h', data[0:2])
  737. return value/512.0, data[2:]
  738. def write(self, frame, value):
  739. return pack('>h', int(round(value * 512)))
  740. def validate(self, frame, value): return value
  741. class VolumePeakSpec(Spec):
  742. def read(self, frame, data):
  743. # http://bugs.xmms.org/attachment.cgi?id=113&action=view
  744. peak = 0
  745. bits = ord(data[0])
  746. bytes = min(4, (bits + 7) >> 3)
  747. # not enough frame data
  748. if bytes + 1 > len(data): raise ID3JunkFrameError
  749. shift = ((8 - (bits & 7)) & 7) + (4 - bytes) * 8
  750. for i in range(1, bytes+1):
  751. peak *= 256
  752. peak += ord(data[i])
  753. peak *= 2**shift
  754. return (float(peak) / (2**31-1)), data[1+bytes:]
  755. def write(self, frame, value):
  756. # always write as 16 bits for sanity.
  757. return "\x10" + pack('>H', int(round(value * 32768)))
  758. def validate(self, frame, value): return value
  759. class SynchronizedTextSpec(EncodedTextSpec):
  760. def read(self, frame, data):
  761. texts = []
  762. encoding, term = self._encodings[frame.encoding]
  763. while data:
  764. l = len(term)
  765. try:
  766. value_idx = data.index(term)
  767. except ValueError:
  768. raise ID3JunkFrameError
  769. value = data[:value_idx].decode(encoding)
  770. time, = struct.unpack(">I", data[value_idx+l:value_idx+l+4])
  771. texts.append((value, time))
  772. data = data[value_idx+l+4:]
  773. return texts, ""
  774. def write(self, frame, value):
  775. data = []
  776. encoding, term = self._encodings[frame.encoding]
  777. for text, time in frame.text:
  778. text = text.encode(encoding) + term
  779. data.append(text + struct.pack(">I", time))
  780. return "".join(data)
  781. def validate(self, frame, value):
  782. return value
  783. class KeyEventSpec(Spec):
  784. def read(self, frame, data):
  785. events = []
  786. while len(data) >= 5:
  787. events.append(struct.unpack(">bI", data[:5]))
  788. data = data[5:]
  789. return events, data
  790. def write(self, frame, value):
  791. return "".join([struct.pack(">bI", *event) for event in value])
  792. def validate(self, frame, value):
  793. return value
  794. class VolumeAdjustmentsSpec(Spec):
  795. # Not to be confused with VolumeAdjustmentSpec.
  796. def read(self, frame, data):
  797. adjustments = {}
  798. while len(data) >= 4:
  799. freq, adj = struct.unpack(">Hh", data[:4])
  800. data = data[4:]
  801. freq /= 2.0
  802. adj /= 512.0
  803. adjustments[freq] = adj
  804. adjustments = adjustments.items()
  805. adjustments.sort()
  806. return adjustments, data
  807. def write(self, frame, value):
  808. value.sort()
  809. return "".join([struct.pack(">Hh", int(freq * 2), int(adj * 512))
  810. for (freq, adj) in value])
  811. def validate(self, frame, value):
  812. return value
  813. class ASPIIndexSpec(Spec):
  814. def read(self, frame, data):
  815. if frame.b == 16:
  816. format = "H"
  817. size = 2
  818. elif frame.b == 8:
  819. format = "B"
  820. size = 1
  821. else:
  822. warn("invalid bit count in ASPI (%d)" % frame.b, ID3Warning)
  823. return [], data
  824. indexes = data[:frame.N * size]
  825. data = data[frame.N * size:]
  826. return list(struct.unpack(">" + format * frame.N, indexes)), data
  827. def write(self, frame, values):
  828. if frame.b == 16: format = "H"
  829. elif frame.b == 8: format = "B"
  830. else: raise ValueError("frame.b must be 8 or 16")
  831. return struct.pack(">" + format * frame.N, *values)
  832. def validate(self, frame, values):
  833. return values
  834. class Frame(object):
  835. """Fundamental unit of ID3 data.
  836. ID3 tags are split into frames. Each frame has a potentially
  837. different structure, and so this base class is not very featureful.
  838. """
  839. FLAG23_ALTERTAG = 0x8000
  840. FLAG23_ALTERFILE = 0x4000
  841. FLAG23_READONLY = 0x2000
  842. FLAG23_COMPRESS = 0x0080
  843. FLAG23_ENCRYPT = 0x0040
  844. FLAG23_GROUP = 0x0020
  845. FLAG24_ALTERTAG = 0x4000
  846. FLAG24_ALTERFILE = 0x2000
  847. FLAG24_READONLY = 0x1000
  848. FLAG24_GROUPID = 0x0040
  849. FLAG24_COMPRESS = 0x0008
  850. FLAG24_ENCRYPT = 0x0004
  851. FLAG24_UNSYNCH = 0x0002
  852. FLAG24_DATALEN = 0x0001
  853. _framespec = []
  854. def __init__(self, *args, **kwargs):
  855. if len(args)==1 and len(kwargs)==0 and isinstance(args[0], type(self)):
  856. other = args[0]
  857. for checker in self._framespec:
  858. val = checker.validate(self, getattr(other, checker.name))
  859. setattr(self, checker.name, val)
  860. else:
  861. for checker, val in zip(self._framespec, args):
  862. setattr(self, checker.name, checker.validate(self, val))
  863. for checker in self._framespec[len(args):]:
  864. validated = checker.validate(
  865. self, kwargs.get(checker.name, None))
  866. setattr(self, checker.name, validated)
  867. HashKey = property(
  868. lambda s: s.FrameID,
  869. doc="an internal key used to ensure frame uniqueness in a tag")
  870. FrameID = property(
  871. lambda s: type(s).__name__,
  872. doc="ID3v2 three or four character frame ID")
  873. def __repr__(self):
  874. """Python representation of a frame.
  875. The string returned is a valid Python expression to construct
  876. a copy of this frame.
  877. """
  878. kw = []
  879. for attr in self._framespec:
  880. kw.append('%s=%r' % (attr.name, getattr(self, attr.name)))
  881. return '%s(%s)' % (type(self).__name__, ', '.join(kw))
  882. def _readData(self, data):
  883. odata = data
  884. for reader in self._framespec:
  885. if len(data):
  886. try: value, data = reader.read(self, data)
  887. except UnicodeDecodeError:
  888. raise ID3JunkFrameError
  889. else: raise ID3JunkFrameError
  890. setattr(self, reader.name, value)
  891. if data.strip('\x00'):
  892. warn('Leftover data: %s: %r (from %r)' % (
  893. type(self).__name__, data, odata),
  894. ID3Warning)
  895. def _writeData(self):
  896. data = []
  897. for writer in self._framespec:
  898. data.append(writer.write(self, getattr(self, writer.name)))
  899. return ''.join(data)
  900. def pprint(self):
  901. """Return a human-readable representation of the frame."""
  902. return "%s=%s" % (type(self).__name__, self._pprint())
  903. def _pprint(self):
  904. return "[unrepresentable data]"
  905. def fromData(cls, id3, tflags, data):
  906. """Construct this ID3 frame from raw string data."""
  907. if (2,4,0) <= id3.version:
  908. if tflags & (Frame.FLAG24_COMPRESS | Frame.FLAG24_DATALEN):
  909. # The data length int is syncsafe in 2.4 (but not 2.3).
  910. # However, we don't actually need the data length int,
  911. # except to work around a QL 0.12 bug, and in that case
  912. # all we need are the raw bytes.
  913. datalen_bytes = data[:4]
  914. data = data[4:]
  915. if tflags & Frame.FLAG24_UNSYNCH or id3.f_unsynch:
  916. try: data = unsynch.decode(data)
  917. except ValueError, err:
  918. if id3.PEDANTIC:
  919. raise ID3BadUnsynchData, '%s: %r' % (err, data)
  920. if tflags & Frame.FLAG24_ENCRYPT:
  921. raise ID3EncryptionUnsupportedError
  922. if tflags & Frame.FLAG24_COMPRESS:
  923. try: data = data.decode('zlib')
  924. except zlibError, err:
  925. # the initial mutagen that went out with QL 0.12 did not
  926. # write the 4 bytes of uncompressed size. Compensate.
  927. data = datalen_bytes + data
  928. try: data = data.decode('zlib')
  929. except zlibError, err:
  930. if id3.PEDANTIC:
  931. raise ID3BadCompressedData, '%s: %r' % (err, data)
  932. elif (2,3,0) <= id3.version:
  933. if tflags & Frame.FLAG23_COMPRESS:
  934. usize, = unpack('>L', data[:4])
  935. data = data[4:]
  936. if tflags & Frame.FLAG23_ENCRYPT:
  937. raise ID3EncryptionUnsupportedError
  938. if tflags & Frame.FLAG23_COMPRESS:
  939. try: data = data.decode('zlib')
  940. except zlibError, err:
  941. if id3.PEDANTIC:
  942. raise ID3BadCompressedData, '%s: %r' % (err, data)
  943. frame = cls()
  944. frame._rawdata = data
  945. frame._flags = tflags
  946. frame._readData(data)
  947. return frame
  948. fromData = classmethod(fromData)
  949. def __hash__(self):
  950. raise TypeError("Frame objects are unhashable")
  951. class FrameOpt(Frame):
  952. """A frame with optional parts.
  953. Some ID3 frames have optional data; this class extends Frame to
  954. provide support for those parts.
  955. """
  956. _optionalspec = []
  957. def __init__(self, *args, **kwargs):
  958. super(FrameOpt, self).__init__(*args, **kwargs)
  959. for spec in self._optionalspec:
  960. if spec.name in kwargs:
  961. validated = spec.validate(self, kwargs[spec.name])
  962. setattr(self, spec.name, validated)
  963. else: break
  964. def _readData(self, data):
  965. odata = data
  966. for reader in self._framespec:
  967. if len(data): value, data = reader.read(self, data)
  968. else: raise ID3JunkFrameError
  969. setattr(self, reader.name, value)
  970. if data:
  971. for reader in self._optionalspec:
  972. if len(data): value, data = reader.read(self, data)
  973. else: break
  974. setattr(self, reader.name, value)
  975. if data.strip('\x00'):
  976. warn('Leftover data: %s: %r (from %r)' % (
  977. type(self).__name__, data, odata),
  978. ID3Warning)
  979. def _writeData(self):
  980. data = []
  981. for writer in self._framespec:
  982. data.append(writer.write(self, getattr(self, writer.name)))
  983. for writer in self._optionalspec:
  984. try: data.append(writer.write(self, getattr(self, writer.name)))
  985. except AttributeError: break
  986. return ''.join(data)
  987. def __repr__(self):
  988. kw = []
  989. for attr in self._framespec:
  990. kw.append('%s=%r' % (attr.name, getattr(self, attr.name)))
  991. for attr in self._optionalspec:
  992. if hasattr(self, attr.name):
  993. kw.append('%s=%r' % (attr.name, getattr(self, attr.name)))
  994. return '%s(%s)' % (type(self).__name__, ', '.join(kw))
  995. class TextFrame(Frame):
  996. """Text strings.
  997. Text frames support casts to unicode or str objects, as well as
  998. list-like indexing, extend, and append.
  999. Iterating over a TextFrame iterates over its strings, not its
  1000. characters.
  1001. Text frames have a 'text' attribute which is the list of strings,
  1002. and an 'encoding' attribute; 0 for ISO-8859 1, 1 UTF-16, 2 for
  1003. UTF-16BE, and 3 for UTF-8. If you don't want to worry about
  1004. encodings, just set it to 3.
  1005. """
  1006. _framespec = [ EncodingSpec('encoding'),
  1007. MultiSpec('text', EncodedTextSpec('text'), sep=u'\u0000') ]
  1008. def __str__(self): return self.__unicode__().encode('utf-8')
  1009. def __unicode__(self): return u'\u0000'.join(self.text)
  1010. def __eq__(self, other):
  1011. if isinstance(other, str): return str(self) == other
  1012. elif isinstance(other, unicode): return unicode(self) == other
  1013. return self.text == other
  1014. __hash__ = Frame.__hash__
  1015. def __getitem__(self, item): return self.text[item]
  1016. def __iter__(self): return iter(self.text)
  1017. def append(self, value): return self.text.append(value)
  1018. def extend(self, value): return self.text.extend(value)
  1019. def _pprint(self): return " / ".join(self.text)
  1020. class NumericTextFrame(TextFrame):
  1021. """Numerical text strings.
  1022. The numeric value of these frames can be gotten with unary plus, e.g.
  1023. frame = TLEN('12345')
  1024. length = +frame
  1025. """
  1026. _framespec = [ EncodingSpec('encoding'),
  1027. MultiSpec('text', EncodedNumericTextSpec('text'), sep=u'\u0000') ]
  1028. def __pos__(self):
  1029. """Return the numerical value of the string."""
  1030. return int(self.text[0])
  1031. class NumericPartTextFrame(TextFrame):
  1032. """Multivalue numerical text strings.
  1033. These strings indicate 'part (e.g. track) X of Y', and unary plus
  1034. returns the first value:
  1035. frame = TRCK('4/15')
  1036. track = +frame # track == 4
  1037. """
  1038. _framespec = [ EncodingSpec('encoding'),
  1039. MultiSpec('text', EncodedNumericPartTextSpec('text'), sep=u'\u0000') ]
  1040. def __pos__(self):
  1041. return int(self.text[0].split("/")[0])
  1042. class TimeStampTextFrame(TextFrame):
  1043. """A list of time stamps.
  1044. The 'text' attribute in this frame is a list of ID3TimeStamp
  1045. objects, not a list of strings.
  1046. """
  1047. _framespec = [ EncodingSpec('encoding'),
  1048. MultiSpec('text', TimeStampSpec('stamp'), sep=u',') ]
  1049. def __str__(self): return self.__unicode__().encode('utf-8')
  1050. def __unicode__(self): return ','.join([stamp.text for stamp in self.text])
  1051. def _pprint(self):
  1052. return " / ".join([stamp.text for stamp in self.text])
  1053. class UrlFrame(Frame):
  1054. """A frame containing a URL string.
  1055. The ID3 specification is silent about IRIs and normalized URL
  1056. forms. Mutagen assumes all URLs in files are encoded as Latin 1,
  1057. but string conversion of this frame returns a UTF-8 representation
  1058. for compatibility with other string conversions.
  1059. The only sane way to handle URLs in MP3s is to restrict them to
  1060. ASCII.
  1061. """
  1062. _framespec = [ Latin1TextSpec('url') ]
  1063. def __str__(self): return self.url.encode('utf-8')
  1064. def __unicode__(self): return self.url
  1065. def __eq__(self, other): return self.url == other
  1066. __hash__ = Frame.__hash__
  1067. def _pprint(self): return self.url
  1068. class UrlFrameU(UrlFrame):
  1069. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.url))
  1070. class TALB(TextFrame): "Album"
  1071. class TBPM(NumericTextFrame): "Beats per minute"
  1072. class TCOM(TextFrame): "Composer"
  1073. class TCON(TextFrame):
  1074. """Content type (Genre)
  1075. ID3 has several ways genres can be represented; for convenience,
  1076. use the 'genres' property rather than the 'text' attribute.
  1077. """
  1078. from mutagen._constants import GENRES
  1079. def __get_genres(self):
  1080. genres = []
  1081. import re
  1082. genre_re = re.compile(r"((?:\((?P<id>[0-9]+|RX|CR)\))*)(?P<str>.+)?")
  1083. for value in self.text:
  1084. if value.isdigit():
  1085. try: genres.append(self.GENRES[int(value)])
  1086. except IndexError: genres.append(u"Unknown")
  1087. elif value == "CR": genres.append(u"Cover")
  1088. elif value == "RX": genres.append(u"Remix")
  1089. elif value:
  1090. newgenres = []
  1091. genreid, dummy, genrename = genre_re.match(value).groups()
  1092. if genreid:
  1093. for gid in genreid[1:-1].split(")("):
  1094. if gid.isdigit() and int(gid) < len(self.GENRES):
  1095. gid = unicode(self.GENRES[int(gid)])
  1096. newgenres.append(gid)
  1097. elif gid == "CR": newgenres.append(u"Cover")
  1098. elif gid == "RX": newgenres.append(u"Remix")
  1099. else: newgenres.append(u"Unknown")
  1100. if genrename:
  1101. # "Unescaping" the first parenthesis
  1102. if genrename.startswith("(("): genrename = genrename[1:]
  1103. if genrename not in newgenres: newgenres.append(genrename)
  1104. genres.extend(newgenres)
  1105. return genres
  1106. def __set_genres(self, genres):
  1107. if isinstance(genres, basestring): genres = [genres]
  1108. self.text = map(self.__decode, genres)
  1109. def __decode(self, value):
  1110. if isinstance(value, str):
  1111. enc = EncodedTextSpec._encodings[self.encoding][0]
  1112. return value.decode(enc)
  1113. else: return value
  1114. genres = property(__get_genres, __set_genres, None,
  1115. "A list of genres parsed from the raw text data.")
  1116. def _pprint(self):
  1117. return " / ".join(self.genres)
  1118. class TCOP(TextFrame): "Copyright (c)"
  1119. class TCMP(NumericTextFrame): "iTunes Compilation Flag"
  1120. class TDAT(TextFrame): "Date of recording (DDMM)"
  1121. class TDEN(TimeStampTextFrame): "Encoding Time"
  1122. class TDOR(TimeStampTextFrame): "Original Release Time"
  1123. class TDLY(NumericTextFrame): "Audio Delay (ms)"
  1124. class TDRC(TimeStampTextFrame): "Recording Time"
  1125. class TDRL(TimeStampTextFrame): "Release Time"
  1126. class TDTG(TimeStampTextFrame): "Tagging Time"
  1127. class TENC(TextFrame): "Encoder"
  1128. class TEXT(TextFrame): "Lyricist"
  1129. class TFLT(TextFrame): "File type"
  1130. class TIME(TextFrame): "Time of recording (HHMM)"
  1131. class TIT1(TextFrame): "Content group description"
  1132. class TIT2(TextFrame): "Title"
  1133. class TIT3(TextFrame): "Subtitle/Description refinement"
  1134. class TKEY(TextFrame): "Starting Key"
  1135. class TLAN(TextFrame): "Audio Languages"
  1136. class TLEN(NumericTextFrame): "Audio Length (ms)"
  1137. class TMED(TextFrame): "Source Media Type"
  1138. class TMOO(TextFrame): "Mood"
  1139. class TOAL(TextFrame): "Original Album"
  1140. class TOFN(TextFrame): "Original Filename"
  1141. class TOLY(TextFrame): "Original Lyricist"
  1142. class TOPE(TextFrame): "Original Artist/Performer"
  1143. class TORY(NumericTextFrame): "Original Release Year"
  1144. class TOWN(TextFrame): "Owner/Licensee"
  1145. class TPE1(TextFrame): "Lead Artist/Performer/Soloist/Group"
  1146. class TPE2(TextFrame): "Band/Orchestra/Accompaniment"
  1147. class TPE3(TextFrame): "Conductor"
  1148. class TPE4(TextFrame): "Interpreter/Remixer/Modifier"
  1149. class TPOS(NumericPartTextFrame): "Part of set"
  1150. class TPRO(TextFrame): "Produced (P)"
  1151. class TPUB(TextFrame): "Publisher"
  1152. class TRCK(NumericPartTextFrame): "Track Number"
  1153. class TRDA(TextFrame): "Recording Dates"
  1154. class TRSN(TextFrame): "Internet Radio Station Name"
  1155. class TRSO(TextFrame): "Internet Radio Station Owner"
  1156. class TSIZ(NumericTextFrame): "Size of audio data (bytes)"
  1157. class TSO2(TextFrame): "iTunes Album Artist Sort"
  1158. class TSOA(TextFrame): "Album Sort Order key"
  1159. class TSOC(TextFrame): "iTunes Composer Sort"
  1160. class TSOP(TextFrame): "Perfomer Sort Order key"
  1161. class TSOT(TextFrame): "Title Sort Order key"
  1162. class TSRC(TextFrame): "International Standard Recording Code (ISRC)"
  1163. class TSSE(TextFrame): "Encoder settings"
  1164. class TSST(TextFrame): "Set Subtitle"
  1165. class TYER(NumericTextFrame): "Year of recording"
  1166. class TXXX(TextFrame):
  1167. """User-defined text data.
  1168. TXXX frames have a 'desc' attribute which is set to any Unicode
  1169. value (though the encoding of the text and the description must be
  1170. the same). Many taggers use this frame to store freeform keys.
  1171. """
  1172. _framespec = [ EncodingSpec('encoding'), EncodedTextSpec('desc'),
  1173. MultiSpec('text', EncodedTextSpec('text'), sep=u'\u0000') ]
  1174. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.desc))
  1175. def _pprint(self): return "%s=%s" % (self.desc, " / ".join(self.text))
  1176. class WCOM(UrlFrameU): "Commercial Information"
  1177. class WCOP(UrlFrame): "Copyright Information"
  1178. class WOAF(UrlFrame): "Official File Information"
  1179. class WOAR(UrlFrameU): "Official Artist/Performer Information"
  1180. class WOAS(UrlFrame): "Official Source Information"
  1181. class WORS(UrlFrame): "Official Internet Radio Information"
  1182. class WPAY(UrlFrame): "Payment Information"
  1183. class WPUB(UrlFrame): "Official Publisher Information"
  1184. class WXXX(UrlFrame):
  1185. """User-defined URL data.
  1186. Like TXXX, this has a freeform description associated with it.
  1187. """
  1188. _framespec = [ EncodingSpec('encoding'), EncodedTextSpec('desc'),
  1189. Latin1TextSpec('url') ]
  1190. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.desc))
  1191. class PairedTextFrame(Frame):
  1192. """Paired text strings.
  1193. Some ID3 frames pair text strings, to associate names with a more
  1194. specific involvement in the song. The 'people' attribute of these
  1195. frames contains a list of pairs:
  1196. [['trumpet', 'Miles Davis'], ['bass', 'Paul Chambers']]
  1197. Like text frames, these frames also have an encoding attribute.
  1198. """
  1199. _framespec = [ EncodingSpec('encoding'), MultiSpec('people',
  1200. EncodedTextSpec('involvement'), EncodedTextSpec('person')) ]
  1201. def __eq__(self, other):
  1202. return self.people == other
  1203. __hash__ = Frame.__hash__
  1204. class TIPL(PairedTextFrame): "Involved People List"
  1205. class TMCL(PairedTextFrame): "Musicians Credits List"
  1206. class IPLS(TIPL): "Involved People List"
  1207. class MCDI(Frame):
  1208. """Binary dump of CD's TOC.
  1209. The 'data' attribute contains the raw byte string.
  1210. """
  1211. _framespec = [ BinaryDataSpec('data') ]
  1212. def __eq__(self, other): return self.data == other
  1213. __hash__ = Frame.__hash__
  1214. class ETCO(Frame):
  1215. """Event timing codes."""
  1216. _framespec = [ ByteSpec("format"), KeyEventSpec("events") ]
  1217. def __eq__(self, other): return self.events == other
  1218. __hash__ = Frame.__hash__
  1219. class MLLT(Frame):
  1220. """MPEG location lookup table.
  1221. This frame's attributes may be changed in the future based on
  1222. feedback from real-world use.
  1223. """
  1224. _framespec = [ SizedIntegerSpec('frames', 2),
  1225. SizedIntegerSpec('bytes', 3),
  1226. SizedIntegerSpec('milliseconds', 3),
  1227. ByteSpec('bits_for_bytes'),
  1228. ByteSpec('bits_for_milliseconds'),
  1229. BinaryDataSpec('data') ]
  1230. def __eq__(self, other): return self.data == other
  1231. __hash__ = Frame.__hash__
  1232. class SYTC(Frame):
  1233. """Synchronised tempo codes.
  1234. This frame's attributes may be changed in the future based on
  1235. feedback from real-world use.
  1236. """
  1237. _framespec = [ ByteSpec("format"), BinaryDataSpec("data") ]
  1238. def __eq__(self, other): return self.data == other
  1239. __hash__ = Frame.__hash__
  1240. class USLT(Frame):
  1241. """Unsynchronised lyrics/text transcription.
  1242. Lyrics have a three letter ISO language code ('lang'), a
  1243. description ('desc'), and a block of plain text ('text').
  1244. """
  1245. _framespec = [ EncodingSpec('encoding'), StringSpec('lang', 3),
  1246. EncodedTextSpec('desc'), EncodedTextSpec('text') ]
  1247. HashKey = property(lambda s: '%s:%s:%r' % (s.FrameID, s.desc, s.lang))
  1248. def __str__(self): return self.text.encode('utf-8')
  1249. def __unicode__(self): return self.text
  1250. def __eq__(self, other): return self.text == other
  1251. __hash__ = Frame.__hash__
  1252. class SYLT(Frame):
  1253. """Synchronised lyrics/text."""
  1254. _framespec = [ EncodingSpec('encoding'), StringSpec('lang', 3),
  1255. ByteSpec('format'), ByteSpec('type'), EncodedTextSpec('desc'),
  1256. SynchronizedTextSpec('text') ]
  1257. HashKey = property(lambda s: '%s:%s:%r' % (s.FrameID, s.desc, s.lang))
  1258. def __eq__(self, other):
  1259. return str(self) == other
  1260. __hash__ = Frame.__hash__
  1261. def __str__(self):
  1262. return "".join([text for (text, time) in self.text]).encode('utf-8')
  1263. class COMM(TextFrame):
  1264. """User comment.
  1265. User comment frames have a descrption, like TXXX, and also a three
  1266. letter ISO language code in the 'lang' attribute.
  1267. """
  1268. _framespec = [ EncodingSpec('encoding'), StringSpec('lang', 3),
  1269. EncodedTextSpec('desc'),
  1270. MultiSpec('text', EncodedTextSpec('text'), sep=u'\u0000') ]
  1271. HashKey = property(lambda s: '%s:%s:%r' % (s.FrameID, s.desc, s.lang))
  1272. def _pprint(self): return "%s=%r=%s" % (
  1273. self.desc, self.lang, " / ".join(self.text))
  1274. class RVA2(Frame):
  1275. """Relative volume adjustment (2).
  1276. This frame is used to implemented volume scaling, and in
  1277. particular, normalization using ReplayGain.
  1278. Attributes:
  1279. desc -- description or context of this adjustment
  1280. channel -- audio channel to adjust (master is 1)
  1281. gain -- a + or - dB gain relative to some reference level
  1282. peak -- peak of the audio as a floating point number, [0, 1]
  1283. When storing ReplayGain tags, use descriptions of 'album' and
  1284. 'track' on channel 1.
  1285. """
  1286. _framespec = [ Latin1TextSpec('desc'), ChannelSpec('channel'),
  1287. VolumeAdjustmentSpec('gain'), VolumePeakSpec('peak') ]
  1288. _channels = ["Other", "Master volume", "Front right", "Front left",
  1289. "Back right", "Back left", "Front centre", "Back centre",
  1290. "Subwoofer"]
  1291. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.desc))
  1292. def __eq__(self, other):
  1293. return ((str(self) == other) or
  1294. (self.desc == other.desc and
  1295. self.channel == other.channel and
  1296. self.gain == other.gain and
  1297. self.peak == other.peak))
  1298. __hash__ = Frame.__hash__
  1299. def __str__(self):
  1300. return "%s: %+0.4f dB/%0.4f" % (
  1301. self._channels[self.channel], self.gain, self.peak)
  1302. class EQU2(Frame):
  1303. """Equalisation (2).
  1304. Attributes:
  1305. method -- interpolation method (0 = band, 1 = linear)
  1306. desc -- identifying description
  1307. adjustments -- list of (frequency, vol_adjustment) pairs
  1308. """
  1309. _framespec = [ ByteSpec("method"), Latin1TextSpec("desc"),
  1310. VolumeAdjustmentsSpec("adjustments") ]
  1311. def __eq__(self, other): return self.adjustments == other
  1312. __hash__ = Frame.__hash__
  1313. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.desc))
  1314. # class RVAD: unsupported
  1315. # class EQUA: unsupported
  1316. class RVRB(Frame):
  1317. """Reverb."""
  1318. _framespec = [ SizedIntegerSpec('left', 2), SizedIntegerSpec('right', 2),
  1319. ByteSpec('bounce_left'), ByteSpec('bounce_right'),
  1320. ByteSpec('feedback_ltl'), ByteSpec('feedback_ltr'),
  1321. ByteSpec('feedback_rtr'), ByteSpec('feedback_rtl'),
  1322. ByteSpec('premix_ltr'), ByteSpec('premix_rtl') ]
  1323. def __eq__(self, other): return (self.left, self.right) == other
  1324. __hash__ = Frame.__hash__
  1325. class APIC(Frame):
  1326. """Attached (or linked) Picture.
  1327. Attributes:
  1328. encoding -- text encoding for the description
  1329. mime -- a MIME type (e.g. image/jpeg) or '-->' if the data is a URI
  1330. type -- the source of the image (3 is the album front cover)
  1331. desc -- a text description of the image
  1332. data -- raw image data, as a byte string
  1333. Mutagen will automatically compress large images when saving tags.
  1334. """
  1335. _framespec = [ EncodingSpec('encoding'), Latin1TextSpec('mime'),
  1336. ByteSpec('type'), EncodedTextSpec('desc'), BinaryDataSpec('data') ]
  1337. def __eq__(self, other): return self.data == other
  1338. __hash__ = Frame.__hash__
  1339. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.desc))
  1340. def _pprint(self):
  1341. return "%s (%s, %d bytes)" % (
  1342. self.desc, self.mime, len(self.data))
  1343. class PCNT(Frame):
  1344. """Play counter.
  1345. The 'count' attribute contains the (recorded) number of times this
  1346. file has been played.
  1347. This frame is basically obsoleted by POPM.
  1348. """
  1349. _framespec = [ IntegerSpec('count') ]
  1350. def __eq__(self, other): return self.count == other
  1351. __hash__ = Frame.__hash__
  1352. def __pos__(self): return self.count
  1353. def _pprint(self): return unicode(self.count)
  1354. class POPM(FrameOpt):
  1355. """Popularimeter.
  1356. This frame keys a rating (out of 255) and a play count to an email
  1357. address.
  1358. Attributes:
  1359. email -- email this POPM frame is for
  1360. rating -- rating from 0 to 255
  1361. count -- number of times the files has been played (optional)
  1362. """
  1363. _framespec = [ Latin1TextSpec('email'), ByteSpec('rating') ]
  1364. _optionalspec = [ IntegerSpec('count') ]
  1365. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.email))
  1366. def __eq__(self, other): return self.rating == other
  1367. __hash__ = FrameOpt.__hash__
  1368. def __pos__(self): return self.rating
  1369. def _pprint(self): return "%s=%r %r/255" % (
  1370. self.email, getattr(self, 'count', None), self.rating)
  1371. class GEOB(Frame):
  1372. """General Encapsulated Object.
  1373. A blob of binary data, that is not a picture (those go in APIC).
  1374. Attributes:
  1375. encoding -- encoding of the description
  1376. mime -- MIME type of the data or '-->' if the data is a URI
  1377. filename -- suggested filename if extracted
  1378. desc -- text description of the data
  1379. data -- raw data, as a byte string
  1380. """
  1381. _framespec = [ EncodingSpec('encoding'), Latin1TextSpec('mime'),
  1382. EncodedTextSpec('filename'), EncodedTextSpec('desc'),
  1383. BinaryDataSpec('data') ]
  1384. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.desc))
  1385. def __eq__(self, other): return self.data == other
  1386. __hash__ = Frame.__hash__
  1387. class RBUF(FrameOpt):
  1388. """Recommended buffer size.
  1389. Attributes:
  1390. size -- recommended buffer size in bytes
  1391. info -- if ID3 tags may be elsewhere in the file (optional)
  1392. offset -- the location of the next ID3 tag, if any
  1393. Mutagen will not find the next tag itself.
  1394. """
  1395. _framespec = [ SizedIntegerSpec('size', 3) ]
  1396. _optionalspec = [ ByteSpec('info'), SizedIntegerSpec('offset', 4) ]
  1397. def __eq__(self, other): return self.size == other
  1398. __hash__ = FrameOpt.__hash__
  1399. def __pos__(self): return self.size
  1400. class AENC(FrameOpt):
  1401. """Audio encryption.
  1402. Attributes:
  1403. owner -- key identifying this encryption type
  1404. preview_start -- unencrypted data block offset
  1405. preview_length -- number of unencrypted blocks
  1406. data -- data required for decryption (optional)
  1407. Mutagen cannot decrypt files.
  1408. """
  1409. _framespec = [ Latin1TextSpec('owner'),
  1410. SizedIntegerSpec('preview_start', 2),
  1411. SizedIntegerSpec('preview_length', 2) ]
  1412. _optionalspec = [ BinaryDataSpec('data') ]
  1413. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.owner))
  1414. def __str__(self): return self.owner.encode('utf-8')
  1415. def __unicode__(self): return self.owner
  1416. def __eq__(self, other): return self.owner == other
  1417. __hash__ = FrameOpt.__hash__
  1418. class LINK(FrameOpt):
  1419. """Linked information.
  1420. Attributes:
  1421. frameid -- the ID of the linked frame
  1422. url -- the location of the linked frame
  1423. data -- further ID information for the frame
  1424. """
  1425. _framespec = [ StringSpec('frameid', 4), Latin1TextSpec('url') ]
  1426. _optionalspec = [ BinaryDataSpec('data') ]
  1427. def __HashKey(self):
  1428. try:
  1429. return "%s:%s:%s:%r" % (
  1430. self.FrameID, self.frameid, self.url, self.data)
  1431. except AttributeError:
  1432. return "%s:%s:%s" % (self.FrameID, self.frameid, self.url)
  1433. HashKey = property(__HashKey)
  1434. def __eq__(self, other):
  1435. try: return (self.frameid, self.url, self.data) == other
  1436. except AttributeError: return (self.frameid, self.url) == other
  1437. __hash__ = FrameOpt.__hash__
  1438. class POSS(Frame):
  1439. """Position synchronisation frame
  1440. Attribute:
  1441. format -- format of the position attribute (frames or milliseconds)
  1442. position -- current position of the file
  1443. """
  1444. _framespec = [ ByteSpec('format'), IntegerSpec('position') ]
  1445. def __pos__(self): return self.position
  1446. def __eq__(self, other): return self.position == other
  1447. __hash__ = Frame.__hash__
  1448. class UFID(Frame):
  1449. """Unique file identifier.
  1450. Attributes:
  1451. owner -- format/type of identifier
  1452. data -- identifier
  1453. """
  1454. _framespec = [ Latin1TextSpec('owner'), BinaryDataSpec('data') ]
  1455. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.owner))
  1456. def __eq__(s, o):
  1457. if isinstance(o, UFI): return s.owner == o.owner and s.data == o.data
  1458. else: return s.data == o
  1459. __hash__ = Frame.__hash__
  1460. def _pprint(self):
  1461. isascii = ord(max(self.data)) < 128
  1462. if isascii: return "%s=%s" % (self.owner, self.data)
  1463. else: return "%s (%d bytes)" % (self.owner, len(self.data))
  1464. class USER(Frame):
  1465. """Terms of use.
  1466. Attributes:
  1467. encoding -- text encoding
  1468. lang -- ISO three letter language code
  1469. text -- licensing terms for the audio
  1470. """
  1471. _framespec = [ EncodingSpec('encoding'), StringSpec('lang', 3),
  1472. EncodedTextSpec('text') ]
  1473. HashKey = property(lambda s: '%s:%r' % (s.FrameID, s.lang))
  1474. def __str__(self): return self.text.encode('utf-8')
  1475. def __unicode__(self): return self.text
  1476. def __eq__(self, other): return self.text == other
  1477. __hash__ = Frame.__hash__
  1478. def _pprint(self): return "%r=%s" % (self.lang, self.text)
  1479. class OWNE(Frame):
  1480. """Ownership frame."""
  1481. _framespec = [ EncodingSpec('encoding'), Latin1TextSpec('price'),
  1482. StringSpec('date', 8), EncodedTextSpec('seller') ]
  1483. def __str__(self): return self.seller.encode('utf-8')
  1484. def __unicode__(self): return self.seller
  1485. def __eq__(self, other): return self.seller == other
  1486. __hash__ = Frame.__hash__
  1487. class COMR(FrameOpt):
  1488. """Commercial frame."""
  1489. _framespec = [ EncodingSpec('encoding'), Latin1TextSpec('price'),
  1490. StringSpec('valid_until', 8), Latin1TextSpec('contact'),
  1491. ByteSpec('format'), EncodedTextSpec('seller'),
  1492. EncodedTextSpec('desc')]
  1493. _optionalspec = [ Latin1TextSpec('mime'), BinaryDataSpec('logo') ]
  1494. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s._writeData()))
  1495. def __eq__(self, other): return self._writeData() == other._writeData()
  1496. __hash__ = FrameOpt.__hash__
  1497. class ENCR(Frame):
  1498. """Encryption method registration.
  1499. The standard does not allow multiple ENCR frames with the same owner
  1500. or the same method. Mutagen only verifies that the owner is unique.
  1501. """
  1502. _framespec = [ Latin1TextSpec('owner'), ByteSpec('method'),
  1503. BinaryDataSpec('data') ]
  1504. HashKey = property(lambda s: "%s:%s" % (s.FrameID, s.owner))
  1505. def __str__(self): return self.data
  1506. def __eq__(self, other): return self.data == other
  1507. __hash__ = Frame.__hash__
  1508. class GRID(FrameOpt):
  1509. """Group identification registration."""
  1510. _framespec = [ Latin1TextSpec('owner'), ByteSpec('group') ]
  1511. _optionalspec = [ BinaryDataSpec('data') ]
  1512. HashKey = property(lambda s: '%s:%s' % (s.FrameID, s.group))
  1513. def __pos__(self): return self.group
  1514. def __str__(self): return self.owner.encode('utf-8')
  1515. def __unicode__(self): return self.owner
  1516. def __eq__(self, other): return self.owner == other or self.group == other
  1517. __hash__ = FrameOpt.__hash__
  1518. class PRIV(Frame):
  1519. """Private frame."""
  1520. _framespec = [ Latin1TextSpec('owner'), BinaryDataSpec('data') ]
  1521. HashKey = property(lambda s: '%s:%s:%s' % (
  1522. s.FrameID, s.owner, s.data.decode('latin1')))
  1523. def __str__(self): return self.data
  1524. def __eq__(self, other): return self.data == other
  1525. def _pprint(self):
  1526. isascii = ord(max(self.data)) < 128
  1527. if isascii: return "%s=%s" % (self.owner, self.data)
  1528. else: return "%s (%d bytes)" % (self.owner, len(self.data))
  1529. __hash__ = Frame.__hash__
  1530. class SIGN(Frame):
  1531. """Signature frame."""
  1532. _framespec = [ ByteSpec('group'), BinaryDataSpec('sig') ]
  1533. HashKey = property(lambda s: '%s:%c:%s' % (s.FrameID, s.group, s.sig))
  1534. def __str__(self): return self.sig
  1535. def __eq__(self, other): return self.sig == other
  1536. __hash__ = Frame.__hash__
  1537. class SEEK(Frame):
  1538. """Seek frame.
  1539. Mutagen does not find tags at seek offsets.
  1540. """
  1541. _framespec = [ IntegerSpec('offset') ]
  1542. def __pos__(self): return self.offset
  1543. def __eq__(self, other): return self.offset == other
  1544. __hash__ = Frame.__hash__
  1545. class ASPI(Frame):
  1546. """Audio seek point index.
  1547. Attributes: S, L, N, b, and Fi. For the meaning of these, see
  1548. the ID3v2.4 specification. Fi is a list of integers.
  1549. """
  1550. _framespec = [ SizedIntegerSpec("S", 4), SizedIntegerSpec("L", 4),
  1551. SizedIntegerSpec("N", 2), ByteSpec("b"),
  1552. ASPIIndexSpec("Fi") ]
  1553. def __eq__(self, other): return self.Fi == other
  1554. __hash__ = Frame.__hash__
  1555. Frames = dict([(k,v) for (k,v) in globals().items()
  1556. if len(k)==4 and isinstance(v, type) and issubclass(v, Frame)])
  1557. """All supported ID3v2 frames, keyed by frame name."""
  1558. del(k); del(v)
  1559. # ID3v2.2 frames
  1560. class UFI(UFID): "Unique File Identifier"
  1561. class TT1(TIT1): "Content group description"
  1562. class TT2(TIT2): "Title"
  1563. class TT3(TIT3): "Subtitle/Description refinement"
  1564. class TP1(TPE1): "Lead Artist/Performer/Soloist/Group"
  1565. class TP2(TPE2): "Band/Orchestra/Accompaniment"
  1566. class TP3(TPE3): "Conductor"
  1567. class TP4(TPE4): "Interpreter/Remixer/Modifier"
  1568. class TCM(TCOM): "Composer"
  1569. class TXT(TEXT): "Lyricist"
  1570. class TLA(TLAN): "Audio Language(s)"
  1571. class TCO(TCON): "Content Type (Genre)"
  1572. class TAL(TALB): "Album"
  1573. class TPA(TPOS): "Part of set"
  1574. class TRK(TRCK): "Track Number"
  1575. class TRC(TSRC): "International Standard Recording Code (ISRC)"
  1576. class TYE(TYER): "Year of recording"
  1577. class TDA(TDAT): "Date of recording (DDMM)"
  1578. class TIM(TIME): "Time of recording (HHMM)"
  1579. class TRD(TRDA): "Recording Dates"
  1580. class TMT(TMED): "Source Media Type"
  1581. class TFT(TFLT): "File Type"
  1582. class TBP(TBPM): "Beats per minute"
  1583. class TCP(TCMP): "iTunes Compilation Flag"
  1584. class TCR(TCOP): "Copyright (C)"
  1585. class TPB(TPUB): "Publisher"
  1586. class TEN(TENC): "Encoder"
  1587. class TSS(TSSE): "Encoder settings"
  1588. class TOF(TOFN): "Original Filename"
  1589. class TLE(TLEN): "Audio Length (ms)"
  1590. class TSI(TSIZ): "Audio Data size (bytes)"
  1591. class TDY(TDLY): "Audio Delay (ms)"
  1592. class TKE(TKEY): "Starting Key"
  1593. class TOT(TOAL): "Original Album"
  1594. class TOA(TOPE): "Original Artist/Perfomer"
  1595. class TOL(TOLY): "Original Lyricist"
  1596. class TOR(TORY): "Original Release Year"
  1597. class TXX(TXXX): "User-defined Text"
  1598. class WAF(WOAF): "Official File Information"
  1599. class WAR(WOAR): "Official Artist/Performer Information"
  1600. class WAS(WOAS): "Official Source Information"
  1601. class WCM(WCOM): "Commercial Information"
  1602. class WCP(WCOP): "Copyright Information"
  1603. class WPB(WPUB): "Official Publisher Information"
  1604. class WXX(WXXX): "User-defined URL"
  1605. class IPL(IPLS): "Involved people list"
  1606. class MCI(MCDI): "Binary dump of CD's TOC"
  1607. class ETC(ETCO): "Event timing codes"
  1608. class MLL(MLLT): "MPEG location lookup table"
  1609. class STC(SYTC): "Synced tempo codes"
  1610. class ULT(USLT): "Unsychronised lyrics/text transcription"
  1611. class SLT(SYLT): "Synchronised lyrics/text"
  1612. class COM(COMM): "Comment"
  1613. #class RVA(RVAD)
  1614. #class EQU(EQUA)
  1615. class REV(RVRB): "Reverb"
  1616. class PIC(APIC):
  1617. """Attached Picture.
  1618. The 'mime' attribute of an ID3v2.2 attached picture must be either
  1619. 'PNG' or 'JPG'.
  1620. """
  1621. _framespec = [ EncodingSpec('encoding'), StringSpec('mime', 3),
  1622. ByteSpec('type'), EncodedTextSpec('desc'), BinaryDataSpec('data') ]
  1623. class GEO(GEOB): "General Encapsulated Object"
  1624. class CNT(PCNT): "Play counter"
  1625. class POP(POPM): "Popularimeter"
  1626. class BUF(RBUF): "Recommended buffer size"
  1627. class CRM(Frame):
  1628. """Encrypted meta frame"""
  1629. _framespec = [ Latin1TextSpec('owner'), Latin1TextSpec('desc'),
  1630. BinaryDataSpec('data') ]
  1631. def __eq__(self, other): return self.data == other
  1632. __hash__ = Frame.__hash__
  1633. class CRA(AENC): "Audio encryption"
  1634. class LNK(LINK):
  1635. """Linked information"""
  1636. _framespec = [ StringSpec('frameid', 3), Latin1TextSpec('url') ]
  1637. _optionalspec = [ BinaryDataSpec('data') ]
  1638. Frames_2_2 = dict([(k,v) for (k,v) in globals().items()
  1639. if len(k)==3 and isinstance(v, type) and issubclass(v, Frame)])
  1640. # support open(filename) as interface
  1641. Open = ID3
  1642. # ID3v1.1 support.
  1643. def ParseID3v1(string):
  1644. """Parse an ID3v1 tag, returning a list of ID3v2.4 frames."""
  1645. try:
  1646. string = string[string.index("TAG"):]
  1647. except ValueError:
  1648. return None
  1649. if 128 < len(string) or len(string) < 124:
  1650. return None
  1651. # Issue #69 - Previous versions of Mutagen, when encountering
  1652. # out-of-spec TDRC and TYER frames of less than four characters,
  1653. # wrote only the characters available - e.g. "1" or "" - into the
  1654. # year field. To parse those, reduce the size of the year field.
  1655. # Amazingly, "0s" works as a struct format string.
  1656. unpack_fmt = "3s30s30s30s%ds29sBB" % (len(string) - 124)
  1657. try:
  1658. tag, title, artist, album, year, comment, track, genre = unpack(
  1659. unpack_fmt, string)
  1660. except StructError:
  1661. return None
  1662. if tag != "TAG":
  1663. return None
  1664. def fix(string):
  1665. return string.split("\x00")[0].strip().decode('latin1')
  1666. title, artist, album, year, comment = map(
  1667. fix, [title, artist, album, year, comment])
  1668. frames = {}
  1669. if title: frames["TIT2"] = TIT2(encoding=0, text=title)
  1670. if artist: frames["TPE1"] = TPE1(encoding=0, text=[artist])
  1671. if album: frames["TALB"] = TALB(encoding=0, text=album)
  1672. if year: frames["TDRC"] = TDRC(encoding=0, text=year)
  1673. if comment: frames["COMM"] = COMM(
  1674. encoding=0, lang="eng", desc="ID3v1 Comment", text=comment)
  1675. # Don't read a track number if it looks like the comment was
  1676. # padded with spaces instead of nulls (thanks, WinAmp).
  1677. if track and (track != 32 or string[-3] == '\x00'):
  1678. frames["TRCK"] = TRCK(encoding=0, text=str(track))
  1679. if genre != 255: frames["TCON"] = TCON(encoding=0, text=str(genre))
  1680. return frames
  1681. def MakeID3v1(id3):
  1682. """Return an ID3v1.1 tag string from a dict of ID3v2.4 frames."""
  1683. v1 = {}
  1684. for v2id, name in {"TIT2": "title", "TPE1": "artist",
  1685. "TALB": "album"}.items():
  1686. if v2id in id3:
  1687. text = id3[v2id].text[0].encode('latin1', 'replace')[:30]
  1688. else:
  1689. text = ""
  1690. v1[name] = text + ("\x00" * (30 - len(text)))
  1691. if "COMM" in id3:
  1692. cmnt = id3["COMM"].text[0].encode('latin1', 'replace')[:28]
  1693. else:
  1694. cmnt = ""
  1695. v1["comment"] = cmnt + ("\x00" * (29 - len(cmnt)))
  1696. if "TRCK" in id3:
  1697. try: v1["track"] = chr(+id3["TRCK"])
  1698. except ValueError: v1["track"] = "\x00"
  1699. else: v1["track"] = "\x00"
  1700. if "TCON" in id3:
  1701. try: genre = id3["TCON"].genres[0]
  1702. except IndexError: pass
  1703. else:
  1704. if genre in TCON.GENRES:
  1705. v1["genre"] = chr(TCON.GENRES.index(genre))
  1706. if "genre" not in v1:
  1707. v1["genre"] = "\xff"
  1708. if "TDRC" in id3:
  1709. year = str(id3["TDRC"])
  1710. elif "TYER" in id3:
  1711. year = str(id3["TYER"])
  1712. else:
  1713. year = ""
  1714. v1["year"] = (year + "\x00\x00\x00\x00")[:4]
  1715. return ("TAG%(title)s%(artist)s%(album)s%(year)s%(comment)s"
  1716. "%(track)s%(genre)s") % v1
  1717. class ID3FileType(mutagen.FileType):
  1718. """An unknown type of file with ID3 tags."""
  1719. ID3 = ID3
  1720. class _Info(object):
  1721. length = 0
  1722. def __init__(self, fileobj, offset): pass
  1723. pprint = staticmethod(lambda: "Unknown format with ID3 tag")
  1724. def score(filename, fileobj, header):
  1725. return header.startswith("ID3")
  1726. score = staticmethod(score)
  1727. def add_tags(self, ID3=None):
  1728. """Add an empty ID3 tag to the file.
  1729. A custom tag reader may be used in instead of the default
  1730. mutagen.id3.ID3 object, e.g. an EasyID3 reader.
  1731. """
  1732. if ID3 is None:
  1733. ID3 = self.ID3
  1734. if self.tags is None:
  1735. self.ID3 = ID3
  1736. self.tags = ID3()
  1737. else:
  1738. raise error("an ID3 tag already exists")
  1739. def load(self, filename, ID3=None, **kwargs):
  1740. """Load stream and tag information from a file.
  1741. A custom tag reader may be used in instead of the default
  1742. mutagen.id3.ID3 object, e.g. an EasyID3 reader.
  1743. """
  1744. if ID3 is None:
  1745. ID3 = self.ID3
  1746. else:
  1747. # If this was initialized with EasyID3, remember that for
  1748. # when tags are auto-instantiated in add_tags.
  1749. self.ID3 = ID3
  1750. self.filename = filename
  1751. try: self.tags = ID3(filename, **kwargs)
  1752. except error: self.tags = None
  1753. if self.tags is not None:
  1754. try: offset = self.tags.size
  1755. except AttributeError: offset = None
  1756. else: offset = None
  1757. try:
  1758. fileobj = open(filename, "rb")
  1759. self.info = self._Info(fileobj, offset)
  1760. finally:
  1761. fileobj.close()