Reputation: 517
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.
Upvotes: 4
Views: 4288
Reputation: 1012
(This is not a direct answer to your question) but if you have multiple files to convert you can try something like this: (put your script.py in the same folder)
import os
import subprocess
fileList = os.listdir("path")
for name in fileList:
command = 'ffmpeg -i' + ' ' + str(name) +' '+'-f mp3'+' '+ str(name)+'.mp3'
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = p.communicate()[0]
Please NOTE the variable COMMAND
Upvotes: 1
Reputation: 40374
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.
Upvotes: 8