Why calling ffmpeg from a python block?

I tried 3 methods for calling ffmpeg from python, but it always blocks and returns no result.

However, if I run it from the shell, it works.

For instance,

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

it works.

but

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

This causes python to hang.

+4
source share
2 answers

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

In particular, to get the same behavior, you can start the process through the shell using shell=True and collect 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 should write in the shell.

+7
source

(This is not a direct answer to your question), but if you have several 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] 

Note the variable COMMAND

+1
source

Source: https://habr.com/ru/post/1398190/


All Articles