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: 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.
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 max_bitrate
= max(0, max_bitrate
)
52 min_bitrate
= max(0, min_bitrate
)
53 nominal_bitrate
= max(0, nominal_bitrate
)
55 if nominal_bitrate
== 0:
56 self
.bitrate
= (max_bitrate
+ min_bitrate
) // 2
57 elif max_bitrate
and max_bitrate
< nominal_bitrate
:
58 # If the max bitrate is less than the nominal, we know
59 # the nominal is wrong.
60 self
.bitrate
= max_bitrate
61 elif min_bitrate
> nominal_bitrate
:
62 self
.bitrate
= min_bitrate
64 self
.bitrate
= nominal_bitrate
66 if self
.bitrate
== 0 and self
.length
> 0:
68 self
.bitrate
= int((fileobj
.tell() * 8) / self
.length
)
72 return "Ogg Vorbis, %.2f seconds, %d bps" % (self
.length
, self
.bitrate
)
74 class OggVCommentDict(VCommentDict
):
75 """Vorbis comments embedded in an Ogg bitstream."""
77 def __init__(self
, fileobj
, info
):
81 page
= OggPage(fileobj
)
82 if page
.serial
== info
.serial
:
84 complete
= page
.complete
or (len(page
.packets
) > 1)
85 data
= OggPage
.to_packets(pages
)[0][7:] # Strip off "\x03vorbis".
86 super(OggVCommentDict
, self
).__init
__(data
)
88 def _inject(self
, fileobj
):
89 """Write tag data into the Vorbis comment packet/page."""
91 # Find the old pages in the file; we'll need to remove them,
92 # plus grab any stray setup packet data out of them.
94 page
= OggPage(fileobj
)
95 while not page
.packets
[0].startswith("\x03vorbis"):
96 page
= OggPage(fileobj
)
99 while not (old_pages
[-1].complete
or len(old_pages
[-1].packets
) > 1):
100 page
= OggPage(fileobj
)
101 if page
.serial
== old_pages
[0].serial
:
102 old_pages
.append(page
)
104 packets
= OggPage
.to_packets(old_pages
, strict
=False)
106 # Set the new comment packet.
107 packets
[0] = "\x03vorbis" + self
.write()
109 new_pages
= OggPage
.from_packets(packets
, old_pages
[0].sequence
)
110 OggPage
.replace(fileobj
, old_pages
, new_pages
)
112 class OggVorbis(OggFileType
):
113 """An Ogg Vorbis file."""
115 _Info
= OggVorbisInfo
116 _Tags
= OggVCommentDict
117 _Error
= OggVorbisHeaderError
118 _mimes
= ["audio/vorbis", "audio/x-vorbis"]
120 def score(filename
, fileobj
, header
):
121 return (header
.startswith("OggS") * ("\x01vorbis" in header
))
122 score
= staticmethod(score
)
126 def delete(filename
):
127 """Remove tags from a file."""
128 OggVorbis(filename
).delete()