Improvement for mobile web-GUI. Specifically clicking pyTiVo takes you back to the...
[pyTivo/wmcbrine/lucasnz.git] / mutagen / oggtheora.py
blobe0ff2596057c3e7ff54276814cd604d43617e763
1 # Ogg Theora 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: oggtheora.py 3976 2007-01-13 22:00:14Z piman $
11 """Read and write Ogg Theora comments.
13 This module handles Theora files wrapped in an Ogg bitstream. The
14 first Theora stream found is used.
16 Based on the specification at http://theora.org/doc/Theora_I_spec.pdf.
17 """
19 __all__ = ["OggTheora", "Open", "delete"]
21 import struct
23 from mutagen._vorbis import VCommentDict
24 from mutagen.ogg import OggPage, OggFileType, error as OggError
26 class error(OggError): pass
27 class OggTheoraHeaderError(error): pass
29 class OggTheoraInfo(object):
30 """Ogg Theora stream information.
32 Attributes:
33 length - file length in seconds, as a float
34 fps - video frames per second, as a float
35 """
37 length = 0
39 def __init__(self, fileobj):
40 page = OggPage(fileobj)
41 while not page.packets[0].startswith("\x80theora"):
42 page = OggPage(fileobj)
43 if not page.first:
44 raise OggTheoraHeaderError(
45 "page has ID header, but doesn't start a stream")
46 data = page.packets[0]
47 vmaj, vmin = struct.unpack("2B", data[7:9])
48 if (vmaj, vmin) != (3, 2):
49 raise OggTheoraHeaderError(
50 "found Theora version %d.%d != 3.2" % (vmaj, vmin))
51 fps_num, fps_den = struct.unpack(">2I", data[22:30])
52 self.fps = fps_num / float(fps_den)
53 self.bitrate, = struct.unpack(">I", data[37:40] + "\x00")
54 self.serial = page.serial
56 def pprint(self):
57 return "Ogg Theora, %.2f seconds, %d bps" % (self.length, self.bitrate)
59 class OggTheoraCommentDict(VCommentDict):
60 """Theora comments embedded in an Ogg bitstream."""
62 def __init__(self, fileobj, info):
63 pages = []
64 complete = False
65 while not complete:
66 page = OggPage(fileobj)
67 if page.serial == info.serial:
68 pages.append(page)
69 complete = page.complete or (len(page.packets) > 1)
70 data = OggPage.to_packets(pages)[0][7:]
71 super(OggTheoraCommentDict, self).__init__(data + "\x01")
73 def _inject(self, fileobj):
74 """Write tag data into the Theora comment packet/page."""
76 fileobj.seek(0)
77 page = OggPage(fileobj)
78 while not page.packets[0].startswith("\x81theora"):
79 page = OggPage(fileobj)
81 old_pages = [page]
82 while not (old_pages[-1].complete or len(old_pages[-1].packets) > 1):
83 page = OggPage(fileobj)
84 if page.serial == old_pages[0].serial:
85 old_pages.append(page)
87 packets = OggPage.to_packets(old_pages, strict=False)
89 packets[0] = "\x81theora" + self.write(framing=False)
91 new_pages = OggPage.from_packets(packets, old_pages[0].sequence)
92 OggPage.replace(fileobj, old_pages, new_pages)
94 class OggTheora(OggFileType):
95 """An Ogg Theora file."""
97 _Info = OggTheoraInfo
98 _Tags = OggTheoraCommentDict
99 _Error = OggTheoraHeaderError
100 _mimes = ["video/x-theora"]
102 def score(filename, fileobj, header):
103 return (header.startswith("OggS") *
104 (("\x80theora" in header) + ("\x81theora" in header)))
105 score = staticmethod(score)
107 Open = OggTheora
109 def delete(filename):
110 """Remove tags from a file."""
111 OggTheora(filename).delete()