Allow passthrough of MPEG-1 video, but force remuxing, since TiVos don't
[pyTivo/wmcbrine.git] / mutagen / oggspeex.py
blob100cb1bc3f9b0585c4b559d6e64466fcb38c483e
1 # Ogg Speex 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: oggspeex.py 3976 2007-01-13 22:00:14Z piman $
11 """Read and write Ogg Speex comments.
13 This module handles Speex files wrapped in an Ogg bitstream. The
14 first Speex stream found is used.
16 Read more about Ogg Speex at http://www.speex.org/. This module is
17 based on the specification at http://www.speex.org/manual2/node7.html
18 and clarifications after personal communication with Jean-Marc,
19 http://lists.xiph.org/pipermail/speex-dev/2006-July/004676.html.
20 """
22 __all__ = ["OggSpeex", "Open", "delete"]
24 from mutagen._vorbis import VCommentDict
25 from mutagen.ogg import OggPage, OggFileType, error as OggError
26 from mutagen._util import cdata
28 class error(OggError): pass
29 class OggSpeexHeaderError(error): pass
31 class OggSpeexInfo(object):
32 """Ogg Speex stream information.
34 Attributes:
35 bitrate - nominal bitrate in bits per second
36 channels - number of channels
37 length - file length in seconds, as a float
39 The reference encoder does not set the bitrate; in this case,
40 the bitrate will be 0.
41 """
43 length = 0
45 def __init__(self, fileobj):
46 page = OggPage(fileobj)
47 while not page.packets[0].startswith("Speex "):
48 page = OggPage(fileobj)
49 if not page.first:
50 raise OggSpeexHeaderError(
51 "page has ID header, but doesn't start a stream")
52 self.sample_rate = cdata.uint_le(page.packets[0][36:40])
53 self.channels = cdata.uint_le(page.packets[0][48:52])
54 self.bitrate = cdata.int_le(page.packets[0][52:56])
55 if self.bitrate == -1:
56 self.bitrate = 0
57 self.serial = page.serial
59 def pprint(self):
60 return "Ogg Speex, %.2f seconds" % self.length
62 class OggSpeexVComment(VCommentDict):
63 """Speex comments embedded in an Ogg bitstream."""
65 def __init__(self, fileobj, info):
66 pages = []
67 complete = False
68 while not complete:
69 page = OggPage(fileobj)
70 if page.serial == info.serial:
71 pages.append(page)
72 complete = page.complete or (len(page.packets) > 1)
73 data = OggPage.to_packets(pages)[0] + "\x01"
74 super(OggSpeexVComment, self).__init__(data, framing=False)
76 def _inject(self, fileobj):
77 """Write tag data into the Speex comment packet/page."""
79 fileobj.seek(0)
81 # Find the first header page, with the stream info.
82 # Use it to get the serial number.
83 page = OggPage(fileobj)
84 while not page.packets[0].startswith("Speex "):
85 page = OggPage(fileobj)
87 # Look for the next page with that serial number, it'll start
88 # the comment packet.
89 serial = page.serial
90 page = OggPage(fileobj)
91 while page.serial != serial:
92 page = OggPage(fileobj)
94 # Then find all the pages with the comment packet.
95 old_pages = [page]
96 while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
97 page = OggPage(fileobj)
98 if page.serial == old_pages[0].serial:
99 old_pages.append(page)
101 packets = OggPage.to_packets(old_pages, strict=False)
103 # Set the new comment packet.
104 packets[0] = self.write(framing=False)
106 new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
107 OggPage.replace(fileobj, old_pages, new_pages)
109 class OggSpeex(OggFileType):
110 """An Ogg Speex file."""
112 _Info = OggSpeexInfo
113 _Tags = OggSpeexVComment
114 _Error = OggSpeexHeaderError
115 _mimes = ["audio/x-speex"]
117 def score(filename, fileobj, header):
118 return (header.startswith("OggS") * ("Speex " in header))
119 score = staticmethod(score)
121 Open = OggSpeex
123 def delete(filename):
124 """Remove tags from a file."""
125 OggSpeex(filename).delete()