Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a .wav file into multiple .wav files?

Tags:

python

audio

wave

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

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) 

Printing wavList yields:

['\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']

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.

EDIT: 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.

like image 928
jdsto Avatar asked Jun 23 '16 18:06

jdsto


People also ask

Can you split a WAV file?

Just drag-and-drop the file to the Video Splitter main window or use File menu -> Open Media File... and select the . WAV file in the opened dialog box. 3) Now select fragments for editing. Use slider and "Add marker" button (or "M" hot key) to mark one or several beginning and end positions of fragments for saving.

Can a WAV file have multiple tracks?

WAV Poly files are multi-channel BWF files that contain extra metadata identifying the channels. With WAV mono (monophonic) as the file type, the audio recorder will generate separate data files for each individual track recorded.

How do I split an audio file into multiple files in 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.

How to split audio files in different sizes?

Designed to be very easy-to-use, WavePad will have you splitting wav files, mp3 and other audio files within minutes and creating audio file sizes you want. Slice into x many files (slice one 10MB mp3 into two 5MB files).

What are the features of split MP3?

Features Split MP3, WAV, OGG and FLAC audio files. Several ways to split mp3 files (by number of files, duration or silence detection). Split multiple files at the same time. Split music files with silence detection to determine the end of a song.

How do I split a 10 minute track into two tracks?

Slice into files of x duration where duration equals HH:MM:SS (split one 10 minute track into two 5 minute tracks). Slice into individual tracks using silence detection. Get it Free. A free version of WavePad audio editing software is available for non-commercial use only.

How do I slice an MP3 file into individual tracks?

Slice into x many files (slice one 10MB mp3 into two 5MB files). Slice into files of x duration where duration equals HH:MM:SS (split one 10 minute track into two 5 minute tracks). Slice into individual tracks using silence detection.


Video Answer


1 Answers

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. 
like image 52
siddhantsomani Avatar answered Oct 15 '22 06:10

siddhantsomani