蟒蛇。如何在包装函数(Linux)中将参数传递给Subprocess.Popen

时间:2017-08-14 13:24:27

标签: python subprocess parameter-passing wrapper

我有一个有效的BASH脚本,但是我希望将它迁移到Python以获得灵活性,功能和学习。

所以简而言之,我想编写一个函数来包装Subprocess.open()函数,但对于不同的情况:

推出的主要流程是' ffmpeg'程序。 最后一个目标就是这样吃午餐:

ffmpeg -y -ss 0 -t 5 -i" MP3 sourceName" -codec:副本" MP3输出文件名"

传递的参数正在改变,跟随音频处理链。

直到现在我坚持使用以下代码:

 #The 'Wrapping' function:

    def proces(*args):
        arg = ''.join(args)
        print ("DEBUG- ffmpeg ",arg)
    #these lines are for debug purpose only

        try :
            p = subprocess.Popen(["ffmpeg", arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            response, err = p.communicate()
            return response, err
        except:
            pass

    #Here the initially call in the main programme 

    def A_Process (files):
        proces( " -y -ss 0 -t 3 -i ","\"%s\" "%(files), "%s"%(Param['ACOPY']), " \"%s\""%(fCH) ), " \"%s\""%(fCH) ]  )

#-y param to indicate yes to overwrite
#-ss = param to indicate start time: decimal
#-t = param to indicate time length: decimal
#files is the MP3 sourceName as a string (pure string so i need the "")
#Param['ACOPY']= "copy" string i think?
#fCH = "Output MP3 filename" also string


#### ... more irrelevant code....

#### Launching the code gives me a print:
DEBUG- ffmpeg  -y -ss 0 -t 3 -i "/media/temps/scp/fs/1.mp3" -codec:a copy "1_output.mp3"

    ##and stderr gives:

            b'ffmpeg version 3.3.3-static http://johnvansickle.com/ffmpeg/  Copyright (c)(...etc...)...
  ... \n[NULL @ 0x32861c0] Unable to find a suitable output
 format for \' -y -ss 0 -t 3 -i "/media/temps/scp/fs/1.mp3" -codec:a copy "1_output.mp3"\'\n
     -y -ss 0 -t 3 -i "/media/temps/scp/fs/1.mp3" -codec:a copy ""1_output.mp3": **Invalid argument**\n'

当我复制输出中打印的DEBUG-行并在终端中启动时,我没有错误。 我认为问题来自于真正的参数(-y -ss -t)已作为字符串传递但我不知道如何传递参数及其值 所以有人有任何想法吗?

0 个答案:

没有答案