I want to add ffmpeg options to a variable and later use it in a subprocess call. If the variable contains one word, all is well, but if it contains more than one, I get errors. I am working on a larger script and I will need to have extra options such as this for certain codecs. How can I get this working?
The following works perfectly for me:
import subprocess
import sys
video_codec = 'libx264'
output = sys.argv[1] + '.mkv'
subprocess.call(['ffmpeg',
'-i',sys.argv[1],
'-c:v',video_codec,
'-c:a','copy',
output])
Once I introduce new options/spaces to video_options as such:
video_codec = "'libx264', '-pix_fmt', 'yuv420p'"
I get an ffmpeg error:
Unknown encoder ''libx264', '-pix_fmt', 'yuv420p''
If I remove the double quotes and just use
video_codec = 'libx264', '-pix_fmt', 'yuv420p'
I get a python error:
Traceback (most recent call last):
File "testo.py", line 10, in <module>
output])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
TypeError: execv() arg 2 must contain only strings
Eric Renouf has it right, but don't create a string, its less reliable. You just have to break apart your config like this:
video_codecs = ['libx264', '-pix_fmt', 'yuv420p']
output = sys.argv[1] + '.mkv'
cmd_list = ['ffmpeg',
'-i',sys.argv[1],
'-c:v']
cmd_list += video_codecs
cmd_list += ['-c:a','copy',
output]
subprocess.call(cmd_list)
I'm not sure if the order matters, so I preserved it here, if order doesn't matter, you can just combine the last line with the third line.
Related
I'd like to use SVOX/pico2wave to write a wav-file from Python code. When I execute this line from a terminal the file is written just fine:
/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."
I've verified that pico2wave is located in /usr/bin.
This is my Python code:
from subprocess import call
call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
... which throws this error:
Traceback (most recent call last):
File "app/app.py", line 63, in <module>
call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
File "/usr/lib/python2.7/subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1024, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
From the documentation
Providing a sequence of arguments is generally preferred, as it allows
the module to take care of any required escaping and quoting of
arguments (e.g. to permit spaces in file names). If passing a single
string, either shell must be True (see below) or else the string must
simply name the program to be executed without specifying any
arguments.
So you might try with
call(['/usr/bin/pico2wave', '-w=/tmp/tmp_say.wav', '"Hello world."'])
In my program I call the command:
command_two = 'sfit4Layer0.py -bv5 -fs'
subprocess.call(command_two.split(), shell=False)
I am using PyCharm and I get the error message:
Traceback (most recent call last):
File "part2test.py", line 5, in <module>
subprocess.call(command_two.split(), shell=False) #writes the summary file
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
When walking through my program, it never gets to the program I want it to sfit4Layer0.py, it is getting stuck in subprocess but I am not sure why. Changing the shell=True doesn't do anything helpful either - I don't get these error messages but it does not execute my code properly. Any suggestions would be helpful.
My bash profile:
PATH="~/bin:/usr/bin:${PATH}"
export PATH PYTHONPATH="/Users/nataliekille/Documents/sfit4/pbin/Layer0:/Users/nataliekille/Documents/sfit4/pbin/Layer1:/Users/nataliekille/Documents/sfit4/pbin/ModLib:/Users/nataliekille/Documents/sfit4/SpectralDB"
export PYTHONPATH
PATH=${PATH}:${PYTHONPATH}
export PATH
You've missed an important part of the subprocess documentation. "If passing a single string [at the command, rather than a list of strings], either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments."
So the kernel is compaining because there is not executable with the name 'sfit4Layer0.py -bv5 -fs'. Should work if you replace the string with (for example) 'sfit4Layer0.py -bv5 -fs'.split(), or ['sfit4Layer0.py', '-bv5', '-fs'].
I'm trying to pass these below chained commands at once to the subprocess.But it's throwing error.
Command to pass:
"C:\MyScript\run.bat" -I"C:\MyScript" -- "C:\MyScript\MyScript1.pl" "file_name"
where, file_name is a variable which holds the name of a file.
Code:
cmd = '"C:\MyScript\run.bat" -I"C:\MyScript" -- "C:\MyScript\MyScript1.pl"' + " " + filename
subprocess.call(cmd)
Error:
Traceback (most recent call last):
File "c:\Test\WWX_2.py", line 28, in
<module>
subprocess.call(cmd)
File "C:\Python26\lib\subprocess.py", line 444, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python26\lib\subprocess.py", line 595, in __init__
errread, errwrite)
File "C:\Python26\lib\subprocess.py", line 821, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Use raw string literals to prevent \r from being interpreted as a carriage return. The space-only arguments are also passed as actual command line arguments, but you probably don't want that to happen, so remove them.
cmd = [r'C:\MyScript\run.bat', r'-I"C:\MyScript"', '--', r'C:\MyScript\MyScript1.pl', filename]
subprocess.call(cmd)
You have to escape all the backslashes in the path, i.e.
C:\\whatever\\batch.bat
I am trying to run a program to make some system calls inside Python code using subprocess.call() which throws the following error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
My actual Python code is as follows:
url = "/media/videos/3cf02324-43e5-4996-bbdf-6377df448ae4.mp4"
real_path = "/home/chanceapp/webapps/chanceapp/chanceapp"+url
fake_crop_path = "/home/chanceapp/webapps/chanceapp/chanceapp/fake1"+url
fake_rotate_path = "/home/chanceapp/webapps/chanceapp.chanceapp/fake2"+url
crop = "ffmpeg -i %s -vf "%(real_path)+"crop=400:400:0:0 "+ "-strict -2 %s"%(fake_crop_path)
rotate = "ffmpeg -i %s -vf "%(fake_crop_path)+"transpose=1 "+"%s"%(fake_rotate_path)
move_rotated = "mv"+" %s"%(fake_rotate_path)+" %s"%(real_path)
delete_cropped = "rm "+"%s"%(fake_crop_path)
#system calls:
subprocess.call(crop)
Can I get some relevant advice on how to solve this?
Use shell=True if you're passing a string to subprocess.call.
From docs:
If passing a single string, either shell must be True or
else the string must simply name the program to be executed without
specifying any arguments.
subprocess.call(crop, shell=True)
or:
import shlex
subprocess.call(shlex.split(crop))
No such file or directory can be also raised if you are trying to put a file argument to Popen with double-quotes.
For example:
call_args = ['mv', '"path/to/file with spaces.txt"', 'somewhere']
In this case, you need to remove double-quotes.
call_args = ['mv', 'path/to/file with spaces.txt', 'somewhere']
Can't upvote so I'll repost #jfs comment cause I think it should be more visible.
#AnneTheAgile: shell=True is not required. Moreover you should not use
it unless it is necessary (see # valid's comment). You should pass
each command-line argument as a separate list item instead e.g., use
['command', 'arg 1', 'arg 2'] instead of "command 'arg 1' 'arg 2'". –
jfs Mar 3 '15 at 10:02
So I have a python script that generates a file name based on the time. Then I am trying to cat some data into that file name. However, it seems that I cannot pass it in or something.
Here is what the code looks like:
fileName = "parsedOn_"+strftime("%Y_%m_%d_%H%M%S", gmtime())+".csv"
subprocess.call(['cat' + 'xaa' + '>' + fileName])
Here is the error I get:
Traceback (most recent call last):
File "parseCSV.py", line 96, in <module>
subprocess.call(['cat' + 'xaa' + '>' + finalFile1])
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 595, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 1106, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Any ideas if what I'm trying to do is possible with subprocess at all?
The problem lies in
subprocess.call(['cat' + 'xaa' + '>' + fileName])
First of all, you're missing spaces (if you wanted to use a string), or commas (if you wanted to use a list, the preferred method). Secondly, > is a shell redirection, so you must execute this line in a shell, as in:
subprocess.call('cat xaa > ' + fileName, shell=True)
But you should not do that. Instead, use Python's native shutil.copyfile:
shutil.copyfile('xaa', fileName)
You should consider using envoy.
Sample
In [1]: import envoy
In [2]: r = envoy.run("cat requirements.txt")
In [3]: r.std_out
Out[3]: 'Flask==0.8\ngit+git://github.com/kennethreitz/flask-cache.git\nJinja2==2.6\ngit+git://github.com/kennethreitz/werkze ug.git\ndistribute==0.6.24\ngunicorn==0.13.4\nredis==2.4.9\nvanity==1.1.1\nwsgiref==0.1.2\n'
In [4]: print r.std_out
Flask==0.8
git+git://github.com/kennethreitz/flask-cache.git
Jinja2==2.6
git+git://github.com/kennethreitz/werkzeug.git
distribute==0.6.24
gunicorn==0.13.4
redis==2.4.9
vanity==1.1.1
wsgiref==0.1.2
Edit:
Main advantage of envoy is simplicity.