简体   繁体   English

[Python][Moviepy] 如何在音频末尾添加短暂的静音?

[英][Python][Moviepy] How to add a short silence in the end of an audio?

I'd like to include a short silence duration at the end of an audio clip.我想在音频剪辑的末尾添加一段短暂的静音时间。 I haven't found any specific functions in the Moviepy documentation, so I've resorted to creating a muted audio file of 500ms and concatenating it with the original audio file.我没有在 Moviepy 文档中找到任何特定功能,所以我求助于创建一个 500 毫秒的静音音频文件并将其与原始音频文件连接起来。

In some cases, this concatenation will introduce a noticeable glitch at the intersection, and I haven't figured out why.在某些情况下,这种串联会在交叉点引入明显的故障,我还没有弄清楚原因。 I also realized by importing the concatenated audiofile to Audacity that Moviepy actually creates two audio tracks when concatenating.通过将连接的音频文件导入 Audacity,我还意识到 Moviepy 在连接时实际上创建了两个音轨。

Do you know a better way to add silence to the end of the clip, or maybe the reason why this glitch appears sometimes (in my experience about 1 every 4 instances)?您是否知道在剪辑末尾添加静音的更好方法,或者有时会出现此故障的原因(根据我的经验,大约每 4 次出现 1 次)?

Here's my code:这是我的代码:

from moviepy.editor import *

temp_audio = "original audio dir"
silence = "silence audio dir"

audio1 = AudioFileClip(temp_audio)                      #original audio file
audio2 = AudioFileClip(silence)                         #silence audio file
final_audio = concatenate_audioclips([audio1,audio2])
final_audio.write_audiofile(output)

I am currently using Python 3.9.5 and Moviepy 1.0.3我目前正在使用 Python 3.9.5 和 Moviepy 1.0.3

may be fps=44100 will work.可能 fps=44100 会起作用。 mp3 file's frequence mp3文件的频率

You can use the below solution for adding the silence at the end or start of audio:您可以使用以下解决方案在音频的结尾或开头添加静音:

from pydub import AudioSegment
orig_seg = AudioSegment.from_file('audio.wav')
silence_seg = AudioSegment.silent(duration=1000) # 1000 for 1 sec, 2000 for 2 secs

# for adding silence at the end of audio
combined_audio = orig_seg + silence_seg

# for adding silence at the start of audio
#combined_audio = silence_seg + orig_seg

combined_audio.export('new_audio.wav', format='wav')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM