CD Burning

hugonz at gmail.com hugonz at gmail.com
Tue Apr 4 18:56:17 EDT 2006


Hi,

I used cdrecord, albeit on Linux, to do it. Someone already suggested
to use Cygwin for that.

I'll just drop a piece of code I wrote for getting the percentage of
advance when recording sound, with cdrecord. Here it is (wraps
cdrecord):

#!/usr/bin/env python
"""
Canonizer: will open up a program and try to canonize its output.

Output is program dependent (different information is needed) but
always
canonical (messages are one line long, and parsable through splitting)

Currently only cdrecord will be implemented, but will be tailored for
anything.


Usage:

canonizer.py <myprogram and options>


"""

import re
import sys
import os


track_regexp = re.compile(r"^Track\s+(\d+):\s+(\d+)\s+of\s+(\d+)")
total_regexp = re.compile(r"^Total size:\s+(\d+)")
fixating_regexp = re.compile(r"^Fixating\.")
done_regexp = re.compile(r"^Fixating time")


def loop_cdrecord(filep):
    """Loop over cdrecord's output"""
    mycharbuf = ""


    exitstatus = ""
    burning = 0

    #progress in Mbytes
    progress = 0
    newinterval = oldinterval = 0

    while True:
        data = filep.read(1)

        if not data:
            return exitstatus
        mycharbuf+= data

        if mycharbuf.endswith("\n") or mycharbuf.endswith("\r"):
            #sys.stdout.write(mycharbuf)
            #sys.stdout.flush()
            mycharbuf = mycharbuf[:-1]

            if not burning:
                try:
                    totalsize =
int(total_regexp.match(mycharbuf).group(1))
                    burning = 1
                except:
                    pass
            else:
                if (track_regexp.search(mycharbuf)):
                    reobj = track_regexp.match(mycharbuf)
                    mytrack, mynum, mydem = reobj.group(1, 2, 3)
                    oldinterval = newinterval
                    newinterval = int(mynum)
                    if (oldinterval <= newinterval):
                        progress += newinterval - oldinterval
                    else:
                        oldinterval = 0

                    print "CAN_PERCENT = %d"%((progress*98)/totalsize)

                elif (fixating_regexp.search(mycharbuf)):
                    print "CAN_PERCENT = %d"%(98)
                    print "CAN_FIXATING"

                elif (done_regexp.search(mycharbuf)):
                    progress = 100
                    print "CAN_PERCENT = %d"%(100)
                    exitstatus = "ok"
                    print "CAN_DONE"

                    sys.stdout.flush()

            mycharbuf = ""

commandline = ""

#for i in sys.argv:
#    print i

#Get the intended commandline
for i in sys.argv[1:]:
    commandline += " " + i

pipe_filep = os.popen (commandline, "r")

exitstatus = loop_cdrecord(pipe_filep)

if (exitstatus != "ok"):
    print "CAN_ERROR"




More information about the Python-list mailing list