e1de1bd0d6d2b07c57dc876f3d44f9fd84744e29
[wolnelektury.git] / lib / mutagen / oggvorbis.py
1 # Ogg Vorbis support.
2 #
3 # Copyright 2006 Joe Wreschnig <piman@sacredchao.net>
4 #
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.
8 #
9 # $Id: oggvorbis.py 4275 2008-06-01 06:32:37Z piman $
10
11 """Read and write Ogg Vorbis comments.
12
13 This module handles Vorbis files wrapped in an Ogg bitstream. The
14 first Vorbis stream found is used.
15
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 """
19
20 __all__ = ["OggVorbis", "Open", "delete"]
21
22 import struct
23
24 from mutagen._vorbis import VCommentDict
25 from mutagen.ogg import OggPage, OggFileType, error as OggError
26
27 class error(OggError): pass
28 class OggVorbisHeaderError(error): pass
29
30 class OggVorbisInfo(object):
31     """Ogg Vorbis stream information.
32
33     Attributes:
34     length - file length in seconds, as a float
35     bitrate - nominal ('average') bitrate in bits per second, as an int
36     """
37
38     length = 0
39
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
50
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
61
62     def pprint(self):
63         return "Ogg Vorbis, %.2f seconds, %d bps" % (self.length, self.bitrate)
64
65 class OggVCommentDict(VCommentDict):
66     """Vorbis comments embedded in an Ogg bitstream."""
67
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)
78
79     def _inject(self, fileobj):
80         """Write tag data into the Vorbis comment packet/page."""
81
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)
88
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)
94
95         packets = OggPage.to_packets(old_pages, strict=False)
96
97         # Set the new comment packet.
98         packets[0] = "\x03vorbis" + self.write()
99
100         new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
101         OggPage.replace(fileobj, old_pages, new_pages)
102
103 class OggVorbis(OggFileType):
104     """An Ogg Vorbis file."""
105
106     _Info = OggVorbisInfo
107     _Tags = OggVCommentDict
108     _Error = OggVorbisHeaderError
109     _mimes = ["audio/vorbis", "audio/x-vorbis"]
110
111     def score(filename, fileobj, header):
112         return (header.startswith("OggS") * ("\x01vorbis" in header))
113     score = staticmethod(score)
114
115 Open = OggVorbis
116
117 def delete(filename):
118     """Remove tags from a file."""
119     OggVorbis(filename).delete()