[英]Can pydub set the maximum/minimum volume?
響度有點復雜-一種簡單的解決方案是使用一種較簡單的方法(例如dBFS)進行測量,並設置所有音頻的增益以匹配。
sounds = [audio_segment1, audio_segment2, audio_segment3, audio_segment4]
def set_loudness(sound, target_dBFS):
loudness_difference = target_dBFS - sound.dBFS
return sound.apply_gain(loudness_difference)
# -20dBFS is relatively quiet, but very likely to be enough headroom
same_loudness_sounds = [
set_loudness(sound, target_dBFS=-20)
for sound in sounds
]
一個復雜的因素是,您的某些聲音可能具有較長的靜音部分,甚至只是非常安靜的部分。 這將降低平均值,並且您可能必須編寫更復雜的響度測量值。 再次,這是一種簡單的方法,您可以將聲音切成更短的片段,並假設您的整個聲音長度為15分鍾,而使用最大聲的片段,我們可以將片段切成1分鍾:
from pydub.utils import make_chunks
def get_loudness(sound, slice_size=60*1000):
return max(chunk.dBFS for chunk in make_chunks(sound, slice_size))
# ...and replace set_loudness() in above example with…
def set_loudness(sound, target_dBFS):
loudness_difference = target_dBFS - get_loudness(sound)
return sound.apply_gain(loudness_difference)
這就是我所做的,對我來說效果很好。 如果sample_rate太小,缺點是性能不好。
from pydub import AudioSegment
from pydub.utils import make_chunks
def match_target_amplitude(sound, target_dBFS):
change_in_dBFS = target_dBFS - sound.dBFS
return sound.apply_gain(change_in_dBFS)
def sound_slice_normalize(sound, sample_rate, target_dBFS):
def max_min_volume(min, max):
for chunk in make_chunks(sound, sample_rate):
if chunk.dBFS < min:
yield match_target_amplitude(chunk, min)
elif chunk.dBFS > max:
yield match_target_amplitude(chunk, max)
else:
yield chunk
return reduce(lambda x, y: x + y, max_min_volume(target_dBFS[0], target_dBFS[1]))
sound = AudioSegment.from_mp3("vanilla_sky.mp3")
normalized_db = min_normalized_db, max_normalized_db = [-32.0, -18.0]
sample_rate = 1000
normalized_sound = sound_slice_normalize(sound, sample_rate, normalized_db)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.