- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个函数,其目的是获取 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
更安全.
tempfile.mkstemp(suffix=None, prefix=None, dir=None, text=False)
Creates a temporary file in the most secure manner possible.
...
UnlikeTemporaryFile()
, the user ofmkstemp()
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/
我需要创建一个临时目录来存放另一个命名目录和子文件。最后,指定的目录和子文件将附加到压缩包中,临时目录可以删除。最初打算使用 mkdtemp()但它看起来像 TemporaryDirectory()方
在 Heroku 托管的 Rails 3 应用程序中,一个多部分文件被发布到我的应用程序,我正在尝试使用一些示例代码: File.open(params['filename'][:tempfile].
有谁知道在 JRuby 中是如何处理 tempfile.rb 的。它在 Ruby 中,但不在 JRuby 中。 有人有任何想法吗? 干杯 埃夫 最佳答案 您仍然可以“要求'tempfile'”并且它会
我目前正在 Ruby 上运行这段代码。 file = Tempfile.new(['tempemail', '.html']) file 'xx' 如果没有 file.close,您将得到一个空字符
谁能告诉我在 python 中使用 tempfile 的好处,在什么情况下我应该使用它? 我正在编写一个程序来处理一个文件,它会先调用另一个脚本来处理该文件,生成一个临时文件并执行下一步,我将自己编写
在上传到新目的地之前,我使用 Tempfile 存储生成的 PDF。 pdf_file = WickedPdf.new.pdf_from_string(msgbody) tempfil
我正在尝试创建一个临时文件并向其中写入一些文本。但是我在控制台中得到了这种奇怪的行为 t = Tempfile.new("test_temp") # => # t # t.write("test d
我们的处理服务器主要与 TempFiles 一起工作,因为它使我们这边的事情变得更容易:无需注意删除它们,因为它们会被垃圾收集或处理名称冲突等。 最近,我们遇到了 TempFiles 在此过程中过早进
我正在更改文件的内容,因此我逐行读取文件,替换我想要的内容并逐行写入临时文件。处理整个文件后,我删除原始文件,并将临时文件重命名为原始文件名。像这样 File orginialFile = new F
我正在使用 django 和 Google App Engine。我无法确定错误来自哪里,因为如果我在本地使用它,我的网站就会正常运行。在我将其部署到 GAE 上之后,它工作正常,但是当使用 goog
我正在使用 tempfile.mkstemp 生成一个随机可用的文件名并使用 os.fdopen 写入一些内容。然后我通过 celery 将文件名传递给任务。 此任务打开文件,处理内容,最后删除文件。
我正在使用 tempfile.NamedTemporaryFile()存储一些文本直到程序结束。在 Unix 上工作没有任何问题,但在 Windows 上返回的文件无法读取或写入:python 给出
我一直在使用带有前缀的 tempfile.mkdtemp 来创建我的临时文件。这导致我的 tmp 文件夹中有很多不同的目录,其中包含“tmp/myprefix{uniq-string}/”。 我想改变
我有一个 GUI (lxn/walk) 应用程序修补程序,它通过 ftp 下载文件,将其流式传输到临时文件并提取内容以更新本地文件。删除文件命令被推迟。 除非用户在下载文件时退出程序,否则文件不会被删
在 python 中,您可以创建一个临时文件,如下所示: tempfile.TemporaryFile() 然后你就可以写信给它了。 GNU/Linux 系统中写入的文件在哪里?我似乎无法在/tmp
我正在编写一个 Python 脚本,该脚本需要创建大约 50 个不同的临时文件,这些文件在脚本执行过程中经常被附加并在最后合并。我确信 tempfile 模块可以满足我的需要,但我无法通过阅读文档来弄
无论如何我可以写入临时文件并将其包含在命令中,然后关闭/删除它。我想执行命令,例如:some_command/tmp/some-temp-file. 非常感谢。 import tempfile tem
我在 Python 中使用 tempfile 和 sqlite3 模块。 以下代码有效: import sqlite3, tempfile conn1 = sqlite3.connect(tempfi
根据 tempfile.mkstemp docs , mkstemp() returns a tuple containing an OS-level handle to an open file (
我想在几天内学习Java。我尝试将内容写入临时文件并将该文件保存在 Windows 中的临时文件夹中(这是我的任务)。我找到了一些解决方案,但对我不起作用:( http://www.mkyong.co
我是一名优秀的程序员,十分优秀!