为什么从python调用ffmpeg块? [英] Why does calling ffmpeg from python block?

查看:64
本文介绍了为什么从python调用ffmpeg块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了3种方法来从python调用ffmpeg,但是它始终会阻塞并且不会返回任何结果.

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

但是,如果我从shell中执行它,它将起作用.

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

例如:

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

这有效.

但是,

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

这会使python挂起.

This makes the python hang.

推荐答案

您应使用 subprocess.Popen 而不是 os.popen .

特别是,要获得相同的行为,您可以使用 shell = True 在shell中运行该过程,并从 stdout stderr 收集输出.代码>,如下所示:

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]

其中 command 与您在shell中编写的命令行相同.

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

这篇关于为什么从python调用ffmpeg块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆