How to pass commands to an application from python

2019-09-17 22:23发布

问题:

I am trying to run ffmpeg application from python.I am using the following code to execute the application

import subprocess
subprocess.call(['C:/ffmpeg/bin/ffmpeg.exe'])

by this command the application is getting executed. could somebody tell me how can i pass commands to the application, i tried

subprocess.call(['C:/ffmpeg/bin/ffmpeg.exe','ffmpeg -i 2.mp4 -vn -ab 128 outputaudio.mp3'])

but its not working.

回答1:

They need to be individual args:

subprocess.check_call(['C:/ffmpeg/bin/ffmpeg.exe','ffmpeg','-i',"2.mp4","-vn", "-ab", "128", "outputaudio.mp3"])

Also use check_call instead of call, check_call will raise a CalledProcessError if the command returns a non-zero exit status. I am not sure the 'ffmpeg' should be there.



回答2:

The arguments should each be separate elements in the list. The doc page has an example of how to call it. Yours should be something like this:

['C:/ffmpeg/bin/ffmpeg.exe','ffmpeg','-i 2.mp4','-vn','-ab','128','outputaudio.mp3']