如何将.wav文件拆分为多个.wav文件? [英] How to split a .wav file into multiple .wav files?

查看:757
本文介绍了如何将.wav文件拆分为多个.wav文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个几分钟的.wav文件,我想分割成10秒的另一个.wav文件.

I have a .wav file several minutes long that I would like to split into different 10 second .wav files.

到目前为止,这是我的python代码:

This is my python code so far:

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文件.

I know that this is a list of frames. How do I make one wav file for each element in this list (the first .wav file would be '\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00'? Python's wave module is unclear about using frames to create .wav files.

这是

This is a duplicate question of How to splice an audio file (wav format) into 1 sec splices in python? However, if someone has an answer that does not require pydub I would very much like to see it.

推荐答案

这是我根据需要用于拆分文件的python代码段.
我使用来自

This is a python code snippet that I use for splitting files as per necessity.
I use the pydub library from https://github.com/jiaaro/pydub. You can modify the snippet to suit your requirement.

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.

这篇关于如何将.wav文件拆分为多个.wav文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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