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())
}