gillesC
gillesC

Reputation: 749

Python os.system timeout with strings

I'm unable to execute the following line:

os.system("timeout 1s bash -c \"ffmpeg -i \""+path+\"+" | <some_<other_cmd>\"")

So the purpose of this command is to set a timeout for the whole command, i.e. pipelining some ffmpeg information from a path.

The problem is because bash -c "CMD" is expected, but the command also contains " ".

Is there another way of defining the \"path\", because the path can contain spaces? Or another solution which can resolve my problem?

Thanks in advance!

Upvotes: 2

Views: 2035

Answers (2)

crifan
crifan

Reputation: 14328

Has answered similar question in other posts: 1 and 2

you can use subprocess related functions, which all support timeout parameter, to replace os.system

such as subprocess.check_output

ffmpegCmd = "ffmpeg -I %s | %s" % (path, someOtherCmd)
outputBytes = subprocess.check_output(ffmpegCmd, shell=True, timeout=1)
outputStr = outputBytes.decode("utf-8") # change utf-8 to your console encoding if necessary

Upvotes: 0

LetMeSOThat4U
LetMeSOThat4U

Reputation: 6786

Triple sinqle quotes can do the trick (so that you do not have to escape doublequotes):

os.system('''timeout 1s bash -c "ffmpeg -i "+path+"+" | cat''')

But in general.. Why not use subprocess.call that has saner syntax?

Upvotes: 2

Related Questions