简体   繁体   English

Python捕获子流程输出

[英]Python capture subprocess output

I'm working on a tensorflow project that learns from an audio stream. 我正在研究一个从音频流中学习的tensorflow项目。 I'm using the subprocess module (with Popen ) and FFMPEG to read in the audio data from an mp3. 我正在使用子处理模块(带有Popen )和FFMPEG来从mp3中读取音频数据。 I successfully open the audio file with Popen() and I can print the output through stdout . 我使用Popen()成功打开了音频文件,并且可以通过stdout打印输出。 However, I can't seem to capture it. 但是,我似乎无法捕获它。

I have tried both read() and communicate() 我已经尝试过read()communicate()

I'm following a tutorial here 我在这里关注一个教程

read() simply returns nothing and communicate() throws the error: AttributeError: 'file' object has no attribute 'communicate' read()仅返回任何内容,而communicate()引发错误: AttributeError: 'file' object has no attribute 'communicate'

Here's my code: 这是我的代码:

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)

I've tried everything here and here 我在这里这里都尝试过一切

The Popen object has communicate not stdout : Popen对象具有无法通信的stdout

pipe.communicate(str(88200*4))

To also capture stderr through stdout: 还可以通过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.

相关问题 Python 子进程无法捕获 Windows 程序的输出 - Python subprocess cannot capture output of Windows program python subprocess.Popen捕获大量输出 - python subprocess.Popen capture lots of output 终止后Python捕获子进程输出 - Python capture subprocess output after termination python popen可以捕获子流程的子流程的交互式输出吗? - Can python popen capture interactive output of a subprocess of a subprocess 使用 Python 中的 subprocess.check_output 捕获日志记录 output - Capture logging output using subprocess.check_output in Python 如何捕获从python子进程运行的git clone命令的输出 - How to capture the output of git clone command running from python subprocess 有没有办法在 Python 3 中从 subprocess.run 流式传输和捕获输出? - Is there a way to both stream and capture output from subprocess.run in Python 3? Python子进程:捕获ffmpeg的输出并对其执行正则表达式 - Python subprocess: capture output of ffmpeg and run regular expression against it 如何从subprocess.communicate()捕获python中的流输出 - How to capture streaming output in python from subprocess.communicate() Python subprocess.communicate()无法捕获简单二进制文件的输出 - Python subprocess.communicate() does not capture output from simple binary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM