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: oggflac.py 3976 2007-01-13 22:00:14Z piman $
11 """Read and write Ogg FLAC comments.
13 This module handles FLAC files wrapped in an Ogg bitstream. The first
14 FLAC stream found is used. For 'naked' FLACs, see mutagen.flac.
16 This module is based off the specification at
17 http://flac.sourceforge.net/ogg_mapping.html.
20 __all__
= ["OggFLAC", "Open", "delete"]
24 from cStringIO
import StringIO
26 from mutagen
.flac
import StreamInfo
, VCFLACDict
27 from mutagen
.ogg
import OggPage
, OggFileType
, error
as OggError
29 class error(OggError
): pass
30 class OggFLACHeaderError(error
): pass
32 class OggFLACStreamInfo(StreamInfo
):
33 """Ogg FLAC general header and stream info.
35 This encompasses the Ogg wrapper for the FLAC STREAMINFO metadata
36 block, as well as the Ogg codec setup that precedes it.
38 Attributes (in addition to StreamInfo's):
39 packets -- number of metadata packets
40 serial -- Ogg logical stream serial number
48 while not page
.packets
[0].startswith("\x7FFLAC"):
50 major
, minor
, self
.packets
, flac
= struct
.unpack(
51 ">BBH4s", page
.packets
[0][5:13])
53 raise OggFLACHeaderError("invalid FLAC marker (%r)" % flac
)
54 elif (major
, minor
) != (1, 0):
55 raise OggFLACHeaderError(
56 "unknown mapping version: %d.%d" % (major
, minor
))
57 self
.serial
= page
.serial
59 # Skip over the block header.
60 stringobj
= StringIO(page
.packets
[0][17:])
61 super(OggFLACStreamInfo
, self
).load(StringIO(page
.packets
[0][17:]))
64 return "Ogg " + super(OggFLACStreamInfo
, self
).pprint()
66 class OggFLACVComment(VCFLACDict
):
67 def load(self
, data
, info
, errors
='replace'):
68 # data should be pointing at the start of an Ogg page, after
69 # the first FLAC page.
74 if page
.serial
== info
.serial
:
76 complete
= page
.complete
or (len(page
.packets
) > 1)
77 comment
= StringIO(OggPage
.to_packets(pages
)[0][4:])
78 super(OggFLACVComment
, self
).load(comment
, errors
=errors
)
80 def _inject(self
, fileobj
):
81 """Write tag data into the FLAC Vorbis comment packet/page."""
83 # Ogg FLAC has no convenient data marker like Vorbis, but the
84 # second packet - and second page - must be the comment data.
86 page
= OggPage(fileobj
)
87 while not page
.packets
[0].startswith("\x7FFLAC"):
88 page
= OggPage(fileobj
)
91 while not (page
.sequence
== 1 and page
.serial
== first_page
.serial
):
92 page
= OggPage(fileobj
)
95 while not (old_pages
[-1].complete
or len(old_pages
[-1].packets
) > 1):
96 page
= OggPage(fileobj
)
97 if page
.serial
== first_page
.serial
:
98 old_pages
.append(page
)
100 packets
= OggPage
.to_packets(old_pages
, strict
=False)
102 # Set the new comment block.
104 data
= packets
[0][0] + struct
.pack(">I", len(data
))[-3:] + data
107 new_pages
= OggPage
.from_packets(packets
, old_pages
[0].sequence
)
108 OggPage
.replace(fileobj
, old_pages
, new_pages
)
110 class OggFLAC(OggFileType
):
111 """An Ogg FLAC file."""
113 _Info
= OggFLACStreamInfo
114 _Tags
= OggFLACVComment
115 _Error
= OggFLACHeaderError
116 _mimes
= ["audio/x-oggflac"]
118 def score(filename
, fileobj
, header
):
119 return (header
.startswith("OggS") * (
120 ("FLAC" in header
) + ("fLaC" in header
)))
121 score
= staticmethod(score
)
125 def delete(filename
):
126 """Remove tags from a file."""
127 OggFLAC(filename
).delete()