[xiph-cvs] cvs commit: ogg-python2/examples stream_extract.py

Arc arc at xiph.org
Sat Nov 29 22:33:41 PST 2003



arc         03/11/30 01:33:41

  Added:       .        README
               examples stream_extract.py
  Log:
  A README file and (gasp) the first example script!

Revision  Changes    Path
1.1                  ogg-python2/README

Index: README
===================================================================
Compiling
 Like most Python modules, compiling is done through Python:
  python config_unix.py
  python setup.py build
  python setup.py install (as root)

 It should be just that simple.  If you run into problems, let us know
 by entering a bug report at the url below.

<p>Usage
 Inside Python, simply "import ogg2" like you would any module:
>>> import ogg2

 Before going any further, take a look at the docs and/or example
 scripts to see how the different parts of this module work together. 
 If you're brave and want to figure it out on your own, dir() should
 display the functions and attributes of any object in this module.

<p>Bugs 
 No matter how much effort goes into debugging software, someone is
 eventually going to find another one.  Being as this is currently
 Alpha code, meaning it's still under development, there's sure to be
 bugs everywhere you turn.  To submit a bug report go to:

                    http://bugs.xiph.org/enter_bug.cgi?product=Py-Ogg

<p><p>1.1                  ogg-python2/examples/stream_extract.py

Index: stream_extract.py
===================================================================
'''

This example displays the bitstreams found in an Ogg file and allows the
user to select one for extraction into a seperate file.

by Arc Riley <arc at xiph.org>

'''

import sys

try:
   import ogg2
except:
   print 'Could not import ogg2 module.'
   sys.exit(1)

<p>def getserials(f) :
   streams = []

   def testpage(page) :
      if page.bos :
         try :
            streams.index(page.serialno)
         except ValueError :
            print len(streams), page.serialno
            streams.append(page.serialno)      

   sync = ogg2.OggSyncState()
   while sync.input(f) :
      while 1 :
         page = sync.pageout()
         if page == None : break
         testpage(page)
   return streams

def chooseprompt(streams) :
   while 1:
      choice = raw_input('Index: ')
      if choice.isdigit() :
         choice = int(choice) 
         if choice>-1 and choice<len(streams) : 
            break
         else :
            print 'Not in range (0 - %d).' % (len(streams)-1,)
      else :
         print 'Numbers only.'
   return streams[choice]

def extractit(cserial, f_in, f_out) :
   print "Extracting serialno %d" % cserial
   syncin = ogg2.OggSyncState()
   syncout = ogg2.OggSyncState()
   f_in.seek(0)
   while syncin.input(f_in) :
      while 1 :
         page = syncin.pageout()
         if page == None : break
         if page.serialno == cserial :
            syncout.pagein(page)
            while syncout.output(f_out) : pass
   print 'done.'   

<p>def main() :
   import getopt, string
   try:
      opts, args = getopt.getopt(sys.argv[1:], 'v')
   except getopt.error:
      sys.stderr.write('Usage: ' + sys.argv[0] + ' infile outfile\n')
      sys.exit(1)
   if len(args) != 2 :
      sys.stderr.write('Usage: ' + sys.argv[0] + ' infile outfile\n')
      sys.exit(1)
   try:
      f_in = open(args[0],'r')
   except:
      sys.stderr.write('%s could not be opened for input.' % args[0])
      sys.exit(1)
   try:
      f_out = open(args[1],'w')
   except:
      sys.stderr.write('%s could not be opened for output.' % args[1])
      sys.exit(1)

   # Serial execution, these are seperate to make it easier to read   
   extractit(chooseprompt(getserials(f_in)), f_in, f_out)

   f_in.close()
   f_out.close()
         

if __name__ == '__main__' :
   main()

<p><p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list