我在硬盘驱动器上存储了一个.mp3文件。如何使用Go语言以秒为单位找到其长度?
我发现了2个关于mp3的go库:
但是没有有效的示例,如何以秒为单位获取mp3文件的长度。
您可以计算每个帧的持续时间并将其求和以找到总持续时间。
以下是此(https://github.com/tcolgate/mp3
)库来查找持续时间的示例:
package main
import (
"fmt"
"io"
"os"
"github.com/tcolgate/mp3"
)
func main() {
t := 0.0
r, err := os.Open("<mp3 file path>")
if err != nil {
fmt.Println(err)
return
}
d := mp3.NewDecoder(r)
var f mp3.Frame
skipped := 0
for {
if err := d.Decode(&f, &skipped); err != nil {
if err == io.EOF {
break
}
fmt.Println(err)
return
}
t = t + f.Duration().Seconds()
}
fmt.Println(t)
}