计算mp3平均音量

时间:2009-06-16 19:10:14

标签: mp3 normalization scaling scale volume

我需要知道mp3文件的平均音量,以便当我将其转换为mp3(以不同的比特率)时,我也可以缩放音量,以便将其标准化...

因此我需要一个命令行工具/ ruby​​库,它以dB为单位给出平均音量。

4 个答案:

答案 0 :(得分:3)

您可以使用sox(开源命令行音频工具http://sox.sourceforge.net/sox.html)同时对文件进行规范化和转码。

修改

看起来它没有比特率选项。无论如何,如果LAME正常化,sox可能有点过分。

答案 1 :(得分:1)

您可以使用LAME对mp3进行编码。它具有标准化,缩放和比特率的选项。 LAME几乎可以编译任何平台。

答案 2 :(得分:1)

http://mp3gain.sourceforge.net/是一个经过深思熟虑的解决方案。

答案 3 :(得分:1)

我根据上面的输入写了一个小包装脚本:

#!/bin/sh

# Get the current volume (will reset to this later).
current=`amixer -c 0 get Master 2>&1 |\
    awk '/%/ {
              p=substr($4,2,length($4)-2);
              if( substr(p,length(p)) == "%" )
                 {
                 p = substr(p,1,length(p)-1)
                 }
             print p
             }'`

# Figure out how loud the track is.  The normal amplitude for a track is 0.1. 
#   Ludicrously low values are 0.05, high is 0.37 (!!?)
rm -f /tmp/$$.out
/usr/bin/mplayer -vo null -ao pcm:file=/tmp/$$.out $1 >/dev/null 2>&1
if [ $? = 0 ] ; then
    amplitude=`/usr/bin/sox /tmp/$$.out -n stat 2>&1 | awk '/RMS.+amplitude/ {print $NF}'`
fi
rm -f /tmp/$$.out

# Set an appropriate volume for the track.
to=`echo $current $amplitude | awk '{printf( "%.0f%%", $1 * 0.1/$2 );}'`
echo $current $amplitude | awk '{print "Amplitude:", $2, "  Setting volume to:", 10/$2 "%,  mixer volume:", $1 * 0.1/$2}'
amixer -c 0 set Master $to  >/dev/null 2>&1

mplayer -quiet -cache 2500 $1

# Reset the volume for next time.
amixer -c 0 set Master "$current%"  >/dev/null 2>&1

启动播放文件需要额外一秒钟,并依赖于alsamixer来调整音量,但它确实能让你不必经常调整主音量。它并不关心输入格式是什么,因为如果mplayer可以播放它,它可以提取音频,所以它应该可以正常使用MP3,Ogg,AVI,等等。