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 4275 2008-06-01 06:32:37Z 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.
 
  20 __all__ = ["OggVorbis", "Open", "delete"]
 
  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.
 
  34     length - file length in seconds, as a float
 
  35     bitrate - nominal ('average') bitrate in bits per second, as an int
 
  40     def __init__(self, fileobj):
 
  41         page = OggPage(fileobj)
 
  42         while not page.packets[0].startswith("\x01vorbis"):
 
  43             page = OggPage(fileobj)
 
  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
 
  60             self.bitrate = nominal_bitrate
 
  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):
 
  72             page = OggPage(fileobj)
 
  73             if page.serial == info.serial:
 
  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.
 
  85         page = OggPage(fileobj)
 
  86         while not page.packets[0].startswith("\x03vorbis"):
 
  87             page = OggPage(fileobj)
 
  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)
 
 117 def delete(filename):
 
 118     """Remove tags from a file."""
 
 119     OggVorbis(filename).delete()