首页 后端开发 Golang 在 Go 中计算 Ogg 音频持续时间:分步指南

在 Go 中计算 Ogg 音频持续时间:分步指南

Sep 05, 2024 pm 09:31 PM

Calculating Ogg Audio Duration in Go: A Step-by-Step Guide

我试图克隆不和谐,我发现他们使用 ogg 格式的音频(我认为),我试图将音频持续时间存储在数据库中。

关于获取 OGG 音频持续时间的 stackoverflow 解决方案,我发现很有趣。该方法包括查找文件末尾,找到最后一个 Ogg 页头,并读取其颗粒位置。

func getOggDurationMs(reader io.Reader) (int64, error) {
    // Read the entire Ogg file into a byte slice
    data, err := io.ReadAll(reader)
    if err != nil {
        return 0, fmt.Errorf("error reading Ogg file: %w", err)
    }

    // Search for the "OggS" signature and calculate the length
    var length int64
    for i := len(data) - 14; i >= 0 && length == 0; i-- {
        if data[i] == 'O' && data[i+1] == 'g' && data[i+2] == 'g' && data[i+3] == 'S' {
            length = int64(readLittleEndianInt(data[i+6 : i+14]))
        }
    }

    // Search for the "vorbis" signature and calculate the rate
    var rate int64
    for i := 0; i < len(data)-14 && rate == 0; i++ {
        if data[i] == 'v' && data[i+1] == 'o' && data[i+2] == 'r' && data[i+3] == 'b' && data[i+4] == 'i' && data[i+5] == 's' {
            rate = int64(readLittleEndianInt(data[i+11 : i+15]))
        }
    }

    if length == 0 || rate == 0 {
        return 0, fmt.Errorf("could not find necessary information in Ogg file")
    }

    durationMs := length * 1000 / rate
    return durationMs, nil
}

func readLittleEndianInt(data []byte) int64 {
    return int64(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16 | uint32(data[3])<<24)
}

我决定在这里分享它,我发现这个实现非常酷,也许可以帮助其他人

以上是在 Go 中计算 Ogg 音频持续时间:分步指南的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

将Golang服务与现有Python基础架构集成的策略 将Golang服务与现有Python基础架构集成的策略 Jul 02, 2025 pm 04:39 PM

TOIntegrategolangServicesWithExistingPypythoninFrasture,userestapisorgrpcForinter-serviceCommunication,允许GoandGoandPyThonAppStoStoInteractSeamlessSeamLlyThroughlyThroughStandArdArdAdrotized Protoccols.1.usererestapis(ViaFrameWorkslikeSlikeSlikeGiningOandFlaskInpyThon)Orgrococo(wirs Propococo)

了解Web API的Golang和Python之间的性能差异 了解Web API的Golang和Python之间的性能差异 Jul 03, 2025 am 02:40 AM

Golangofferssuperiorperformance,nativeconcurrencyviagoroutines,andefficientresourceusage,makingitidealforhigh-traffic,low-latencyAPIs;2.Python,whileslowerduetointerpretationandtheGIL,provideseasierdevelopment,arichecosystem,andisbettersuitedforI/O-bo

是Golang前端还是后端 是Golang前端还是后端 Jul 08, 2025 am 01:44 AM

Golang主要用于后端开发,但也能在前端领域间接发挥作用。其设计目标聚焦高性能、并发处理和系统级编程,适合构建API服务器、微服务、分布式系统、数据库操作及CLI工具等后端应用。虽然Golang不是网页前端的主流语言,但可通过GopherJS编译成JavaScript、通过TinyGo运行于WebAssembly,或搭配模板引擎生成HTML页面来参与前端开发。然而,现代前端开发仍需依赖JavaScript/TypeScript及其生态。因此,Golang更适合以高性能后端为核心的技术栈选择。

如何完全,干净地从我的系统中卸载Golang? 如何完全,干净地从我的系统中卸载Golang? Jun 30, 2025 am 01:58 AM

TocompletelyuninstallGolang,firstdeterminehowitwasinstalled(packagemanager,binary,source,etc.),thenremoveGobinariesanddirectories,cleanupenvironmentvariables,anddeleterelatedtoolsandcaches.Beginbycheckinginstallationmethod:commonmethodsincludepackage

如何使用自定义字段名称将golang结构元载到JSON? 如何使用自定义字段名称将golang结构元载到JSON? Jun 30, 2025 am 01:59 AM

在Go中,若希望结构体字段在转换为JSON时使用自定义字段名,可通过结构体字段的json标签实现。1.使用json:"custom_name"标签指定字段在JSON中的键名,如Namestringjson:"username""会使Name字段输出为"username";2.添加,omitempty可控制字段为空值时省略输出,例如Emailstringjson:"email,omitempty""

如何安装去 如何安装去 Jul 09, 2025 am 02:37 AM

安装Go的关键在于选择正确版本、配置环境变量并验证安装。1.前往官网下载对应系统的安装包,Windows使用.msi文件,macOS使用.pkg文件,Linux使用.tar.gz文件并解压至/usr/local目录;2.配置环境变量,在Linux/macOS中编辑~/.bashrc或~/.zshrc添加PATH和GOPATH,Windows则在系统属性中设置PATH为Go的安装路径;3.使用goversion命令验证安装,并运行测试程序hello.go确认编译执行正常。整个流程中PATH设置和环

安装后如何修复' GO:找不到命令”? 安装后如何修复' GO:找不到命令”? Jun 30, 2025 am 01:54 AM

“Go:commandnotfound”通常因环境变量未正确配置导致;1.检查是否已正确安装Go,使用whichgo确认路径;2.手动将Go的bin目录(如/usr/local/go/bin)添加到PATH环境变量;3.修改对应shell的配置文件(如.bashrc或.zshrc),执行source使配置生效;4.可选设置GOROOT、GOPATH以避免后续模块问题。完成上述步骤后运行goversion验证是否修复。

典型Golang vs Python Web服务的资源消耗(CPU/内存)基准 典型Golang vs Python Web服务的资源消耗(CPU/内存)基准 Jul 03, 2025 am 02:38 AM

Golang在构建Web服务时CPU和内存消耗通常低于Python。1.Golang的goroutine模型调度高效,并发请求处理能力强,CPU使用率更低;2.Go编译为原生代码,运行时不依赖虚拟机,内存占用更小;3.Python因GIL和解释执行机制,在并发场景下CPU和内存开销更大;4.虽然Python开发效率高、生态丰富,但资源消耗较高,适合并发要求不高的场景。

See all articles