[英]Python capture subprocess output
我正在研究一個從音頻流中學習的tensorflow項目。 我正在使用子處理模塊(帶有Popen
)和FFMPEG來從mp3中讀取音頻數據。 我使用Popen()
成功打開了音頻文件,並且可以通過stdout
打印輸出。 但是,我似乎無法捕獲它。
我已經嘗試過read()
和communicate()
我在這里關注一個教程
read()
僅返回任何內容,而communicate()
引發錯誤: AttributeError: 'file' object has no attribute 'communicate'
這是我的代碼:
for image_index, image in enumerate(image_files):
count += 1
image_file = os.path.join(folder, image)
try:
output_files = "output/output" + str(count) + ".png"
if image_file != 'train/rock/.DS_Store':
command = [FFMPEG_BIN,
'-i', image_file,
'-f', 's16le',
'-acodec', 'pcm_s16le',
'-ar', '44100',
'-ac', '2',
output_files]
pipe = sp.Popen(command, stdout=sp.PIPE)
print (pipe)
raw_audio = pipe.stdout.communicate(88200*4)
Popen對象具有無法通信的stdout :
pipe.communicate(str(88200*4))
還可以通過stdout捕獲stderr:
pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
raw_audio, _ = pipe.communicate(str(88200*4).encode())
print(raw_audio)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.