2013-05-20 121 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

相關問題