分割一个音频文件到任意大小的块 [英] Split an audio file into pieces of an arbitrary size

查看:255
本文介绍了分割一个音频文件到任意大小的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的声音文件(150 MB),我想拆分成一些更容易管理的大小的小文件,也就是说,用5分钟的音频文件。显然,最后一段将是< = 5分钟,这是确定。有没有一种方法能够轻而易举地完成这类任务?

用于这个问题的一个小样本的.mp3文件可以使用这个链接下载:download.linnrecords.com/test/mp3/recit.aspx。

下面是我到目前为止已经试过。我进口使用 readMP3 调谐器的数据,并打算使用 cutw 的功能,但还没有找到使用它的有效方法。

 库(TUNER)样品< -readMP3(recit.mp3)#此文件只9.04秒长(44.1赫兹,16位,斯特里奥)
#所以,对于这个例子,我们可以把它切成以0.5秒间隔)
subsamp1&所述; -cutw(样品,从= 0,= 0.5,输出=波形)#那么我将不得不为每个间隔长达做到这一点:
subsampn< -cutw(样品,从= 9,以9.04 =,=输出波浪)
#在那里我有明确的状态下的最大秒(即9.04)
#除非有一种方法,我不知道来提取这些信息。

此方法效率低时的时间间隔相比,在总文件长度变小。此外,样品是立体的,但 subsamp1 是单声道的,而且我preFER不改变任何东西数据,如果可能的。

在提高效率的方式,我想从和参数输入向量到,但我得到了错误(见下文)。即使工作过,虽然它不会是一个特别好的解决方案。任何人都知道一个更优雅的方式,使用R解决这个问题呢?

  cutw(subsamp1,从SEQ =(0,9,0.5),以= C(SEQ(0.5,9.0,0.5),9.04)
#必须明确提供最大秒(即9.04)。
#必须有更好的方式来获取最大第二错误波[A:B,]:下标越界
另外:警告信息:
1:在如果(从>到)停止('从'不能优于'到'):
  条件有长度GT; 1,仅将使用的第一元件
2:如果在(从== 0){:
  条件有长度GT; 1,仅将使用的第一元件
3:A:B:数值前pression有19个元素:只用第一


解决方案

我没有任何与R中的音频文件的工作经验,但我可以想出一种方法,可以帮助你。看看下面的code。

 库(seewave)#您的音频文件(使用seewave包示例文件)
数据(TICO)
音频< - 天工
#音频文件的频率
频率< - 22050
#您的音频文件的长度和持续时间
totlen< - 长度(音频)
totsec< - totlen /频率#要文件切成时间
seglen< - 0.5#定义破发点
休息< - 独特的(C(SEQ(0,totsec,seglen),totsec))
指数< - 1:(长度(时间)-1)
#所有段的列表
subsamps&下; - lapply(指数,函数(ⅰ)cutw(音频中,f =频率,从=符[I]中,以=场所第[i + 1]))

I have a large sound file (150 MB) that I would like to split into smaller files of some more easily managed size, say, files with 5 minutes of audio. Clearly, the last segment is going to be <= 5 minutes, and that's OK. Is there a way to do this sort of task easily?

A small sample .mp3 file to be used for this problem can be downloaded using this link: download.linnrecords.com/test/mp3/recit.aspx.

Here is what I have tried so far. I imported the data using readMP3 from tuneR and was going to use the cutw function, but haven't found an efficient way of using it.

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.

This approach is inefficient when intervals become small in comparison to the total file length. Also, sample was stereo, but subsamp1 is mono, and I'd prefer not to change anything about the data if possible.

In the way of improving efficiency, I tried inputting vectors to the from and to arguments, but I got an error (see below). Even if it had worked, though, it would not be a particularly nice solution. Anyone know of a more elegant way to approach this problem using 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

解决方案

I don't have any experience working with audio files in R, but I was able to come up with an approach that might help you. Check out the code below.

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]))

这篇关于分割一个音频文件到任意大小的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆