[英]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.