首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线程中线程

线程中线程
EN

Stack Overflow用户
提问于 2014-05-28 14:09:40
回答 1查看 152关注 0票数 1

我的公司致力于视觉效果,我们建立了一个内部的镜头回放通过浏览器为我们的客户。为此,我们需要将视频文件上传到FTP服务器。

我想将图像序列转换为mp4,并在呈现完成后直接上传该文件。

为此,我使用:

  • 一个要转换的命令提示符
  • 一个命令提示符以获得`md5散列
  • 一个用来上传文件

我已经在我的本地计算机上实现了这一点,在那里我刚刚链接了os.system('command')

在认识到程序被长时间的图像序列冻结之后,我改变了脚本,使用os.system链生成一个线程。但是在呈现农场服务器上,这个脚本实际上不起作用。

RenderFarm服务器运行Python2.5

有一些代码示例:

代码语言:javascript
复制
class CopraUpload(threading.Thread):

    # initializing Thread
    # via super constructor
    def __init__(self):
        threading.Thread.__init__(self)


    # return the size of the
    # created mp4 file
    #
    # @return: the file size in byte
    def _getFileSize(self):


    # creates a random id for organising
    # the server upload used as flag
    #
    # @return: a hash
    def _getHash(self):
            self.fileLoc = str(self.outputfileName + '.mp4')
            self.fileLoc = os.path.normpath(self.fileLoc)
            return str(os.path.getsize(self.fileLoc))

    # integrates the "missing" data for the xml file
    # generated post render from the mp4 file
    def _setPreviewDataToXML(self):
        self.xmlFile = str(self.outputfileName + '_copraUpload.xml')
        self.xmlFile = os.path.normpath(self.xmlFile)

        ett = ET.parse(self.xmlFile)
        root = ett.getroot()
        for child in root.getiterator('preview_size'):
            child.text = self._getFileSize()
        for child in root.getiterator('preview_md5hash'):
            child.text = self._getHash()
        ett.write(self.xmlFile)


    # create a connection to a ftp server
    # and copies the mp4 file and the xml file
    # on the server
    def _uploadToCopra(self):

        os.system(self.uploadCommand)
        #process = Popen(self.uploadCommand)


    # the main function of the program
    # called via start from a Thread Object
    def run(self):

        # the command which will be send to the commando shell
        # for further adjustments see ffmpeg help with ffmpeg.exe -h
        FinalCommand = self.ffmpegLocation + " -r "+ self.framerate + " -i " + self.inputPath + " -an -strict experimental -s hd720 -vcodec libx264 -preset slow -profile:v baseline -level 31 -refs 1 -maxrate 6M -bufsize 10M -vb 6M -threads 0 -g 8 -r " + self.framerate + " " + self.outputfileName + ".mp4 -y"
        FinalCommandList = FinalCommand.split(" ")


        # calling the program
        print "Start ffmpeg convertion"

        outInfo = os.path.normpath("C:\\Users\\sarender\\Desktop\\stdoutFFMPEG.txt")
        outError = os.path.normpath("C:\\Users\\sarender\\Desktop\\stderrFFMPEG.txt")
        stdoutFile = open(outInfo,"w")
        stderrFile = open(outError,"w")

        handle = subp.check_all(FinalCommandList,stdout = stdoutFile,stderr = stderrFile)
        handle.communicate()
        stdoutFile.close()
        stderrFile.close()
        print "Convertion from ffmpeg done"

        # fill the xml file with the missing data
        # - preview file size
        # - preview md5hash
        self._setPreviewDataToXML()
        self._uploadToCopra()
        print "---------------------------------->FINISHED------------------------------------------------------>"


    # Creates a callable Thread for the Copra Upload.
    # start is calling the run method which will start the Uploading

主要的出发点是:

代码语言:javascript
复制
    if "$(RenderSet.writenode)" == "PREVIEW":
        print "---------------------------------->Initializing Script------------------------------------------------------>"
        process = CopraUpload()
        process.start()

发生了什么:

脚本在呈现之后开始,ffmpeg转换图像序列并创建一个mp4。但在那之后就停止了。它不打印"Conversion from ffmpeg complet"。把剧本停下来。

实际上,它应该使用ffmpeg创建线程转换并等待完成。之后,应该在xml文件中编写一些内容,并将两者上传到服务器。

我错过了什么吗?线程中的subprocess不是要走的路吗?但是我需要一个线程,因为我不能锁住渲染管理服务器。

EN

回答 1

Stack Overflow用户

发布于 2014-05-28 14:33:51

我猜这个命令失败了,并在handle.communicate()中抛出了一个异常。确保捕获所有异常并将它们记录在某个地方,因为线程无法传递异常。

另外,您不应该使用FinalCommand.split(" ") --如果文件名包含空格,这将以奇怪(且不可预测)的方式失败。创建一个列表,而将该列表传递给subp

代码语言:javascript
复制
FinalCommand = [
    self.ffmpegLocation,
    "-r", self.framerate,
    "-i", self.inputPath,
    "-an",
    "-strict experimental",
    ...
]

也更易读。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23914007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档