Python module to change MP3 files to play back faster?

Question:

I have a number of MP3 files containing lectures where the speaker talks very slowly and I would like to alter the MP3 file so that the playback rate is about 1.5 times as fast as normal.

Can someone suggest a good Python library for this? By the way, I’m running Python 2.6 on Windows.

Thanks in advance.

Asked By: reckoner

||

Answers:

pymedia includes a recode_audio.py example that allows arbitrary input and output formats available here. This of course requires the installation of pymedia as well.

Note that as Nick T notes, if you just change the sample-rate without resampling you’ll get high-pitched ‘fast’ audio, so you’ll want to employ time-stretching in combination with changing the bit-rate.

Answered By: synthesizerpatel

I wrote a library, pydub which is mainly designed for manipulating audio.

I’ve created an experimental time-stretching algorithm if you’re interested in seeing how these sorts of things work.

Essentially you want to throw away a portion of your data, but you can’t just play back the waveform faster because then it’ll all get high pitched (as synthesizerpatel mentioned). Instead you want to throw away chunks (20 Hz is the lowest a human can hear so 50ms chunks do not cause audible frequency changes, though there are other artifacts).

PS – I get 50ms like so:

20 Hz == 1 second per 20 cycles

or

1000 ms per 20 cycles

or

1000ms / 20Hz == 50ms per cycle
Answered By: Jiaaro

You can have a try on _spawn module in audio_segment.py of Pydub. Here is an example code:

from pydub import AudioSegment
import os

def speed_swifter(sound, speed=1.0):
    return sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={"frame_rate": int(sound.frame_rate * speed)})

in_path = 'your/path/of/input_file/hello.mp3'
ex_path = 'your/path/of/output_file/hello.mp3'
sound = AudioSegment.from_file(in_path)    

# generate a slower audio for example
slower_sound = speed_change(sound, 0.5)

slower_sound.export(os.path.join(ex_path, 'slower.mp3'), format="mp3")
Answered By: itsMe

I was trying to achieve the same, and I’ve tried all suggested answers. Unfortunately, the answer given by pydub creator wasn’t too descriptive and I struggled to find an example, and the other is eventually outputting a file with a high pitch.

Eventually, the easiest thing that worked for me was indeed to use pydub as suggested, but using an apparently undocumented (or, hard to find) feature, speedup.

It is extremely simple:

from pydub import AudioSegment
import os

# A couple of var for readability
slow_mp3_path = "/path/to/my/slow.mp3"
fast_mp3_path = "/path/to/my/fast.mp3"

# Get the audiosegment from the file
slow_mp3_obj = AudioSegment.from_file(tmp_path)
# File's in memory, you can safely delete the original file if you want to save disk space now
os.remove(slow_mp3_path)
# Speed it up
speed_update = slow_mp3.speedup(1.5)
# Save the updated mp3
speed_update.export(fast_mp3_path, format="mp3")

That’s it, the new file in fast_mp3_path will be 1.5 faster without changing the pitch.

Answered By: nnsense
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.