如何在不改变音高的情况下改变音频?

时间:2016-12-30 13:30:21

标签: python audio moviepy

我需要在特定时间以一定的持续时间将音频应用于视频,但某些音频持续时间更大(或更小)然后需要。如何在不改变音高的情况下改变音频速度?我试图改变fps(通过乘以所需的持续时间除以音频持续时间),但它不能正常工作。

original = VideoFileClip("orig.mp4")
clips = [orig.audio.volumex(0.3)]
subs = [] #some array
i = 0
for sub in subs:
    clip = AudioFileClip("\\temp{}.mp3")
    mult = clip.duration / (sub.end - sub.start)  + 0.00001
    clip = AudioArrayClip(clip.to_soundarray(buffersize=500, fps=24000/mult), fps=24000).set_start(sub.start).set_end(sub.end)
    clips.append(clip)
    i += 1

final = CompositeAudioClip(clips)
final.write_audiofile("final.mp3")

2 个答案:

答案 0 :(得分:-1)

使用wave:更改采样率

import wave

CHANNELS = 1
swidth = 2
Change_RATE = 2

spf = wave.open('VOZ.wav', 'rb')
RATE=spf.getframerate()
signal = spf.readframes(-1)

wf = wave.open('changed.wav', 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(swidth)
wf.setframerate(RATE*Change_RATE)
wf.writeframes(signal)
wf.close()

答案 1 :(得分:-1)

你可以使用 librosa 模块:

from scipy.io import wavfile
import librosa, numpy as np

song, fs = librosa.load("song.wav")

song_2_times_faster = librosa.effects.time_stretch(song, 2)

scipy.io.wavfile.write("song_2_times_faster.wav", fs, song_2_times_faster) # save the song