python-3.x - python 的 ffmpy 是否不适用于使用 tempfile.TemporaryFile 制作的临时文件?

标签 python-3.x audio ffmpeg signal-processing temporary-files

我正在制作一个函数,其目的是获取 mp3文件和 分析处理它。所以,从 this 那里获得帮助所以回答,我正在制作一个临时 wav 文件,然后使用 python ffmpy我正在尝试转换的库 mp3 (实际给定的文件)到 wav 文件。但问题是我将上面生成的临时 wav 文件作为输出文件提供给 ffmpy 以将结果存储到即我正在这样做:

import ffmpy
import tempfile
from scipy.io import wavfile

# audioFile variable is known here

tempWavFile = tempfile.TemporaryFile(suffix="wav")
ff_obj = ffmpy.FFmpeg(
                      global_options="hide_banner",
                      inputs={audioFile:None},
                      outputs={tempWavFile: " -acodec pcm_s16le -ac 1 -ar 44000"}
                     )

ff_obj.run()

[fs, frames] = wavfile.read(tempWavFile)
print(" fs is: ", fs)
print(" frames is: ", frames)

但是在线ff_obj.run()发生此错误:
File "/home/tushar/.local/lib/python3.5/site-packages/ffmpy.py", line 95, in run
    stderr=stderr
  File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1490, in _execute_child
    restore_signals, start_new_session, preexec_fn)
TypeError: Can't convert '_io.TextIOWrapper' object to str implicitly

所以,我的问题是:
  • 当我更换 tempWavFile = tempfile.TemporaryFile(suffix="wav")tempWavFile = tempfile.mktemp('.wav') ,没有发生错误,为什么会这样?
  • 这个错误是什么意思,它发生的原因是什么,如何纠正?
  • 最佳答案

    tempfile.TemporaryFile返回一个类似文件的对象:

    >>> tempWavFile = tempfile.TemporaryFile(suffix="wav")
    >>> tempWavFile
    <_io.BufferedRandom name=12>
    

    另一方面,tempfile.mktemp返回一个字符串,指向刚刚在文件系统上创建的真实文件的路径:
    >>> tempWavFile = tempfile.mktemp('.wav')
    >>> tempWavFile
    '/var/folders/f1/9b4sf0gx0dx78qpkq57sz4bm0000gp/T/tmpf2117fap.wav'
    

    创建后tempWavFile ,您将其传递给 ffmpy.FFmpeg ,它将在单个命令中聚合输入和输出文件和参数,以传递给 subprocess .命令行采用列表的形式,可能类似于以下内容:["ffmpeg", "-i", "input.wav", "output.wav"] .

    最后,ffmpy将此列表传递给 subprocess.Popen这就是当你使用 tempfile.TemporaryFile 时它会爆炸的地方.这是正常的,因为 subprocess对您的论点一无所知,并希望它们都是字符串。当它看到 _io.BufferedRandom tempfile.TemporaryFile 返回的对象,它不知道该怎么办。

    因此,要修复它,只需使用 tempfile.mkstemp , 这比 tempfile.TemporaryFile 更安全.

    来自 the Python docs :

    tempfile.mkstemp(suffix=None, prefix=None, dir=None, text=False)
    Creates a temporary file in the most secure manner possible.
    ...
    Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it.



    您最初提到mktemp ,自 Python 2.3 起已弃用(参见 docs ),应替换为 mkstemp .

    关于python-3.x - python 的 ffmpy 是否不适用于使用 tempfile.TemporaryFile 制作的临时文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44434707/

    相关文章:

    video - 如何从 TS 视频中裁剪最后 N 秒

    stream - 如何使用 ffmpeg 创建流式音频文件?

    python - 如何在 Alpine 上安装 matplotlib

    ios - 按下 UITableView "cell"时在文档中加载音频

    actionscript-3 - 我可以使用 ffmpeg 和 actionscript 将网络摄像头视频 flv 转换为 mp4 吗?

    apache-flex - Flex中的音频播放器

    c - Gstreamer 客户端服务器音频流

    python - TypeError at/admin/jobs/job/add/'tuple' 不支持缓冲区接口(interface)

    python - 使用 multiprocessing.dummy 并行请求

    Python3 Tkinter 文本小部件在同一行插入