2013-06-25 46 views
1

這裏是Python代碼:分配輸出到一個變量,而不是到輸出文件

import subprocess 

cmnd = ["ffmpeg", "-i", "/home/xincoz/test/connect.flv", "-acodec", "copy", "-ss", "00:00:00", "-t", "00:00:30", "/home/xincoz/test/output.flv"] 

subprocess.call(cmnd) 

在這裏,我得到connect.flv視頻的30秒長的視頻輸出文件output.flv。但是我想將這30秒長的二進制數據存儲在一個變量中,而不是將其複製到輸出文件中。 我怎麼能夠做到這一點?

請幫幫我。提前致謝。

+0

http://stackoverflow.com/questions/2502833/python-store-output-of-subprocess-popen-call -in-a-string – matino

+1

非常感謝@matino – rash

回答

1

你必須告訴ffmpeg輸出到標準輸出,並告訴輸出格式。然後,從蟒蛇,如@matino說,使用POPEN讀什麼內容寫到標準輸出:

cmnd = ["ffmpeg", "-i", "/your/file.avi", "-acodec", "copy", "-ss", "00:00:00", "-t", "00:00:30", "-f", "avi", "pipe:1"] 

p = subprocess.Popen(cmnd, stdout=subprocess.PIPE) 

out, err = p.communicate() 

print len(out) # print the length of the data received 
+1

非常感謝bro @Guillaume。 – rash