微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Moviepy 编写视频文件

如何解决如何使用 Moviepy 编写视频文件

我正在尝试在 .mp4 文件中实现惊人的爆发。我已经能够成功地做到这一点,但是我如何改变原始音频结果在一个数组中。我遇到的问题是“write_videofile”函数似乎只采用特定类型的结构来写入音频文件。到目前为止,这就是我所拥有的任何帮助都会很棒。

#Necessary imports 
import os as os 
import moviepy.editor as mp
from playsound import playsound
import librosa as librosa
import librosa.display
import sounddevice as sd
import numpy as np
from moviepy.audio.AudioClip import AudioArrayClip

#Set working directory 
os.chdir('/Users/matthew/Desktop/Python_startle')

#Upload the video and indicate where in the clip the burst should be inserted 
vid = mp.VideoFileClip("37vids_balanced/Baby_Babble.mp4") 
audio,sr = librosa.load('37vids_balanced/Baby_Babble.mp4') 
librosa.display.waveplot(audio,sr = sr) 
sd.play(audio,sr) 

audio_duration = librosa.get_duration(filename = '37vids_balanced/Baby_Babble.mp4') 
start_point_sec = 6 
start_point_samp = start_point_sec * sr 
burst_samp = int(sr * .05) #how many samples are in the noise burst
burst_window = audio[start_point_samp : start_point_samp + burst_samp].astype(np.int32) #Isolating part of the audio clip to add burst to 

#Creating startle burst 
noise = np.random.normal(0,1,len(burst_window))

#Inserting the noise created into the portion of the audio that is wanted 
audio[start_point_samp : start_point_samp + burst_samp] = audio[start_point_samp : start_point_samp + burst_samp].astype(np.int64) + noise #Puts the modified segment into the original audio 
librosa.display.waveplot(audio,sr) 


new_vid = vid.set_audio(audio)
sd.play(new_vid.audio,sr) 

#Trying to get the  audio into the proper form to convert into a mp4 

new_vid.audio = [[i] for i in new_vid.audio] #This makes the data into a list because the 'AudioArrayClip' Could not take in an array because an array is not 'iterable' this might not be the best solution but it has worked so far
new_vid.audio = AudioArrayClip(new_vid.audio,fps = new_vid.fps)

new_vid.write_videofile('37vids_balanced_noise/Baby_Babble.mp4')

也许有一种更简单的方法可以将数组转化为可行的可迭代形式。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。