Support FLAC Ogg encoding
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2022-04-17 20:27:29 +02:00
parent 28d9a9f508
commit 4623419be8
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
5 changed files with 65 additions and 18 deletions

View file

@ -444,6 +444,42 @@ func TestEncodeFLAC(t *testing.T) {
}
}
func TestEncodeFLACOgg(t *testing.T) {
t.Parallel()
fp, err := os.Open(TestSingleSample24)
if err != nil {
t.Error(err)
return
}
defer fp.Close()
source, err := flac.NewFormat().Open(fp)
if err != nil {
t.Error(err)
return
}
target, err := os.CreateTemp("/tmp", "encode_test_*.ogg")
if err != nil {
t.Error(err)
return
}
defer func() {
name := target.Name()
target.Close()
os.Remove(name)
}()
options := make(map[string]interface{})
options["ogg"] = true
err = flac.NewFormat().Encode(source, target, options)
if err != nil {
t.Error(err)
return
}
}
func TestPacketizeFLAC(t *testing.T) {
t.Parallel()
fp, err := os.Open(TestSingleSample24)

View file

@ -13,18 +13,17 @@ Collection of audio utilities for decoding/encoding files and streams.
## Codecs supported
| Codec | Containers | Decoder | Analyzer | Encoder | Notes |
|:----------:|:----------:|:-------:|:--------:|:-------:|:------------------------------------------------------------------------------------------------------------------------------------------------------|
| **FLAC** | FLAC, Ogg | ✅ | ✅ | ✅* | Adjustable encoding compression level and block size. |
| **TTA** | TTA | ✅ | ✅ | ✅ | Only 16bit encoding |
| **MP3** | MP3 | ✅ | - | ✅ | Adjustable encoding bitrate and mode. Decoding via [minimp3](https://github.com/kvark128/minimp3), encoding via [LAME](https://lame.sourceforge.io/). |
| **Opus** | Ogg | ✅ | - | ✅ | Adjustable encoding bitrate. |
| **Vorbis** | Ogg | ✅ | - | ❌ | |
| **AAC** | ADTS | ✅ | - | ✅ | Adjustable encoding bitrate and mode (LC, HEv2). |
* *Only FLAC container output implemented for encoder.
| Codec | Containers | Decoder | Analyzer | Encoder | Notes |
|:----------:|:----------------------------------------------------------------------------------------:|:-------:|:--------:|:-------:|:------------------------------------------------------------------------------------------------------------------------------------------------------|
| **FLAC** | [FLAC](https://xiph.org/flac/format.html), [Ogg](https://xiph.org/flac/ogg_mapping.html) | ✅ | ✅ | ✅ | Adjustable encoding compression level and block size. |
| **TTA** | [TTA](https://www.tausoft.org/en/true_audio_codec_format/) | ✅ | ✅ | ✅ | Only 16bit encoding |
| **MP3** | [MP3](http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html) | ✅ | - | ✅ | Adjustable encoding bitrate and mode. Decoding via [minimp3](https://github.com/kvark128/minimp3), encoding via [LAME](https://lame.sourceforge.io/). |
| **Opus** | [Ogg](https://www.xiph.org/ogg/doc/framing.html) | ✅ | - | ✅ | Adjustable encoding bitrate. |
| **Vorbis** | [Ogg](https://www.xiph.org/ogg/doc/framing.html) | ✅ | - | ❌ | |
| **AAC** | [ADTS](https://wiki.multimedia.cx/index.php/ADTS) | ✅ | - | ✅ | Adjustable encoding bitrate and mode (LC, HEv2). |
## Container packetizers supported
| Container | Packetizer | Keep Mode | Sample Number |
|:---------:|:----------:|:---------:|:-------------:|
| **FLAC** | ✅ | ✅ | ✅ |
@ -33,7 +32,7 @@ Collection of audio utilities for decoding/encoding files and streams.
| **Ogg** | ✅ | ✅ | ✅* |
| **ADTS** | ✅ | ✅ | ✅ |
* *Sample number (absolute granule position) of Ogg depends on underlying codec implementing it. Currently implemented for Opus
* *Sample number (absolute granule position) of Ogg depends on underlying codec implementing it. Has been tested as working for Opus
## Dependencies
### Go >= 1.18

View file

@ -124,6 +124,7 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
var compressionLevel = 8
var blockSize = 0
var streamable = true
var ogg = false
if options != nil {
var val interface{}
@ -157,15 +158,28 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
streamable = boolVal
}
}
if val, ok = options["ogg"]; ok {
if boolVal, ok = val.(bool); ok {
ogg = boolVal
}
}
}
var encoder *libflac.Encoder
var err error
if writeSeeker, ok := writer.(libflac.FlacWriter); ok {
encoder, err = libflac.NewEncoderWriteSeeker(writeSeeker, source.Channels, bitsPerSample, source.SampleRate, compressionLevel, streamable, blockSize)
if ogg {
encoder, err = libflac.NewEncoderWriteSeekerOgg(writeSeeker, source.Channels, bitsPerSample, source.SampleRate, compressionLevel, streamable, blockSize)
} else {
encoder, err = libflac.NewEncoderWriteSeeker(writeSeeker, source.Channels, bitsPerSample, source.SampleRate, compressionLevel, streamable, blockSize)
}
} else {
encoder, err = libflac.NewEncoderWriter(writer, source.Channels, bitsPerSample, source.SampleRate, compressionLevel, streamable, blockSize)
if ogg {
encoder, err = libflac.NewEncoderWriterOgg(writer, source.Channels, bitsPerSample, source.SampleRate, compressionLevel, streamable, blockSize)
} else {
encoder, err = libflac.NewEncoderWriter(writer, source.Channels, bitsPerSample, source.SampleRate, compressionLevel, streamable, blockSize)
}
}
if err != nil {

2
go.mod
View file

@ -7,7 +7,7 @@ require (
git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220417020459-7018d91e3eed
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220227175608-6cc027f24dba
git.gammaspectra.live/S.O.N.G/go-tta v0.2.1-0.20220226150007-096de1072bd6
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220305093419-2fd5e3285566
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220417181802-3057bde44c07
github.com/dh1tw/gosamplerate v0.1.2
github.com/edgeware/mp4ff v0.27.0
github.com/jfreymuth/oggvorbis v1.0.3

6
go.sum
View file

@ -1,15 +1,13 @@
git.gammaspectra.live/S.O.N.G/go-ebur128 v0.0.0-20220308113719-afad5c6e5c28 h1:7YLU2eyGBX8juV445KlBxW71NjFAzbRvfotZBUP16Bs=
git.gammaspectra.live/S.O.N.G/go-ebur128 v0.0.0-20220308113719-afad5c6e5c28/go.mod h1:5H4eVW9uknpn8REFr+C3ejhvXdncgm/pbGqKGC43gFY=
git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220228131722-e9cb84c52f48 h1:MaKiBfXQl0keyfdCi1PxGOKRTiWhIs8PqCal5GhKDi0=
git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220228131722-e9cb84c52f48/go.mod h1:pkWt//S9hLVEQaJDPu/cHHPk8vPpo/0+zHy0me4LIP4=
git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220417020459-7018d91e3eed h1:aJPCb3LS4Wai34S5bKUGgAm+/hPt0J2tvWPOl6RnbbE=
git.gammaspectra.live/S.O.N.G/go-fdkaac v0.0.0-20220417020459-7018d91e3eed/go.mod h1:pkWt//S9hLVEQaJDPu/cHHPk8vPpo/0+zHy0me4LIP4=
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220227175608-6cc027f24dba h1:JEaxCVgdr3XXAuDCPAx7ttLFZaaHzTEzG+oRnVUtUKU=
git.gammaspectra.live/S.O.N.G/go-pus v0.0.0-20220227175608-6cc027f24dba/go.mod h1:vkoHSHVM9p6vAUmXAik0gvaLcIfiQYrD6bQqVpOulUk=
git.gammaspectra.live/S.O.N.G/go-tta v0.2.1-0.20220226150007-096de1072bd6 h1:ITVVisbHPnUclp3PBkCbXFeBhOCBcOjPdgjJ9wRH3TI=
git.gammaspectra.live/S.O.N.G/go-tta v0.2.1-0.20220226150007-096de1072bd6/go.mod h1:cobkT8u8vq/+ngLy+feKS2M2ZT2HoCec5riA/0Cex3Q=
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220305093419-2fd5e3285566 h1:nhnwjyaAydpSU3UADA9BRJmwpmJ8UlffxvBDuHC1T+8=
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220305093419-2fd5e3285566/go.mod h1:/po1QgOh3xynbvi4sxdY6Iw8m5WPJfGGmry2boZD8fs=
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220417181802-3057bde44c07 h1:7YxnU4AY/H5ow8TRkgZEJ5O04oC3hMIuha8GaEXsI0M=
git.gammaspectra.live/S.O.N.G/goflac v0.0.0-20220417181802-3057bde44c07/go.mod h1:/po1QgOh3xynbvi4sxdY6Iw8m5WPJfGGmry2boZD8fs=
github.com/cocoonlife/testify v0.0.0-20160218172820-792cc1faeb64 h1:LjPYdzoFSAJ5Tr/ElL8kzTJghXgpnOjJVbgd1UvZB1o=
github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkEQxENCrlLo=
github.com/dh1tw/gosamplerate v0.1.2 h1:oyqtZk67xB9B4l+vIZCZ3F0RYV/z66W58VOah11/ktI=