」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 在 Go 中計算 Ogg 音訊持續時間:逐步指南

在 Go 中計算 Ogg 音訊持續時間:逐步指南

發佈於2024-11-01
瀏覽:439

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 



我決定在這裡分享它,我發現這個實現非常酷,也許可以幫助其他人

版本聲明 本文轉載於:https://dev.to/mmvergara/calculating-ogg-audio-duration-in-go-a-step-by-step-guide-1igh?1如有侵犯,請洽[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3