'I'm finding a solution to change the pitch of a video at a time I want

I am new at python. I wonder how I can change pitch of sound at a time we choose. What I'm doing is seperate video and sound and change pitch of sound. But I think, it's not good for performance. This code below is for separate video and sound:

from hashlib import new  
import moviepy.editor as mp  
import pyaudio  
import numpy as np  
from time import time  
my_clip = mp.VideoFileClip(r"example.mp4")  
my_clip.audio.write_audiofile(r"my_result.wav")  
new_clip = my_clip.without_audio()  
new_clip.write_videofile("final.mp4")  

Then I will change pitch of sound, after that I will remove sound of video and combine video with sound with this code:

import librosa
import soundfile as sf
import moviepy.editor as mp
signal, sr  = librosa.load("my_result.wav")
argument_signal = librosa.effects.pitch_shift(signal,sr, 2)
sf.write("final_file.wav",argument_signal,sr)
audio = mp.AudioFileClip("final_file.wav")
video = mp.VideoFileClip("final.mp4")
final = video.set_audio(audio)
final.write_videofile("final_video.mp4")

It's work but this performance not good.

So I'm finding a solution can change pitch of sound in a video at the time we choose. Thanks for any help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source