2013-12-20 116 views
6

我有一個很大的聲音文件(150 MB),我想將它分割爲一些更容易管理的大小的較小文件,例如帶有5分鐘音頻的文件。顯然,最後一部分將是< = 5分鐘,沒關係。有沒有辦法輕鬆完成這種任務?將音頻文件分割成任意大小的片段

可以使用此鏈接下載用於此問題的小示例.mp3文件:download.linnrecords.com/test/mp3/recit.aspx。

這是我到目前爲止嘗試過的。我從tuneR使用readMP3導入數據,並打算使用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 
+0

你可以看看到'mapply' – Dason

+0

不,但它會b方便這類問題。 – Dason

回答

2

我沒有任何使用R語言處理音頻文件的經驗,但是我能夠想出一個可能對您有幫助的方法。看看下面的代碼。由@Jean V.亞當斯優異答案

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

大廈,我發現使用索引(即[)中的溶液。

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將輸出對象更改爲單聲道。

+0

看起來像你自己解決了立體聲/單聲道問題。尼斯。 –

0

檢查https://github.com/schultzm/SliceAudio.py 我寫了這個腳本來做一個非常相似的事情,就像在這個問題中提出的那樣,但是我用python寫了這個腳本。不知道它是否仍然相關,但無論如何這都是我的解決方案。如果需要,您可以從R內啓動Python腳本。

python腳本沿着文件的長度切割音頻文件(如果需要的話批量處理),直到到達文件末尾。默認情況下,它會將文件分割成2秒的塊,每個塊從下一個塊的末尾開始,每個塊輸出爲一個單獨的文件(輸入到包含輸入文件的文件夾中;文件輸出名稱按照輸入,但與將原始文件中的位置添加到輸出文件名稱中)。輸出切片的默認格式是16位,48kHz,單聲道。用戶可以將樣本壓碎爲8位寬度,或者將其設置爲中等(16位)或高質量(32位)。採樣率可以從低質量(11025Hz)到高質量(48000Hz)的任何地方 - 實際上,採樣率可以是任何你想要的,但是你的計算機可能不知道如何處理那些非標準的速率(例如,我測試過它與1赫茲和iTunes試圖播放時死了 - 請參閱標準/接受選項的幫助菜單[python SliceAudio.py -h])。用戶還可以改變樣本切片長度和前一個切片上的重疊切片(例如,您可以切片成10秒的切片,每個後續切片沿1秒切片,與前一個切片重疊1秒鐘。毫秒,所以將x-乘以1000,以秒爲單位獲得所需的切片長度)。有一個立體聲輸出選項。該腳本可以輸入和輸出ffmpeg **支持的任何格式。

依賴關係: 1. GCC 2. pydub(sudo pip install pydub),見github.com/jiaaro/pydub 3. ffmpeg的(brew install libav --with-libvorbis --with-sdl --with-theora) 4. audioread(sudo pip install audioread

實例:蟒SliceAudio。 PY -i xyz.m4a -f M4A -B 2 -S 11025 -l 10000蟒蛇SliceAudio.py -h

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

相關問題