Pydub - How to change frame rate without changing playback speed
Asked Answered
B

2

15

I have a couple audio files that I open in Pydub with AudioSegment.

I want to decrease the audio quality from frame rate 22050 to 16000 Hz. (One channel files)

If I simply change the frame rate of AudioSegment, what I get is the exact same wave played in slower speed. Well, fair enough.

But how do I actually change the waves to fit a lower quality, same speed playback?

(Manual interpolation is the only thing I can think of, but I don't want to get into that trouble)

Beekman answered 17/5, 2017 at 21:42 Comment(2)
Have you tried AudioSegment().set_frame_rate() ?Drinker
Would you post that as an answer?Nib
D
38

You can use:

sound = AudioSegment.from_file(…)
sound = sound.set_frame_rate(16000)
Drinker answered 19/5, 2017 at 3:38 Comment(2)
That's much easier than what I was doing: subprocess.check_output(['sox',filename,'-r','16000',new_filename])Showing
I made a mistake in trying to implement this, maybe it can save some time for others reading the answer: I tried sound.set_frame_rate(16000) thinking it would apply the operation on the sound object which could afterwards be exported. However, as correctly stated in this answer, you need to assign the result to a (new or existing) variable, so sound = sound.set_frame_rate(16000).Mercurochrome
W
2

Or you can do this instead also:

import librosa

y, sr = librosa.load(path, sr=16000)

and use it further as per your requirement.

Wheelchair answered 8/2, 2022 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.