我设置了一个文本到语音转换的请求,发送给OpenAI API,然后它生成一个音频.现在我想知道它在[MM:SS]中的持续时间,有什么方法或库可以获得它吗?
我设置了一个文本到语音转换的请求,发送给OpenAI API,然后它生成一个音频.现在我想知道它在[MM:SS]中的持续时间,有什么方法或库可以获得它吗?
这个问题的答案如下:
How to find the length of mp3 file in golang?个
此外,您可能想要将Float64转换为MM:SS格式.在本例中,您可以使用类似以下内容:
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)
}