库
github.com/tcolgate/mp3 库可以导入mod项目中
package info
import (
"context"
"ff/pojo"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/tcolgate/mp3"
)
func GetMP3Duration33(filePath string) (int, error) {
// 打开 MP3 文件
file, err := os.Open(filePath)
if err != nil {
return 0, err
}
defer file.Close()
// 创建解码器
d := mp3.NewDecoder(file)
// 计算时长
var totalDuration time.Duration
skipped := 0
for {
// 解码一帧
frame := mp3.Frame{}
if err := d.Decode(&frame, &skipped); err != nil {
// 到达文件结尾
break
}
// 累加帧时长
totalDuration += frame.Duration()
}
durationSec := totalDuration.Seconds()
durationInt := int(durationSec)
return durationInt, nil
}