package main
import (
"fmt"
"io"
"os"
"time"
"github.com/tcolgate/mp3"
)
const mp3File = <path-to-mp3-file>
func main() {
var t float64
fd, err := os.Open(mp3File)
if err != nil {
panic(err)
}
defer fd.Close()
d := mp3.NewDecoder(fd)
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)
// Convert the value to a time.Duration object
duration := time.Duration(t * float64(time.Second))
// Get the duration in minutes and seconds
minutes := int(duration.Minutes())
seconds := int(duration.Seconds()) - (minutes * 60)
// Format the duration in the MM:SS format
formatted_duration := fmt.Sprintf("%02d:%02d", minutes, seconds)
fmt.Printf("The value %v expressed in MM:SS format is %v.\n", t, formatted_duration)
}
3条答案
按热度按时间h6my8fg21#
您可以查看go-audio library
从那里,您可以从文件中查找最后一帧
字符串
h5qlskok2#
这个问题在这里得到了回答:
How to find the length of mp3 file in golang?
另外,你可能想将float64转换为MM:SS格式。在这种情况下,你可以使用这样的方法:
字符串
6ss1mwsb3#
字符串
的数据
的