Skip to content

Commit

Permalink
Merge pull request #352 from bcc-code/feat/mp3-cbr-support
Browse files Browse the repository at this point in the history
Implement CBR for MP3
  • Loading branch information
KillerX authored Nov 28, 2024
2 parents 82b7a00 + ecd2013 commit e9c8a99
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
3 changes: 3 additions & 0 deletions common/transcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type AudioInput struct {
Path paths.Path
Bitrate string
DestinationPath paths.Path

// Not all codecs may support constant bitrate
ForceCBR bool
}

type DetectSilenceInput struct {
Expand Down
1 change: 1 addition & 0 deletions services/ffmpeg/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func GetStreamInfo(path string) (StreamInfo, error) {
if err != nil {
return StreamInfo{}, err
}

return ProbeResultToInfo(info), nil
}

Expand Down
11 changes: 9 additions & 2 deletions services/transcode/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package transcode
import (
"bytes"
"fmt"
"github.com/bcc-code/bcc-media-flows/utils"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/bcc-code/bcc-media-flows/utils"

"github.com/bcc-code/bcc-media-flows/paths"

"github.com/bcc-code/bcc-media-flows/common"
Expand Down Expand Up @@ -284,9 +285,15 @@ func AudioMP3(input common.AudioInput, cb ffmpeg.ProgressCallback) (*common.Audi
"-hide_banner",
"-i", input.Path.Local(),
"-c:a", "libmp3lame",
"-q:a", fmt.Sprint(getQfactorFromBitrate(input.Bitrate)),
}

if input.ForceCBR {
params = append(params, "-b:a", input.Bitrate)
} else {
params = append(params, "-q:a", fmt.Sprint(getQfactorFromBitrate(input.Bitrate)))
}


outputFilePath := filepath.Join(input.DestinationPath.Local(), input.Path.Base())
outputFilePath = fmt.Sprintf("%s-%s.mp3", outputFilePath[:len(outputFilePath)-len(filepath.Ext(outputFilePath))], input.Bitrate)

Expand Down
59 changes: 56 additions & 3 deletions services/transcode/audio_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package transcode

import (
"github.com/bcc-code/bcc-media-flows/services/ffmpeg"
"github.com/bcc-code/bcc-media-flows/utils/testutils"
"os"
"testing"

"github.com/bcc-code/bcc-media-flows/services/ffmpeg"
"github.com/bcc-code/bcc-media-flows/utils/testutils"

"github.com/bcc-code/bcc-media-flows/common"
"github.com/bcc-code/bcc-media-flows/paths"
"github.com/stretchr/testify/assert"
)

func Test_Audio(t *testing.T) {
func Test_AACEncode(t *testing.T) {
tempDstPath := paths.MustParse("./testdata/test" + t.Name() + ".wav")
err := GenerateToneFile(1000, 5, 48000, "01:00:00:00", tempDstPath)
assert.NoError(t, err)
Expand All @@ -35,6 +36,58 @@ func Test_Audio(t *testing.T) {
assert.Equal(t, 1, len(info.AudioStreams))
}

func Test_MP3Encode_VBR(t *testing.T) {
tempDstPath := paths.MustParse("./testdata/test" + t.Name() + ".wav")
err := GenerateToneFile(1000, 5, 48000, "01:00:00:00", tempDstPath)
assert.NoError(t, err)

p, stop := printProgress()
defer close(stop)
res, err := AudioMP3(common.AudioInput{
Path: tempDstPath,
DestinationPath: tempDstPath.Dir(),
Bitrate: "128k",
ForceCBR: false,
}, p)
assert.Nil(t, err)
assert.NotNil(t, res)

info, err := ffmpeg.GetStreamInfo(res.OutputPath.Local())
assert.NoError(t, err)

assert.True(t, info.HasAudio)
assert.False(t, info.HasVideo)
assert.InDelta(t, 5, 0.2, info.TotalSeconds)
assert.Equal(t, 1, len(info.AudioStreams))

}

func Test_MP3Encode_CBR(t *testing.T) {
tempDstPath := paths.MustParse("./testdata/test" + t.Name() + ".wav")
err := GenerateToneFile(1000, 5, 48000, "01:00:00:00", tempDstPath)
assert.NoError(t, err)

p, stop := printProgress()
defer close(stop)
res, err := AudioMP3(common.AudioInput{
Path: tempDstPath,
DestinationPath: tempDstPath.Dir(),
Bitrate: "128k",
ForceCBR: true,
}, p)
assert.Nil(t, err)
assert.NotNil(t, res)

info, err := ffmpeg.GetStreamInfo(res.OutputPath.Local())
assert.NoError(t, err)

assert.True(t, info.HasAudio)
assert.False(t, info.HasVideo)
assert.InDelta(t, 5, 0.2, info.TotalSeconds)
assert.Equal(t, 1, len(info.AudioStreams))
assert.Equal(t, "128000", info.AudioStreams[0].BitRate)
}

func Test_AudioSplit_Stereo(t *testing.T) {
tempDstPath := paths.MustParse("./testdata/test" + t.Name() + ".wav")
testutils.GenerateMultichannelAudioFile(tempDstPath, 2, 10)
Expand Down
6 changes: 4 additions & 2 deletions workflows/export/vx_export_bmm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package export
import (
"crypto/sha1"
"fmt"
bccmflows "github.com/bcc-code/bcc-media-flows"
"github.com/bcc-code/bcc-media-flows/services/telegram"
"path"
"strconv"
"strings"
"time"

bccmflows "github.com/bcc-code/bcc-media-flows"
"github.com/bcc-code/bcc-media-flows/services/telegram"

pcommon "github.com/bcc-code/bcc-media-platform/backend/common"

platform_activities "github.com/bcc-code/bcc-media-flows/activities/platform"
Expand Down Expand Up @@ -122,6 +123,7 @@ func VXExportToBMM(ctx workflow.Context, params VXExportChildWorkflowParams) (*V
Path: audio.FilePath,
DestinationPath: params.OutputDir,
Bitrate: bitrate,
ForceCBR: true,
})
encodings = append(encodings, f.Future)
}
Expand Down

0 comments on commit e9c8a99

Please sign in to comment.