0

我正在用 IMAPI2 刻录 CD/DVD 的模块在 Python 上编写程序。我使用了这些示例:codeplexcomtypes doc

import comtypes.client as cc

class MyProgram(object):
    def main(self):
        # index of recording drive
        index = 0

        # files to transfer to disc
        path = r'D:\Example'

        # DiscMaster2 object to connect to optical drives
        discMaster = cc.CreateObject('IMAPI2.MsftDiscMaster2')
        # cc.ShowEvents(discMaster) ### uncomment for print events

        # DiscRecorder object for specified burning device
        recorder = cc.CreateObject('IMAPI2.MsftDiscRecorder2')
        uniqueId = discMaster.Item(index)
        recorder.InitializeDiscRecorder(uniqueId)

        # create an image stream for a specified directory
        # create a new file system image and retrieve root directory
        _fsi = cc.CreateObject('IMAPI2FS.MsftFileSystemImage')
        _dir = _fsi.Root
        _fsi.VolumeName = 'TestName'

        # create the new disc format and set the recorder
        dataWriter = cc.CreateObject('IMAPI2.MsftDiscFormat2Data')
        # cc.ShowEvents(dataWriter) ### uncomment for print events
        dataWriter.Recorder = recorder
        dataWriter.ClientName = 'MyClient'
        dataWriter.ForceOverwrite = True

        _fsi.ChooseImageDefaults(recorder)

        # get events
        sink = Sink()
        _events = cc.GetEvents(dataWriter, sink)
        # print(_events)  ### <comtypes.client._events._AdviseConnection object at 0x...>

        # add the directory and its content to the file system
        _dir.AddTree(path, False)

        # create an image from the file system
        _result = _fsi.CreateResultImage()
        _stream = _result.ImageStream

        # write disc using the specified recorder
        print('Writing content to disc...')
        dataWriter.Write(_stream)
        print('Finished writing content')

使用名为“dataWriter”事件的方法创建一个自定义类:

class Sink(object):
    def DDiscFormat2DataEvents_Update(self, this, progress):
        strTimeStatus = 'Time: ' + progress.ElapsedTime + ' / ' + progress.TotalTime
        print(strTimeStatus)

并运行此脚本:

prog = MyProgram()
prog.main()

我可以刻录 CD/DVD,但无法监控进度,我不明白,为什么。

有人有什么主意吗?

非常感谢。

4

0 回答 0