【问题标题】:Python capture subprocess outputPython捕获子进程输出
【发布时间】:2016-07-21 18:28:03
【问题描述】:

我正在开发一个从音频流中学习的 tensorflow 项目。我正在使用 subprocess 模块(使用Popen)和 FFMPEG 从 mp3 中读取音频数据。我用Popen() 成功打开了音频文件,我可以通过stdout 打印输出。但是,我似乎无法捕捉到它。

read()communicate() 我都试过了

我正在关注一个教程here

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)

herehere 我都试过了

【问题讨论】:

    标签: python audio ffmpeg subprocess popen


    【解决方案1】:

    Popen 对象没有通信stdout

    pipe.communicate(str(88200*4))
    

    还要通过标准输出捕获标准错误:

     pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
     raw_audio, _  = pipe.communicate(str(88200*4).encode())
     print(raw_audio)
    

    【讨论】:

    • 谢谢!我正用头撞墙,当然这很简单。 print(raw_audio) 打印 (' ', None) 是否有原因?
    • @KendallWeihe,试试加stderr=sp.STDOUT
    • Popen?现在它只是以这个作为最后一个输出<subprocess.Popen object at 0x10f3cdf50>
    • @KendallWeihe,你知道在进程返回之前通信会阻塞吗?您看到subprocess.Popen object at 0x10f3cdf50> 的唯一方法是,如果您打印的是管道而不是 raw_audio,那么您的最终目标是写入文件吗?
    • 我的目标是打印 ffmpeg 读取的原始数据,以便我可以确认它实际上正在读取一些东西
    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 2017-09-17
    • 2018-04-12
    • 2011-02-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多