jiluo
jiluo

Reputation: 2356

How can I use python to split a mp3 file to several parts

Now, I have to split a mp3 file into several parts. I use python to do the work. But I cannot find a good library to do this job. I've already tried pymp3cut, echo-nest-remix, but it cannot work well.

So is there any better choice?

Upvotes: 0

Views: 2161

Answers (3)

Anna Andreeva Rogotulka
Anna Andreeva Rogotulka

Reputation: 1636

good choice might be librosa, your just split array using sample rate and desirable seconds, in example below number of seconds - 2

import librosa
import soundfile as sf


audio_file = "your_example.mp3"
y, sr = librosa.load(audio_file, sr=None) 

# chunk duration 2 seconds
chunk_duration = 2  


chunk_samples = int(chunk_duration * sr)


chunks = [y[i:i + chunk_samples] for i in range(0, len(y), chunk_samples)]

for i, chunk in enumerate(chunks):
    output_file = f"chunk_{i}.mp3"
    sf.write(output_file, chunk, sr)

Upvotes: 0

Meitham
Meitham

Reputation: 9680

I have been using VLC's python binding http://wiki.videolan.org/Python_bindings to convert videos and split into frames. VLC supports MP3 but I must admit I have not used it with audio.

Upvotes: 1

styts
styts

Reputation: 1023

You could convert your mp3s to raw .wav and use audiolab to read it in, then write out files every N frames, then convert those to mp3.

Upvotes: 1

Related Questions