2013-05-20 147 views
0

我使用python的subprocess.popen来获取视频文件的信息。为什么subprocess.popen返回空字符串

output = Popen('ffmpeg -i "'+avifile+'" -dframes 0 -vframes 0', 
    executable="/bin/bash", stdout=PIPE, stderr=STDOUT, 
    shell=True).communicate()[0] 

事情是每当我运行它的输出变量是一个空字符串,当我知道应该有东西。我可以手动运行ffmpeg。

我在想也许它是一个管道问题,我重新定向。想知道是否有人能解决这个问题。

+0

'output = Popen(...)。communicate()'给你什么?也许你得到一个错误 –

+0

有没有错误,只是没有。 – incognito2

+2

传递参数序列而不是字符串,您将避免引用问题 –

回答

0
cmd = ['ffmpeg','-i',avifile,'-dframes','0','-vframes', '0'] 

output = Popen(cmd, 
    #executable="/bin/bash", ive never seen this used ... you may not need it 
    stdout=PIPE, stderr=PIPE, 
    shell=True).communicate() 

尝试,而不是也许......

+0

Im获取TypeError:execv()arg 2必须只包含字符串。 avifile虽然是一个字符串变量。 – incognito2

+0

什么是avifile? '打印avifile,键入(avifile)' –

+0

这是没有引号的0。但它仍然没有工作。输出变量是空的。 – incognito2

0

要读取的合成标准输出/标准错误作为一个字符串,并得到非零返回状态的异常:

from subprocess import STDOUT, check_output as qx 

output = qx(['ffmpeg', '-i', avifile] + '-dframes 0 -vframes 0'.split(), 
      stderr=STDOUT) 

不要使用shell=True除非你需要它。

+1

...为什么'qx'?我认为这是一个令人困惑的名字,我没有理由用它改变可读的'check_output'。 – Bakuriu

+0

可读性取决于您的背景。 qx(eXecute)的起源是在bash和相应的*'qx(command)'*运算符中在perl中反引号(向后引号)*'\'命令\''*。 – jfs

0

我将变量输出更改为video_output,并且因为某种原因而工作。我完全没有理由。

+0

我的猜测*是您以某种方式覆盖了'output'变量,因此重命名它现在代码不再覆盖它。如果你发布了MWE,那么我们不会浪费时间猜测,但我们可以指出真正的问题。 – Bakuriu

相关问题