用python写一个wav切割代码
时间: 2023-05-14 12:07:21 浏览: 83
wav切割程序
3星 · 编辑精心推荐
当然可以,以下是一个简单的Python代码示例,可以用来切割WAV文件:
```python
import wave
def split_wav(input_file, output_file, start_time, end_time):
with wave.open(input_file, 'rb') as wav_file:
framerate = wav_file.getframerate()
nchannels = wav_file.getnchannels()
sampwidth = wav_file.getsampwidth()
frames = wav_file.getnframes()
start_frame = int(start_time * framerate)
end_frame = int(end_time * framerate)
if end_frame > frames:
end_frame = frames
with wave.open(output_file, 'wb') as out_file:
out_file.setframerate(framerate)
out_file.setnchannels(nchannels)
out_file.setsampwidth(sampwidth)
wav_file.setpos(start_frame)
out_file.writeframes(wav_file.readframes(end_frame - start_frame))
input_file = 'input.wav'
output_file = 'output.wav'
start_time = 2.0
end_time = 4.0
split_wav(input_file, output_file, start_time, end_time)
```
这个代码使用了Python的wave模块来读取和写入WAV文件,可以指定起始时间和结束时间来切割WAV文件。
阅读全文