As title, can I set a value for maximum/minimum volume, that is, there won't be too loud or too quiet in output audio file? (Not normalize, I just want tune the specific volume to normal, as the photo below.)
问问题
4329 次
2 回答
2
响度有点复杂 - 一个简单的解决方案是使用一种更简单的方法(如 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)
于 2015-11-15T20:10:57.070 回答
0
这就是我所做的,它对我很有效。如果 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)
于 2015-11-17T06:37:53.600 回答