media.go 481 B

12345678910111213141516171819
  1. package services
  2. import (
  3. "github.com/tosone/minimp3"
  4. )
  5. // GetMP3PlayDuration 获取MP3的时长
  6. func GetMP3PlayDuration(mp3Data []byte) (seconds int, err error) {
  7. dec, _, err := minimp3.DecodeFull(mp3Data)
  8. if err != nil {
  9. return 0, err
  10. }
  11. if dec.Kbps <= 0 {
  12. return 0, err
  13. }
  14. // 音乐时长 = (文件大小(byte) - 128(ID3信息)) * 8(to bit) / (码率(kbps b:bit) * 1000)(kilo bit to bit)
  15. seconds = (len(mp3Data) - 128) * 8 / (dec.Kbps * 1000)
  16. return seconds, nil
  17. }