如何将一个 .wav 文件分割成多个 .wav 文件?
How to split a .wav file into multiple .wav files?
我有一个几分钟长的 .wav 文件,我想将其分成不同的 10 秒 .wav 文件。
到目前为止,这是我的 python 代码:
import wave
import math
def main(filename, time):
read = wave.open(filename, 'r')
#get sample rate
frameRate = read.getframerate()
#get number of frames
numFrames = read.getnframes()
#get duration
duration = numFrames/frameRate
#get all frames as a string of bytes
frames = read.readframes(numFrames)
#get 1 frame as a string of bytes
oneFrame = read.readframes(1)
#framerate*time == numframesneeded
numFramesNeeded=frameRate*time
#numFramesNeeded*oneFrame=numBytes
numBytes = numFramesNeeded*oneFrame
#splice frames to get a list strings each representing a 'time' length
#wav file
x=0
wavList=[]
while x+time<=duration:
curFrame= frames[x:x+time]
x=x+time
wavList.append(curFrame)
打印 wavList
产量:
['\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00']
我知道这是帧列表。我如何为这个列表中的每个元素制作一个 wav 文件(第一个 .wav 文件将是 '\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00'
?Python 的 wave
模块不清楚使用帧来创建 .wav 文件.
编辑:这是 的重复问题
但是,如果有人有不需要的答案pydub
我很想看看
这是一个 python 代码片段,我根据需要使用它来拆分文件。
我使用 https://github.com/jiaaro/pydub 中的 pydub 库。
您可以修改代码段以满足您的要求。
from pydub import AudioSegment
t1 = t1 * 1000 #Works in milliseconds
t2 = t2 * 1000
newAudio = AudioSegment.from_wav("oldSong.wav")
newAudio = newAudio[t1:t2]
newAudio.export('newSong.wav', format="wav") #Exports to a wav file in the current path.
我写了一个class来简化整个过程。
尽管它用于 wav
个文件。
Here it is:
from pydub import AudioSegment
import math
class SplitWavAudioMubin():
def __init__(self, folder, filename):
self.folder = folder
self.filename = filename
self.filepath = folder + '\' + filename
self.audio = AudioSegment.from_wav(self.filepath)
def get_duration(self):
return self.audio.duration_seconds
def single_split(self, from_min, to_min, split_filename):
t1 = from_min * 60 * 1000
t2 = to_min * 60 * 1000
split_audio = self.audio[t1:t2]
split_audio.export(self.folder + '\' + split_filename, format="wav")
def multiple_split(self, min_per_split):
total_mins = math.ceil(self.get_duration() / 60)
for i in range(0, total_mins, min_per_split):
split_fn = str(i) + '_' + self.filename
self.single_split(i, i+min_per_split, split_fn)
print(str(i) + ' Done')
if i == total_mins - min_per_split:
print('All splited successfully')
Usage
folder = 'F:\My Audios\Khaled'
file = 'Khaled Speech.wav'
split_wav = SplitWavAudioMubin(folder, file)
split_wav.multiple_split(min_per_split=1)
就是这样!它会将 single wav
文件拆分为 multiple wav
个文件,每个文件的持续时间为 1 minute
。最后一个分割音频的持续时间可能少于 1 分钟 ;)
Note: If you're in Mac/Linux, then change \
to /
对于那些严格按照 wave
模块来寻求答案的人。
import wave
# times between which to extract the wave from
start = 5.2 # seconds
end = 78.3 # seconds
# file to extract the snippet from
with wave.open('my_in_file.wav', "rb") as infile:
# get file data
nchannels = infile.getnchannels()
sampwidth = infile.getsampwidth()
framerate = infile.getframerate()
# set position in wave to start of segment
infile.setpos(int(start * framerate))
# extract data
data = infile.readframes(int((end - start) * framerate))
# write the extracted data to a new file
with wave.open('my_out_file.wav', 'w') as outfile:
outfile.setnchannels(nchannels)
outfile.setsampwidth(sampwidth)
outfile.setframerate(framerate)
outfile.setnframes(int(len(data) / sampwidth))
outfile.writeframes(data)
我有一个几分钟长的 .wav 文件,我想将其分成不同的 10 秒 .wav 文件。
到目前为止,这是我的 python 代码:
import wave
import math
def main(filename, time):
read = wave.open(filename, 'r')
#get sample rate
frameRate = read.getframerate()
#get number of frames
numFrames = read.getnframes()
#get duration
duration = numFrames/frameRate
#get all frames as a string of bytes
frames = read.readframes(numFrames)
#get 1 frame as a string of bytes
oneFrame = read.readframes(1)
#framerate*time == numframesneeded
numFramesNeeded=frameRate*time
#numFramesNeeded*oneFrame=numBytes
numBytes = numFramesNeeded*oneFrame
#splice frames to get a list strings each representing a 'time' length
#wav file
x=0
wavList=[]
while x+time<=duration:
curFrame= frames[x:x+time]
x=x+time
wavList.append(curFrame)
打印 wavList
产量:
['\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00']
我知道这是帧列表。我如何为这个列表中的每个元素制作一个 wav 文件(第一个 .wav 文件将是 '\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00'
?Python 的 wave
模块不清楚使用帧来创建 .wav 文件.
编辑:这是 pydub
我很想看看
这是一个 python 代码片段,我根据需要使用它来拆分文件。
我使用 https://github.com/jiaaro/pydub 中的 pydub 库。
您可以修改代码段以满足您的要求。
from pydub import AudioSegment
t1 = t1 * 1000 #Works in milliseconds
t2 = t2 * 1000
newAudio = AudioSegment.from_wav("oldSong.wav")
newAudio = newAudio[t1:t2]
newAudio.export('newSong.wav', format="wav") #Exports to a wav file in the current path.
我写了一个class来简化整个过程。
尽管它用于 wav
个文件。
Here it is:
from pydub import AudioSegment
import math
class SplitWavAudioMubin():
def __init__(self, folder, filename):
self.folder = folder
self.filename = filename
self.filepath = folder + '\' + filename
self.audio = AudioSegment.from_wav(self.filepath)
def get_duration(self):
return self.audio.duration_seconds
def single_split(self, from_min, to_min, split_filename):
t1 = from_min * 60 * 1000
t2 = to_min * 60 * 1000
split_audio = self.audio[t1:t2]
split_audio.export(self.folder + '\' + split_filename, format="wav")
def multiple_split(self, min_per_split):
total_mins = math.ceil(self.get_duration() / 60)
for i in range(0, total_mins, min_per_split):
split_fn = str(i) + '_' + self.filename
self.single_split(i, i+min_per_split, split_fn)
print(str(i) + ' Done')
if i == total_mins - min_per_split:
print('All splited successfully')
Usage
folder = 'F:\My Audios\Khaled'
file = 'Khaled Speech.wav'
split_wav = SplitWavAudioMubin(folder, file)
split_wav.multiple_split(min_per_split=1)
就是这样!它会将 single wav
文件拆分为 multiple wav
个文件,每个文件的持续时间为 1 minute
。最后一个分割音频的持续时间可能少于 1 分钟 ;)
Note: If you're in Mac/Linux, then change
\
to/
对于那些严格按照 wave
模块来寻求答案的人。
import wave
# times between which to extract the wave from
start = 5.2 # seconds
end = 78.3 # seconds
# file to extract the snippet from
with wave.open('my_in_file.wav', "rb") as infile:
# get file data
nchannels = infile.getnchannels()
sampwidth = infile.getsampwidth()
framerate = infile.getframerate()
# set position in wave to start of segment
infile.setpos(int(start * framerate))
# extract data
data = infile.readframes(int((end - start) * framerate))
# write the extracted data to a new file
with wave.open('my_out_file.wav', 'w') as outfile:
outfile.setnchannels(nchannels)
outfile.setsampwidth(sampwidth)
outfile.setframerate(framerate)
outfile.setnframes(int(len(data) / sampwidth))
outfile.writeframes(data)