[xiph-commits] r12839 - experimental/dholth/oggpy

dholth at svn.xiph.org dholth at svn.xiph.org
Mon Apr 9 10:35:24 PDT 2007


Author: dholth
Date: 2007-04-09 10:35:23 -0700 (Mon, 09 Apr 2007)
New Revision: 12839

Added:
   experimental/dholth/oggpy/oggtext-read.py
Modified:
   experimental/dholth/oggpy/oggcc.h
   experimental/dholth/oggpy/oggpy.cpp
Log:
oggpy: retrieve packet contents as Python string


Modified: experimental/dholth/oggpy/oggcc.h
===================================================================
--- experimental/dholth/oggpy/oggcc.h	2007-04-09 17:13:28 UTC (rev 12838)
+++ experimental/dholth/oggpy/oggcc.h	2007-04-09 17:35:23 UTC (rev 12839)
@@ -36,6 +36,9 @@
 
             ogg_int64_t packetno( )     { return this->get_data()->packetno; }
             ogg_int64_t granulepos( )   { return this->get_data()->granulepos; } // may want to expose only page granulepos, or this may be useful for encode only.
+            std::string get_bytes( )    { return std::string((char*)this->get_data()->packet, this->get_data()->bytes); }
+            bool bos( )                 { return this->get_data()->b_o_s; }
+            bool eos( )                 { return this->get_data()->e_o_s; }
     };
 
 

Modified: experimental/dholth/oggpy/oggpy.cpp
===================================================================
--- experimental/dholth/oggpy/oggpy.cpp	2007-04-09 17:13:28 UTC (rev 12838)
+++ experimental/dholth/oggpy/oggpy.cpp	2007-04-09 17:35:23 UTC (rev 12839)
@@ -26,6 +26,9 @@
         .def(init< const ogg::packet& >())
         .def("packetno", &ogg::packet::packetno)
         .def("granulepos", &ogg::packet::granulepos)
+        .def("get_bytes", &ogg::packet::get_bytes)
+        .def("bos", &ogg::packet::bos)
+        .def("eos", &ogg::packet::eos)
     ;
 
     class_< ogg::userpacket, bases< ogg::packet >  >("userpacket", init< const ogg::userpacket& >())

Added: experimental/dholth/oggpy/oggtext-read.py
===================================================================
--- experimental/dholth/oggpy/oggtext-read.py	2007-04-09 17:13:28 UTC (rev 12838)
+++ experimental/dholth/oggpy/oggtext-read.py	2007-04-09 17:35:23 UTC (rev 12839)
@@ -0,0 +1,62 @@
+#!/usr/bin/python
+#
+# The new line-based text format, oggtext!
+#
+# Reads lines of text from an ogg-format stream.
+#
+# Daniel Holth <dholth at fastmail.fm>, 2004
+# Charles Duffy <cduffy at spamcop.net>, 2007
+
+import oggpy
+import sys
+
+BUFSIZE=8192
+
+def packetsource(f, header):
+    """Generator to take ogg packets out of a file; filters for streams
+    whose header begins with a specific string, and returns tuples of the
+    form (stream_num, page)."""
+    streams = {}
+    ignore_streams = set()
+    
+    oy = oggpy.sync()
+    og = oggpy.page()
+    op = oggpy.packet()
+    
+    packet_buffer = True   # prime the pump
+    while packet_buffer:
+        packet_buffer = f.read(BUFSIZE)
+        oy.write(packet_buffer)
+        while(oy.pageout(og)):
+            stream_num = og.serialno()
+            if stream_num in ignore_streams:
+                continue
+            if not streams.has_key(stream_num):
+                stream = oggpy.stream(stream_num)
+                stream.pagein(og)
+                stream.packetout(op)
+                if op.bos() and op.get_bytes()[:len(header)] == header:
+                    streams[stream_num] = stream
+                    yield (stream_num, op)
+                else:
+                    ignore_streams.add(stream_num)
+                continue
+            if streams.has_key(stream_num):
+                stream = streams[stream_num]
+                stream.pagein(og)
+                while(stream.packetout(op) == 1):
+                    yield (stream_num, op)
+
+def test(f):
+    ps = packetsource(f, 'OGGText')
+    for streamnum, packet in ps:
+        if not packet.bos():
+            sys.stdout.write(packet.get_bytes())
+
+if __name__ == '__main__':
+    infile = None
+    if len(sys.argv) < 2 or sys.argv[1] == '-':
+        infile = sys.stdin
+    else:
+        infile = open(sys.argv[1], 'r')
+    test(infile)



More information about the commits mailing list