0

I have a working BASH script with the process but I would like to migrate it to Python for flexibility, power and learning.

So In short I would like to write a function to wrap the Subprocess.open() function, but for different cases:

The main process to launch is the 'ffmpeg' programme. The final objective it's to get to lunch lines like this:

ffmpeg -y -ss 0 -t 5 -i "MP3 sourceName" -codec:a copy "MP3 Output filename"

The params to pass are changing, following the audio process chain.

Till now i stuck in the following code:

 #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'

When i copy the DEBUG- line printed in the output and launch in Terminal, i get no errors. I think the problem comes from the true params (-y -ss -t ) are been passed as a string but i don't know how to pass the params and its values So someone have any ideas?

FranGar
  • 137
  • 6
  • Possible duplicate of [Passing double quote shell commands in python to subprocess.Popen()?](https://stackoverflow.com/questions/14928860/passing-double-quote-shell-commands-in-python-to-subprocess-popen) – phd Aug 14 '17 at 13:39
  • YES itwas the quoting: – FranGar Aug 14 '17 at 15:11
  • ES it was the quoting and Shell=TRUE This now works. The call: proces( 'ffmpeg -y -ss 0 -t 3 -i "%s" -codec:a %s "%s"' %(files, Param['ACOPY'], fCH )) scaping the " withing ' and the Wrapper takes one argument: the whole command which it's passed to the Subprocess: p = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) Thanks a lot. – FranGar Aug 14 '17 at 15:18
  • Hm… well, my point was exactly the opposite — use `shell=False` and split args into a lsit (actually, avoid `.join()`). With `shell=True` you can just use `os.system()` instead of `subprocess`. – phd Aug 14 '17 at 15:26
  • Ok i see, I am returning to the high level os.system calls. I'll try your approach, (we must avoid going 'backwards') Thanks again – FranGar Aug 14 '17 at 17:58
  • YES again. This time without shell as you commented. Using a list:_italic_"ffmpeg","-y" ,"-ss", "0", "-t", "3","-i", files, "-codec:a", "%s"%(Param['ACOPY']),"%s" %(fC)] _italic_ no forced pairing the params and values, just putting them sequentially a nice neat 4seconds mp3 file created. Thanks (till the next one) – FranGar Aug 14 '17 at 19:18

0 Answers0