Skip to content
This repository was archived by the owner on Apr 21, 2020. It is now read-only.

Commit 8964019

Browse files
authored
adding opus codec. (#143)
1 parent d3f5f7e commit 8964019

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

provider/bitmovin/bitmovin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040

4141
codecVorbis = "vorbis"
4242
codecAAC = "aac"
43+
codecOpus = "opus"
4344
codecVP8 = "vp8"
4445
codecH264 = "h264"
4546
codecH265 = "h265"
@@ -58,6 +59,7 @@ const (
5859
cfgStoreH265 cfgStore = "h265"
5960
cfgStoreAV1 cfgStore = "av1"
6061
cfgStoreAAC cfgStore = "aac"
62+
cfgStoreOpus cfgStore = "opus"
6163

6264
filterVideoDeinterlace = iota + 1
6365
)
@@ -155,6 +157,7 @@ func bitmovinFactory(cfg *config.Config) (provider.TranscodingProvider, error) {
155157
cfgStoreH265AAC: configuration.NewH265AAC(api, dbRepo),
156158
cfgStoreVP8Vorbis: configuration.NewVP8Vorbis(api, dbRepo),
157159
cfgStoreAAC: configuration.NewAAC(api, dbRepo),
160+
cfgStoreOpus: configuration.NewOpus(api, dbRepo),
158161
},
159162
containerSvcs: map[mediaContainer]containerSvc{
160163
containerHLS: {
@@ -680,6 +683,8 @@ func (p *bitmovinProvider) cfgServiceFrom(vcodec, acodec string) (configuration.
680683
return p.cfgStores[cfgStoreAV1], nil
681684
case codecAAC:
682685
return p.cfgStores[cfgStoreAAC], nil
686+
case codecOpus:
687+
return p.cfgStores[cfgStoreOpus], nil
683688
}
684689

685690
return nil, fmt.Errorf("the pair of vcodec: %q and acodec: %q is not yet supported", vcodec, acodec)

provider/bitmovin/internal/configuration/codec/aac.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
const defaultAACSampleRate = 48000
1313

14-
// NewAAC creates a AAC codec configuration and returns its ID
14+
// NewAAC creates an AAC codec configuration and returns its ID
1515
func NewAAC(api *bitmovin.BitmovinApi, bitrate string) (string, error) {
1616
createCfg, err := aacConfigFrom(bitrate)
1717
if err != nil {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package codec
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/bitmovin/bitmovin-api-sdk-go"
8+
"github.com/bitmovin/bitmovin-api-sdk-go/model"
9+
"github.com/pkg/errors"
10+
)
11+
12+
const defaultOpusSampleRate = 48000
13+
14+
// NewOpus creates an Opus codec configuration and returns its ID
15+
func NewOpus(api *bitmovin.BitmovinApi, bitrate string) (string, error) {
16+
createCfg, err := opusConfigFrom(bitrate)
17+
if err != nil {
18+
return "", err
19+
}
20+
21+
cfg, err := api.Encoding.Configurations.Audio.Opus.Create(createCfg)
22+
if err != nil {
23+
return "", errors.Wrap(err, "creating audio cfg")
24+
}
25+
26+
return cfg.Id, nil
27+
}
28+
29+
func opusConfigFrom(bitrate string) (model.OpusAudioConfiguration, error) {
30+
convertedBitrate, err := strconv.ParseInt(bitrate, 10, 64)
31+
if err != nil {
32+
return model.OpusAudioConfiguration{}, errors.Wrapf(err, "parsing audio bitrate %q to int64", bitrate)
33+
}
34+
35+
return model.OpusAudioConfiguration{
36+
Name: fmt.Sprintf("opus_%s_%d", bitrate, defaultOpusSampleRate),
37+
Bitrate: &convertedBitrate,
38+
Rate: floatToPtr(defaultOpusSampleRate),
39+
}, nil
40+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package configuration
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/bitmovin/bitmovin-api-sdk-go"
7+
"github.com/bitmovin/bitmovin-api-sdk-go/model"
8+
"github.com/cbsinteractive/video-transcoding-api/db"
9+
"github.com/cbsinteractive/video-transcoding-api/provider/bitmovin/internal/configuration/codec"
10+
"github.com/pkg/errors"
11+
)
12+
13+
// Opus is a configuration service for content using only this codec
14+
type Opus struct {
15+
api *bitmovin.BitmovinApi
16+
repo db.PresetSummaryRepository
17+
}
18+
19+
// NewOpus returns a service for managing Opus audio only configurations
20+
func NewOpus(api *bitmovin.BitmovinApi, repo db.PresetSummaryRepository) *Opus {
21+
return &Opus{api: api, repo: repo}
22+
}
23+
24+
// Create will create a new Opus configuration based on a preset
25+
func (c *Opus) Create(preset db.Preset) (string, error) {
26+
audCfgID, err := codec.NewOpus(c.api, preset.Audio.Bitrate)
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
err = c.repo.CreatePresetSummary(&db.PresetSummary{
32+
Name: preset.Name,
33+
Container: preset.Container,
34+
AudioCodec: string(model.CodecConfigType_OPUS),
35+
AudioConfigID: audCfgID,
36+
})
37+
if err != nil {
38+
return "", err
39+
}
40+
41+
return preset.Name, nil
42+
}
43+
44+
// Get retrieves a stored db.PresetSummary by its name
45+
func (c *Opus) Get(presetName string) (db.PresetSummary, error) {
46+
return c.repo.GetPresetSummary(presetName)
47+
}
48+
49+
// Delete removes the audio configurations
50+
func (c *Opus) Delete(presetName string) error {
51+
summary, err := c.Get(presetName)
52+
if err != nil {
53+
return err
54+
}
55+
56+
_, err = c.api.Encoding.Configurations.Audio.Opus.Delete(summary.AudioConfigID)
57+
if err != nil {
58+
return errors.Wrap(err, "removing the audio config")
59+
}
60+
61+
err = c.repo.DeletePresetSummary(presetName)
62+
if err != nil {
63+
return fmt.Errorf("deleting preset summary: %w", err)
64+
}
65+
66+
return nil
67+
}

0 commit comments

Comments
 (0)