Python捕获子流程输出

Ken*_*ihe 1 python audio subprocess ffmpeg popen

我正在研究一个从音频流中学习的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)
Run Code Online (Sandbox Code Playgroud)

我在这里这里都尝试过一切

Pad*_*ham 6

POPEN对象沟通没有 标准输出

pipe.communicate(str(88200*4))
Run Code Online (Sandbox Code Playgroud)

还可以通过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)
Run Code Online (Sandbox Code Playgroud)