/mutagen/id3.py

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

Large files are truncated click here to view the full 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 f