package services import ( "github.com/tosone/minimp3" ) // GetMP3PlayDuration 获取MP3的时长 func GetMP3PlayDuration(mp3Data []byte) (seconds int, err error) { dec, _, err := minimp3.DecodeFull(mp3Data) if err != nil { return 0, err } if dec.Kbps <= 0 { return 0, err } // 音乐时长 = (文件大小(byte) - 128(ID3信息)) * 8(to bit) / (码率(kbps b:bit) * 1000)(kilo bit to bit) seconds = (len(mp3Data) - 128) * 8 / (dec.Kbps * 1000) return seconds, nil }