使用Python tempfiles,为什么是.mkstemp工作正常时会给我错误

发布于 2025-01-23 22:09:52 字数 1977 浏览 5 评论 0 原文

我试图使用Sox Transformer编写一些代码以首先处理两个音频示例,然后使用Sox Combiner组合两个音频文件。我还试图在内存中首先进行处理以加快事情的速度。

使用 .temporaryFile() .pooledTemporaryFile()时,我会遇到此错误

trackback(最近的最新调用):文件“ ...”,第128行,in< module> generate_new_audio_files(user_library_folder_pathlist,new_folder_path,file_count)文件“ ...”,第101行,在generate_new_new_audio_files bombine_two_files_files_mild(sample1_file_path,sampe2_file_path,sampe2_file_path,new_folder_pathiles),new_folder_ pather in ... 1_audio = tempfile.spooledtemporaryfile( )value error:没有足够的值解开包装(预期2,获得0)

这是我的代码:

def combine_two_files_mild(file_path_1, file_path_2, parent_folder_path, count=None):
    combiner = sox.Combiner()
    tfm1 = sox.Transformer()
    tfm2 = sox.Transformer()
    tfm1.set_input_format(rate=44100)
    tfm2.set_input_format(rate=44100)
    combiner.set_input_format(channels=[2, 2])
    combiner.gain(normalize=True)
    # will eventually insert here some randomized settings for the two transformers

    # this determins the file category based on it's file name
    file_main_category, file_sub_category = Category.get_file_category(file_path_1)

    sorted_file_folder = parent_folder_path + "/" + file_main_category + "/" + file_sub_category
    new_file_path = sorted_file_folder + "/" + file_sub_category + " Combined " + \
                                                        str(count + 1) + " Mild.wav"

    # load the audio data for the two samples
    _, sample1_audio = tempfile.SpooledTemporaryFile()
    tfm1.build(file_path_1, sample1_audio)
    _, sample2_audio = tempfile.SpooledTemporaryFile()
    tfm2.build(file_path_2, sample2_audio)

    # combine the two files and write to disk
    combiner.build([sample1_audio, sample2_audio], new_file_path, 'mix-power',
                   input_volumes=[0.5, 0.5])

    # clear the 'memory'
    os.remove(sample1_audio)
    os.remove(sample2_audio)

Im trying to write some code to first process two audio samples using sox transformer, then combine the two audio files using sox combiner. Im also trying to do the processing first in memory to speed things up.

I am getting this error when using .TemporaryFile() and .SpooledTemporaryFile() but it works fine when using .mkstemp():

Traceback (most recent call last): File "...", line 128, in <module> generate_new_audio_files(user_library_folder_pathlist, new_folder_path, file_count) File "...", line 101, in generate_new_audio_files combine_two_files_mild(sample1_file_path, sample2_file_path, new_folder_path, count) File "...", line 30, in combine_two_files_mild _, sample1_audio = tempfile.SpooledTemporaryFile() ValueError: not enough values to unpack (expected 2, got 0)

Here is my code:

def combine_two_files_mild(file_path_1, file_path_2, parent_folder_path, count=None):
    combiner = sox.Combiner()
    tfm1 = sox.Transformer()
    tfm2 = sox.Transformer()
    tfm1.set_input_format(rate=44100)
    tfm2.set_input_format(rate=44100)
    combiner.set_input_format(channels=[2, 2])
    combiner.gain(normalize=True)
    # will eventually insert here some randomized settings for the two transformers

    # this determins the file category based on it's file name
    file_main_category, file_sub_category = Category.get_file_category(file_path_1)

    sorted_file_folder = parent_folder_path + "/" + file_main_category + "/" + file_sub_category
    new_file_path = sorted_file_folder + "/" + file_sub_category + " Combined " + \
                                                        str(count + 1) + " Mild.wav"

    # load the audio data for the two samples
    _, sample1_audio = tempfile.SpooledTemporaryFile()
    tfm1.build(file_path_1, sample1_audio)
    _, sample2_audio = tempfile.SpooledTemporaryFile()
    tfm2.build(file_path_2, sample2_audio)

    # combine the two files and write to disk
    combiner.build([sample1_audio, sample2_audio], new_file_path, 'mix-power',
                   input_volumes=[0.5, 0.5])

    # clear the 'memory'
    os.remove(sample1_audio)
    os.remove(sample2_audio)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2025-01-30 22:09:52

spooledtemporaryFile 的API不同于 mkstemp mkstemp 返回两个对象的元组(您将其设置为 _ sample1_audio )。

spooledtemporaryfile 让您使用。通常 spooledtemporaryFile 临时file 被用作上下文管理器,例如:

with tempfile.SpooledTemporaryFile() as sample1_audio:
    tfm1.build(file_path_1, sample1_audio)

请注意,在使用 block block之后,临时文件将被删除,因此要么保留您需要此块中需要临时文件的所有代码,也可以手动处理文件打开和关闭:

sample1_audio = tempfile.SpooledTemporaryFile()
tfm1.build(file_path_1, sample1_audio)

... # Other stuff you want to do

sample1_audio.close()

此外, tempfile 模块是专门为创建临时文件而自动删除的临时文件不再需要,因此您不应该在最后需要 os.remove 的电话!

The API for SpooledTemporaryFile is different from mkstemp. mkstemp returns a tuple of two objects (which you are setting as _ and sample1_audio respectively).

SpooledTemporaryFile will return a single file-like object for you to use. Usually SpooledTemporaryFile and TemporaryFile are used as context managers like so:

with tempfile.SpooledTemporaryFile() as sample1_audio:
    tfm1.build(file_path_1, sample1_audio)

Note that after the with block, the temporary file will be deleted, so either keep all of your code that requires the temporary files in this block, or you can manually handle the file opening and closing yourself:

sample1_audio = tempfile.SpooledTemporaryFile()
tfm1.build(file_path_1, sample1_audio)

... # Other stuff you want to do

sample1_audio.close()

Additionally, the tempfile module is specifically made to create temporary files which are removed automatically when they are no longer needed, so you should not need your calls to os.remove at the end!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文