开发者

Length of an audio file (e.g .wav) using RubyAudio

How can I determine the length (in ms) o开发者_开发知识库f an audio file (e.g .wav) using RubyAudio

s = RubyAudio::Sound.open("1.wav")


You can get the SongInfo by:

songInfo = s.info

And then the song info contains the sample rate and the number of frames which you can use to calculate the duration of the sound file:

duration = songInfo.frames / songInfo.samplerate


From a cursory look at the docs, it looks like you can't do that with RubyAudio.

Have you tried looking at ruby-mp3info? I don't know if it's still actively developed, nor if it works for multiple audio formats, but it claims to be able to give you the duration of an mp3.

An alternate way would be to do an estimate based on the bitrate and the file length.


RubyAudio doesn't appear to have been updated in six years and its documentation is sparse. If you're able I'd recommend using rtaglib instead.

However, if you're married to RubyAudio it looks like you can get both a frame count (Audio::Soundfile#frames) and a sample (frame) rate (Audio::Soundfile#samplerate). Knowing this you should be able to divide the number of frames by sample rate to get the length of the file in seconds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜