2016-04-14 27 views

回答

4

有兩個部分給你的問題。

  • 將現有FLAC的音頻文件爲其它格式一樣wav
  • 拆分轉換wav文件轉換成特定大小的塊。

很明顯,有多種方法可以做到這一點。但是,pydub提供了更簡單的方法來完成以上。 有關pydub文檔的詳細信息可以是found here

1)將現有的FLAC音頻文件,其他格式一樣wav

使用pydub您可以閱讀FLAC音頻格式,然後轉換爲wav如下

flac_audio = AudioSegment.from_file("sample.flac", "flac") 
flac_audio.export("audio.wav", format="wav") 

2 )將wav文件拆分成特定大小的塊。

同樣,有多種方法可以做到這一點。我這樣做的方式是確定轉換的總長度和大小wavfile,然後將其近似爲期望的塊大小。

使用的樣本wav file101,612 KB大小和約589 sec或略高於9 minutes

WAV文件的大小通過觀察:

立體聲幀頻爲44.1KHz的音頻文件大約每分鐘10 MB。 48K會稍大一些。這意味着,相應的單文件將是每分鐘5兆

近似認爲有利於我們與about10 MB的樣本文件每分鐘

的Wav文件大小的數學:

有限公司WAV之間的關係http://manual.audacityteam.org/o/man/digital_audio.html

:文件大小和持續時間由

wav_file_size_in_bytes = (sample rate (44100) * bit rate (16-bit) * number of channels (2 for stereo) * number of seconds)/8 (8 bits = 1 byte)

來源給出

我用於計算音頻文件的組塊的公式:通過以下方法

獲取塊大小

for duration_in_sec (X) we get wav_file_size (Y)
So whats duration in sec (K) given file size of 10Mb

這給出K = X * 10Mb/Y

pydub.utils具有方法make_chunks那可以使特定的時間段(in milliseconds)。我們使用上述公式確定期望大小的持續時間。

我們使用它創建10Mb(或接近10Mb)的塊並分別導出每個塊。取決於大小,最後的塊可能會更小。

這是一個工作代碼。

from pydub import AudioSegment 
#from pydub.utils import mediainfo 
from pydub.utils import make_chunks 
import math 

flac_audio = AudioSegment.from_file("sample.flac", "flac") 
flac_audio.export("audio.wav", format="wav") 
myaudio = AudioSegment.from_file("audio.wav" , "wav") 
channel_count = myaudio.channels #Get channels 
sample_width = myaudio.sample_width #Get sample width 
duration_in_sec = len(myaudio)/1000#Length of audio in sec 
sample_rate = myaudio.frame_rate 

print "sample_width=", sample_width 
print "channel_count=", channel_count 
print "duration_in_sec=", duration_in_sec 
print "frame_rate=", sample_rate 
bit_rate =16 #assumption , you can extract from mediainfo("test.wav") dynamically 


wav_file_size = (sample_rate * bit_rate * channel_count * duration_in_sec)/8 
print "wav_file_size = ",wav_file_size 


file_split_size = 10000000 # 10Mb OR 10, 000, 000 bytes 
total_chunks = wav_file_size // file_split_size 

#Get chunk size by following method #There are more than one ofcourse 
#for duration_in_sec (X) --> wav_file_size (Y) 
#So whats duration in sec (K) --> for file size of 10Mb 
# K = X * 10Mb/Y 

chunk_length_in_sec = math.ceil((duration_in_sec * 10000000) /wav_file_size) #in sec 
chunk_length_ms = chunk_length_in_sec * 1000 
chunks = make_chunks(myaudio, chunk_length_ms) 

#Export all of the individual chunks as wav files 

for i, chunk in enumerate(chunks): 
    chunk_name = "chunk{0}.wav".format(i) 
    print "exporting", chunk_name 
    chunk.export(chunk_name, format="wav") 

輸出:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> 
sample_width= 2 
channel_count= 2 
duration_in_sec= 589 
frame_rate= 44100 
wav_file_size = 103899600 
exporting chunk0.wav 
exporting chunk1.wav 
exporting chunk2.wav 
exporting chunk3.wav 
exporting chunk4.wav 
exporting chunk5.wav 
exporting chunk6.wav 
exporting chunk7.wav 
exporting chunk8.wav 
exporting chunk9.wav 
exporting chunk10.wav 
>>> 
相關問題