我设置了一个文本到语音转换的请求,发送给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)
}

Go相关问答推荐

Go -SDP服务器读缓冲区不会更改任何内容

GORM没有从 struct 创建完整的表,如何修复?

获取作为类型参数传递给方法接收方中的类型参数的切片的基础类型

在不耗尽资源的情况下处理S3文件下载

Golang XML密钥名称冲突

使用Go使用Gorm使用外键对数据进行排序

go中跨域自定义验证的问题

如何找到一个空闲的TPM句柄来保存新的密钥对对象?

Docker 执行失败并显示cmd/ENTRYPOINT 中的命令未找到

从 eBPF LRU 哈希映射中错误驱逐的元素

使用模拟在 golang 中测试我的界面.专门测试调用同级函数的 1 个函数

泛型:实现嵌套接口

如何匹配两次出现的相同但随机字符串之间的字符

Golang Oauth2 服务帐户返回空刷新令牌字符串

SSH 代理,数据包长度错误

Golang 数据库/sql 与 SetMaxOpenConns 挂起

关系不存在 GORM

无限期运行 Go routine (完成后重新启动)

关于GO的几个问题

并发 map map导致的 Golang 数据竞争