【问题标题】:writing multiple sound files into a single file in python在python中将多个声音文件写入一个文件
【发布时间】:2011-05-13 22:28:06
【问题描述】:

我有问题。我有三个声音文件,例如 a.wav、b.wav 和 c.wav。我想将它们写入单个文件,例如 all.xmv(扩展名也可能不同),当我需要时,我想提取其中一个并播放它(例如,我想播放 a.wav 并提取它形成 all.xmv)。

我如何在 python 中做到这一点。我听说 Delphi 中有一个名为 blockwrite 的函数,它可以做我想要的事情。 python中是否有类似Delphi中blockwrite的函数,或者我如何编写这些文件并播放它们?

【问题讨论】:

    标签: python file audio writing


    【解决方案1】:

    【讨论】:

    • @mehmet,困惑 - 您想要的文件与存档文件有何不同?
    • 我会使用 zip 而不是 tar,因为它可以让您直接访问每个条目,而不必跳过前面的条目,这在处理音效时很重要。
    【解决方案2】:

    如果存档的想法(顺便说一句,您的问题的最佳答案)不适合您,您可以将多个文件中的数据融合到一个文件中,例如通过写入连续的二进制数据块(从而创建一个未压缩的存档!)

    让路径是应该连接的文件列表:

    import io
    import os
    
    offsets = [] # the offsets that should be kept for later file navigation
    last_offset = 0
    
    fout = io.FileIO(out_path, 'w')
    for path in paths:
        f = io.FileIO(path) # stream IO
        fout.write(f.read())
        f.close()
        last_offset += os.path.getsize(path)
        offsets.append(last_offset)       
    fout.close()
    
    # Pseudo: write the offsets to separate file e.g. by pickling
    # ...
    
    # reading the data, given that offsets[] list is available
    file_ID = 10                   # e.g. you need to read 10th file
    f = io.FileIO(path)    
    f.seek(offsets[file_ID - 1])   # seek to required position 
    read_size = offsets[filed_ID] - offsets[file_ID - 1]  # get the file size
    data = f.read(read_size)       # here we are! 
    f.close()
    

    【讨论】:

    • 或者存档的内置目录中。
    • 好的,谢谢。事实上我想写一个字典程序,我也想播放声音。当我点击一个词时,我必须听到它的发音。所以会有很多声音文件,我想把它们写到一个文件或几个文件中。我不想使用 zip/tar,因为提取文件然后播放它并从我认为的内存中删除它需要时间。但是使用 zip tar 文件或在 io 模块中使用 fout 哪个更快?
    • 从 'tar' 读取应该足够快,因为 tar 只是原始数据的容器。但是,您可以使用“纯 python”方法,请参阅更新的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多