【问题标题】:Split audio on timestamps librosa在时间戳 librosa 上拆分音频
【发布时间】:2020-02-07 22:04:13
【问题描述】:

我有一个音频文件,我想每 2 秒拆分一次。有没有办法用 librosa 做到这一点?

因此,如果我有一个 60 秒的文件,我会将其拆分为 30 个 2 秒的文件。

【问题讨论】:

    标签: python audio librosa


    【解决方案1】:

    librosa 首先是一个用于音频分析的库,而不是音频合成或处理。提供了对编写简单音频文件的支持(参见here),但也有说明:

    此功能在 librosa 0.7.0 中已弃用。它将在 0.8 中删除。 write_wav 的用法应替换为soundfile.write

    鉴于这些信息,我宁愿使用sox 之类的工具来拆分音频文件。

    来自"Split mp3 file to TIME sec each using SoX"

    你可以像这样运行 SoX:

     sox file_in.mp3 file_out.mp3 trim 0 2 : newfile : restart
    

    它将创建一系列文件,每个文件都有 2 秒的音频块。

    如果您更愿意留在 Python 中,您可能希望使用 pysox 来完成这项工作。

    【讨论】:

      【解决方案2】:

      您可以使用运行以下代码(未经测试)的 librosa 拆分文件。我添加了必要的 cmets,以便您了解所执行的步骤。

      # First load the file
      audio, sr = librosa.load(file_name)
      
      # Get number of samples for 2 seconds; replace 2 by any number
      buffer = 2 * sr
      
      samples_total = len(audio)
      samples_wrote = 0
      counter = 1
      
      while samples_wrote < samples_total:
      
          #check if the buffer is not exceeding total samples 
          if buffer > (samples_total - samples_wrote):
              buffer = samples_total - samples_wrote
      
          block = audio[samples_wrote : (samples_wrote + buffer)]
          out_filename = "split_" + str(counter) + "_" + file_name
      
          # Write 2 second segment
          librosa.output.write_wav(out_filename, block, sr)
          counter += 1
          samples_wrote += buffer
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-09
        • 2021-03-13
        • 1970-01-01
        相关资源
        最近更新 更多