【问题标题】:Split an audio file into pieces of an arbitrary size将音频文件拆分为任意大小的片段
【发布时间】:2013-12-20 05:08:04
【问题描述】:

我有一个较大的声音文件 (150 MB),我想将其拆分为更易于管理的较小文件,例如 5 分钟音频的文件。显然,最后一段将是

可以使用以下链接下载用于此问题的小示例 .mp3 文件:download.linnrecords.com/test/mp3/recit.aspx。

这是我到目前为止所尝试的。我使用readMP3tuneR 导入了数据,并打算使用cutw 函数,但还没有找到一种有效的使用方法。

library(tuneR)

sample<-readMP3("recit.mp3") 

# the file is only 9.04 seconds long (44.1 Hz, 16-bit, sterio)
# so, for this example we can cut it into 0.5 second intervals)
subsamp1<-cutw(sample, from=0, to=0.5, output="Wave")

# then I would have to do this for each interval up to:
subsampn<-cutw(sample, from=9, to=9.04, output="Wave") 
# where I have to explicitly state the maximum second (i.e. 9.04), 
# unless there is a way I don't know of to extract this information.

当间隔与文件总长度相比变小时,这种方法效率低下。另外,sample 是立体声,但 subsamp1 是单声道,如果可能的话,我不希望对数据进行任何更改。

为了提高效率,我尝试在fromto 参数中输入向量,但出现错误(见下文)。但是,即使它起作用了,它也不是一个特别好的解决方案。有人知道使用 R 解决这个问题的更优雅的方法吗?

cutw(subsamp1,from=seq(0,9,0.5),to=c(seq(0.5,9.0,0.5),9.04) 
# had to explicitly supply the max second (i.e. 9.04). 
# must be a better way to extract the maximum second

Error in wave[a:b, ] : subscript out of bounds
In addition: Warning messages:
1: In if (from > to) stop("'from' cannot be superior to 'to'") :
  the condition has length > 1 and only the first element will be used
2: In if (from == 0) { :
  the condition has length > 1 and only the first element will be used
3: In a:b : numerical expression has 19 elements: only the first used

【问题讨论】:

  • 你可以看看mapply
  • 否,但对这类问题会很方便。

标签: r file-io split audio


【解决方案1】:

在@Jean V. Adams 的出色回答的基础上,我找到了一个使用索引的解决方案(即[)。

library(seewave)

# your audio file (using example file from seewave package)
data(tico)
audio <- tico
# the frequency of your audio file
freq <- 22050
# the length and duration of your audio file
totlen <- length(audio)
totsec <- totlen/freq

# the duration that you want to chop the file into
seglen <- 0.5

# defining the break points
breaks <- unique(c(seq(0, totsec, seglen), totsec))
index <- 1:(length(breaks)-1)
# a list of all the segments
lapply(index, function(i) audio[(breaks[i]*freq):(breaks[i+1]*freq)])
# the above final line is the only difference between this code and the 
# code provided by @Jean V. Adams

这里的优点是,如果您的输入音频对象是立体声,则返回的对象也是立体声。 cutw 将输出对象更改为单声道,据我所知。

【讨论】:

    【解决方案2】:

    我没有在 R 中处理音频文件的任何经验,但我想出了一种可能对您有所帮助的方法。查看下面的代码。

    library(seewave)
    
    # your audio file (using example file from seewave package)
    data(tico)
    audio <- tico
    # the frequency of your audio file
    freq <- 22050
    # the length and duration of your audio file
    totlen <- length(audio)
    totsec <- totlen/freq
    
    # the duration that you want to chop the file into
    seglen <- 0.5
    
    # defining the break points
    breaks <- unique(c(seq(0, totsec, seglen), totsec))
    index <- 1:(length(breaks)-1)
    # a list of all the segments
    subsamps <- lapply(index, function(i) cutw(audio, f=freq, from=breaks[i], to=breaks[i+1]))
    

    【讨论】:

      【解决方案3】:

      检查https://github.com/schultzm/SliceAudio.py 我编写了这个脚本来做与这个问题中所要求的非常相似的事情,但我是用 python 编写的。不确定它是否仍然相关,但无论如何这是我的解决方案。如果需要,您可以从 R 中启动 python 脚本。

      python 脚本会沿着文件的长度对音频文件进行切片(如果需要,可以分批),直到到达文件的末尾。默认情况下,它将一个文件分成 2 秒的块,每个块从下一个块的末尾开始,每个块输出为一个单独的文件(到包含输入文件的文件夹中;文件输出名称与输入相同,但带有添加到输出文件名的原始文件中的位置)。输出切片的默认格式是 16 位、48kHz、单声道。用户可以将样本压碎至 8 位宽度或将其设置为中等(16 位)或高质量(32 位)。采样率可以是从低质量(11025 Hz)到高质量(48000 Hz)的任何地方——事实上,采样率可以是任何你想要的,但你的计算机可能不知道如何处理那些非标准的速率(例如,我测试过它以 1 Hz 的频率播放,iTunes 在尝试播放时死机 - 请参阅标准/接受选项的帮助菜单 [python SliceAudio.py -h] )。用户还可以更改样本切片长度和前一个切片上的重叠幻灯片(例如,您可以切片为 10 秒的窗口,每个后续窗口滑动 1 秒以将前一个窗口重叠 1 秒。注意时间以毫秒,因此将 x 秒乘以 1000 以获得所需的切片长度(以秒为单位)。有一个立体声输出选项。该脚本可以输入和输出ffmpeg**支持的任何格式。

      依赖: 1.海合会 2.pydub(sudo pip install pydub),见github.com/jiaaro/pydub 3. ffmpeg (brew install libav --with-libvorbis --with-sdl --with-theora) 4. 音频阅读(sudo pip install audioread)

      示例用法:python SliceAudio.py -i xyz.m4a -f m4a -b 2 -s 11025 -l 10000 python SliceAudio.py -h

      **ffmpeg 格式:trac.ffmpeg.org/wiki/audio%20types

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-23
        • 2021-01-18
        • 1970-01-01
        • 1970-01-01
        • 2021-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多