Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does calling ffmpeg from python block?

Tags:

python

ffmpeg

I tried 3 methods to call ffmpeg from python, but it always blocks and doesn't return any result.

However, if I execute it from shell, it works.

For eg:

/usr/bin/ffmpeg -y  -i /tmp/uploadedfiles/movie8_15_10s.mpg -ar 1600 -ac 1  /tmp/uploadedfiles/movie8_15_10s.mpg.wav

this works.

However,

 ffmpeg_command = "/usr/bin/ffmpeg -y  -i test.wav testout.wav" 
 f_ffmpeg=os.popen(ffmpeg_command);

This makes the python hang.

like image 630
rosh Avatar asked Jan 18 '23 09:01

rosh


1 Answers

You should use subprocess.Popen instead of os.popen.

In particular, to get same behaviour, you can run the process through a shell with shell=True and gather the output from stdout and stderr as follows:

p = subprocess.Popen(command, shell=True,
                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = p.communicate()[0]

where command is the same command line you would write in a shell.

like image 145
jcollado Avatar answered Jan 28 '23 20:01

jcollado