繁体   English   中英

Python捕获子流程输出

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM