如何使用Pydub更改音频播放速度?

时间:2018-07-20 04:04:03

标签: python audio mp3 wav pydub

我是音频编辑库的新学习者-Pydub。我想使用Pydub(例如.wav / mp3格式的文件)更改某些音频文件的播放速度,但是我不知道如何制作。我看到的唯一可以解决此问题的模块是speedup module in effect.py。但是,没有关于我应该如何称呼它的解释。

有人可以解释一下如何在Pydub中执行此任务吗?非常感谢!

(一个相关的问题:Pydub - How to change frame rate without changing playback speed,但我想做的是在不改变音频质量的情况下更改播放速度。)

4 个答案:

答案 0 :(得分:1)

from pydub import AudioSegment
from pydub import effects

root = r'audio.wav'
velocidad_X = 1.5 # No puede estar por debajo de 1.0

sound = AudioSegment.from_file(root)
so = sound.speedup(velocidad_X, 150, 25)
so.export(root[:-4] + '_Out.mp3', format = 'mp3')

答案 1 :(得分:0)

sound.set_frame_rate()进行转换,它不会引起任何“花栗鼠效果”,但是您可以做的是更改帧速率(不进行转换),然后从那里将音频转换回正常的帧速率(例如44.1 kHz,“ CD质量”)

from pydub import AudioSegment
sound = AudioSegment.from_file(…)

def speed_change(sound, speed=1.0):
    # Manually override the frame_rate. This tells the computer how many
    # samples to play per second
    sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={
         "frame_rate": int(sound.frame_rate * speed)
      })
     # convert the sound with altered frame rate to a standard frame rate
     # so that regular playback programs will work right. They often only
     # know how to play audio at standard frame rate (like 44.1k)
    return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)


slow_sound = speed_change(sound, 0.75)
fast_sound = speed_change(sound, 2.0)

答案 2 :(得分:0)

这可以使用pyrubberband封装来完成,该封装需要橡皮筋库,该橡皮筋库可以在保持音调和高质量的同时拉伸音频。我能够使用brew在MacOS上安装该库,在Ubuntu上通过apt install进行安装。对于极端拉伸,请查看PaulStretch

brew install rubberband

这仅适用于librosa软件包

import librosa
import pyrubberband
import soundfile as sf

y, sr = librosa.load(filepath, sr=None)
y_stretched = pyrubberband.time_stretch(y, sr, 1.5)
sf.write(analyzed_filepath, y_stretched, sr, format='wav')

要使pyrubberband直接与没有librosa的pydub中的AudioSegment一起使用,我弄弄了这个功能:

def change_audioseg_tempo(audiosegment, tempo, new_tempo):
    y = np.array(audiosegment.get_array_of_samples())
    if audiosegment.channels == 2:
        y = y.reshape((-1, 2))

    sample_rate = audiosegment.frame_rate

    tempo_ratio = new_tempo / tempo
    print(tempo_ratio)
    y_fast = pyrb.time_stretch(y, sample_rate, tempo_ratio)

    channels = 2 if (y_fast.ndim == 2 and y_fast.shape[1] == 2) else 1
    y = np.int16(y_fast * 2 ** 15)

    new_seg = pydub.AudioSegment(y.tobytes(), frame_rate=sample_rate, sample_width=2, channels=channels)

    return new_seg

答案 3 :(得分:0)

我知道已经晚了,但是我写了一个程序将mp3转换为不同的播放速度。

首先,转换.MP3-> .Wav,因为PYRubberBand仅支持.wav格式。然后同时调整时间和音高以避免花栗鼠效应。

import wave
import sys
from pydub import AudioSegment
#sound = AudioSegment.from_file("deviprasadgharpehai.mp3")
sound = AudioSegment.from_mp3(sys.argv[1])
sound.export("file.wav", format="wav")

print(sys.argv[1])

import soundfile as sf
import pyrubberband as pyrb
y, sr = sf.read("file.wav")
# Play back at extra low speed
y_stretch = pyrb.time_stretch(y, sr, 0.5)
# Play back extra low tones
y_shift = pyrb.pitch_shift(y, sr, 0.5)
sf.write("analyzed_filepathX5.wav", y_stretch, sr, format='wav')

sound = AudioSegment.from_wav("analyzed_filepathX5.wav")
sound.export("analyzed_filepathX5.mp3", format="mp3")

# Play back at low speed
y_stretch = pyrb.time_stretch(y, sr, 0.75)
# Play back at low tones
y_shift = pyrb.pitch_shift(y, sr, 0.75)
sf.write("analyzed_filepathX75.wav", y_stretch, sr, format='wav')

sound = AudioSegment.from_wav("analyzed_filepathX75.wav")
sound.export("analyzed_filepathX75.mp3", format="mp3")

# Play back at 1.5X speed
y_stretch = pyrb.time_stretch(y, sr, 1.5)
# Play back two 1.5x tones
y_shift = pyrb.pitch_shift(y, sr, 1.5)
sf.write("analyzed_filepathX105.wav", y_stretch, sr, format='wav')

sound = AudioSegment.from_wav("analyzed_filepathX105.wav")
sound.export("analyzed_filepathX105.mp3", format="mp3")

# Play back at same speed
y_stretch = pyrb.time_stretch(y, sr, 1)
# Play back two smae-tones
y_shift = pyrb.pitch_shift(y, sr, 1)
sf.write("analyzed_filepathXnormal.wav", y_stretch, sr, format='wav')

sound = AudioSegment.from_wav("analyzed_filepathXnormal.wav")
sound.export("analyzed_filepathXnormal.mp3", format="mp3")

**确保安装**

波形,音频分段,FFmpeg,PYRubberBand,声音文件

要使用此运行,

python3 filename.py mp3filename.mp3