Ratings from .nfo files should no longer be converted to strings.
[pyTivo/wmcbrine.git] / mutagen / oggspeex.py
blob001f0a9bc162fbeaf5801a6ee05ad832921ef1a7
1 # Ogg Speex support.
3 # Copyright 2006 Joe Wreschnig
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
9 # $Id: oggspeex.py 3976 2007-01-13 22:00:14Z piman $
11 """Read and write Ogg Speex comments.
13 This module handles Speex files wrapped in an Ogg bitstream. The
14 first Speex stream found is used.
16 Read more about Ogg Speex at http://www.speex.org/. This module is
17 based on the specification at http://www.speex.org/manual2/node7.html
18 and clarifications after personal communication with Jean-Marc,
19 http://lists.xiph.org/pipermail/speex-dev/2006-July/004676.html.
20 """
22 __all__ = ["OggSpeex", "Open", "delete"]
24 from mutagen._vorbis import VCommentDict
25 from mutagen.ogg import OggPage, OggFileType, error as OggError
26 from mutagen._util import cdata
28 class error(OggError): pass
29 class OggSpeexHeaderError(error): pass
31 class OggSpeexInfo(object):
32 """Ogg Speex stream information.
34 Attributes:
35 bitrate - nominal bitrate in bits per second
36 channels - number of channels
37 length - file length in seconds, as a float
39 The reference encoder does not set the bitrate; in this case,
40 the bitrate will be 0.
41 """
43 length = 0
45 def __init__(self, fileobj):
46 page = OggPage(fileobj)
47 while not page.packets[0].startswith("Speex "):
48 page = OggPage(fileobj)
49 if not page.first:
50 raise OggSpeexHeaderError(
51 "page has ID header, but doesn't start a stream")
52 self.sample_rate = cdata.uint_le(page.packets[0][36:40])
53 self.channels = cdata.uint_le(page.packets[0][48:52])
54 self.bitrate = max(0, cdata.int_le(page.packets[0][52:56]))
55 self.serial = page.serial
57 def pprint(self):
58 return "Ogg Speex, %.2f seconds" % self.length
60 class OggSpeexVComment(VCommentDict):
61 """Speex comments embedded in an Ogg bitstream."""
63 def __init__(self, fileobj, info):
64 pages = []
65 complete = False
66 while not complete:
67 page = OggPage(fileobj)
68 if page.serial == info.serial:
69 pages.append(page)
70 complete = page.complete or (len(page.packets) > 1)
71 data = OggPage.to_packets(pages)[0] + "\x01"
72 super(OggSpeexVComment, self).__init__(data, framing=False)
74 def _inject(self, fileobj):
75 """Write tag data into the Speex comment packet/page."""
77 fileobj.seek(0)
79 # Find the first header page, with the stream info.
80 # Use it to get the serial number.
81 page = OggPage(fileobj)
82 while not page.packets[0].startswith("Speex "):
83 page = OggPage(fileobj)
85 # Look for the next page with that serial number, it'll start
86 # the comment packet.
87 serial = page.serial
88 page = OggPage(fileobj)
89 while page.serial != serial:
90 page = OggPage(fileobj)
92 # Then find all the pages with the comment packet.
93 old_pages = [page]
94 while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
95 page = OggPage(fileobj)
96 if page.serial == old_pages[0].serial:
97 old_pages.append(page)
99 packets = OggPage.to_packets(old_pages, strict=False)
101 # Set the new comment packet.
102 packets[0] = self.write(framing=False)
104 new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
105 OggPage.replace(fileobj, old_pages, new_pages)
107 class OggSpeex(OggFileType):
108 """An Ogg Speex file."""
110 _Info = OggSpeexInfo
111 _Tags = OggSpeexVComment
112 _Error = OggSpeexHeaderError
113 _mimes = ["audio/x-speex"]
115 def score(filename, fileobj, header):
116 return (header.startswith("OggS") * ("Speex " in header))
117 score = staticmethod(score)
119 Open = OggSpeex
121 def delete(filename):
122 """Remove tags from a file."""
123 OggSpeex(filename).delete()