Merge branch 'master' into mutagen-branch
[pyTivo/wmcbrine.git] / mutagen / oggvorbis.py
blob63ecec44512802b7f9a157eb46a284c042abfecb
1 # Ogg Vorbis support.
3 # Copyright 2006 Joe Wreschnig <piman@sacredchao.net>
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: oggvorbis.py 3976 2007-01-13 22:00:14Z piman $
11 """Read and write Ogg Vorbis comments.
13 This module handles Vorbis files wrapped in an Ogg bitstream. The
14 first Vorbis stream found is used.
16 Read more about Ogg Vorbis at http://vorbis.com/. This module is based
17 on the specification at http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html.
18 """
20 __all__ = ["OggVorbis", "Open", "delete"]
22 import struct
24 from mutagen._vorbis import VCommentDict
25 from mutagen.ogg import OggPage, OggFileType, error as OggError
27 class error(OggError): pass
28 class OggVorbisHeaderError(error): pass
30 class OggVorbisInfo(object):
31 """Ogg Vorbis stream information.
33 Attributes:
34 length - file length in seconds, as a float
35 bitrate - nominal ('average') bitrate in bits per second, as an int
36 """
38 length = 0
40 def __init__(self, fileobj):
41 page = OggPage(fileobj)
42 while not page.packets[0].startswith("\x01vorbis"):
43 page = OggPage(fileobj)
44 if not page.first:
45 raise OggVorbisHeaderError(
46 "page has ID header, but doesn't start a stream")
47 (self.channels, self.sample_rate, max_bitrate, nominal_bitrate,
48 min_bitrate) = struct.unpack("<B4I", page.packets[0][11:28])
49 self.serial = page.serial
51 if nominal_bitrate == 0:
52 self.bitrate = (max_bitrate + min_bitrate) // 2
53 elif max_bitrate and max_bitrate < nominal_bitrate:
54 # If the max bitrate is less than the nominal, we know
55 # the nominal is wrong.
56 self.bitrate = max_bitrate
57 elif min_bitrate > nominal_bitrate:
58 self.bitrate = min_bitrate
59 else:
60 self.bitrate = nominal_bitrate
62 def pprint(self):
63 return "Ogg Vorbis, %.2f seconds, %d bps" % (self.length, self.bitrate)
65 class OggVCommentDict(VCommentDict):
66 """Vorbis comments embedded in an Ogg bitstream."""
68 def __init__(self, fileobj, info):
69 pages = []
70 complete = False
71 while not complete:
72 page = OggPage(fileobj)
73 if page.serial == info.serial:
74 pages.append(page)
75 complete = page.complete or (len(page.packets) > 1)
76 data = OggPage.to_packets(pages)[0][7:] # Strip off "\x03vorbis".
77 super(OggVCommentDict, self).__init__(data)
79 def _inject(self, fileobj):
80 """Write tag data into the Vorbis comment packet/page."""
82 # Find the old pages in the file; we'll need to remove them,
83 # plus grab any stray setup packet data out of them.
84 fileobj.seek(0)
85 page = OggPage(fileobj)
86 while not page.packets[0].startswith("\x03vorbis"):
87 page = OggPage(fileobj)
89 old_pages = [page]
90 while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
91 page = OggPage(fileobj)
92 if page.serial == old_pages[0].serial:
93 old_pages.append(page)
95 packets = OggPage.to_packets(old_pages, strict=False)
97 # Set the new comment packet.
98 packets[0] = "\x03vorbis" + self.write()
100 new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
101 OggPage.replace(fileobj, old_pages, new_pages)
103 class OggVorbis(OggFileType):
104 """An Ogg Vorbis file."""
106 _Info = OggVorbisInfo
107 _Tags = OggVCommentDict
108 _Error = OggVorbisHeaderError
109 _mimes = ["audio/vorbis", "audio/x-vorbis"]
111 def score(filename, fileobj, header):
112 return (header.startswith("OggS") * ("\x01vorbis" in header))
113 score = staticmethod(score)
115 Open = OggVorbis
117 def delete(filename):
118 """Remove tags from a file."""
119 OggVorbis(filename).delete()