pydub可以设置最大/最小音量吗?

时间:2015-11-15 13:45:50

标签: python audio limit volume pydub

作为标题,我可以设置最大/最小音量的值,也就是说,输出音频文件中不会太大或太安静? (不规范化,我只想将特定音量调整为正常,如下图所示。)

enter image description here

2 个答案:

答案 0 :(得分:1)

Loudness有点复杂 - 一个简单的解决方案是使用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)

答案 1 :(得分: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)