1

The problem does not seem to be difficult, but I spent a lot of time, tried several libraries and did not find a solution. for example github.com/hajimehoshi/go-mp3 dosen't contain necessary func.

    file, err := os.Open("example.mp3")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    decoder, err := mp3.NewDecoder(file)
    if err != nil {
        panic(err)
    }
    defer decoder.Close()

    sampleRate := decoder.SampleRate()
    numChannels := ?
Zeke Lu
  • 6,349
  • 1
  • 17
  • 23
ragarac3
  • 51
  • 2

1 Answers1

0

The package github.com/hajimehoshi/go-mp3 knows the the number of channels. It's just not exported. See the implementation.

The bad news is that the package was archived on Apr 3, 2023. But it should be easy to fork the repository and modify the code according to your need.

I have tried to fork the repository and export (*Decoder).NumberOfChannels. See this commit. And here is the demo to use the forked version of the package (with the replace directive):

Note: I'm not sure what do you expect from the number of channels. But (*Decoder).NumberOfChannels always returns 1 (mono mode) or 2 (stereo mode). This doc mentions that MPEG-2/Multichannel has up to 5 full range audio channels and an LFE-channel. But the "MPEG Audio Frame Header" section in that doc does not provide any information about it.

go.mod

module example.com/m

go 1.20

replace github.com/hajimehoshi/go-mp3 v0.3.4 => github.com/ZekeLu/go-mp3 v0.3.5-pre

require github.com/hajimehoshi/go-mp3 v0.3.4

main.go

package main

import (
    "fmt"
    "os"

    "github.com/hajimehoshi/go-mp3"
)

func main() {
    file, err := os.Open("classic.mp3")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    decoder, err := mp3.NewDecoder(file)
    if err != nil {
        panic(err)
    }

    fmt.Println("channels:", decoder.NumberOfChannels())
}
Zeke Lu
  • 6,349
  • 1
  • 17
  • 23