Skip to content

Commit

Permalink
Added onne more thing to attempt prevention of the %2 error for H264
Browse files Browse the repository at this point in the history
Also added test
  • Loading branch information
KillerX committed Aug 20, 2024
1 parent af26304 commit 1b6d34f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
11 changes: 8 additions & 3 deletions services/transcode/h264.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package transcode

import (
"github.com/bcc-code/bcc-media-flows/utils"
"os"
"path/filepath"
"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/services/ffmpeg"
"github.com/samber/lo"
Expand Down Expand Up @@ -69,12 +70,18 @@ func H264(input H264EncodeInput, progressCallback ffmpeg.ProgressCallback) (*Enc
)
}

var videoFilters []string

if input.Resolution != nil {
input.Resolution.EnsureEven()
params = append(
params,
"-s", input.Resolution.FFMpegString(),
)

// This ensures that the scaled image is evenly divisible by 2,
// because the `-s` keeps the aspect ratio and thus can't be forced to be even
videoFilters = append(videoFilters, "scale=trunc(iw/2)*2:trunc(ih/2)*2")
}

if input.FrameRate != 0 {
Expand All @@ -84,8 +91,6 @@ func H264(input H264EncodeInput, progressCallback ffmpeg.ProgressCallback) (*Enc
)
}

var videoFilters []string

if input.Interlace {
params = append(
params,
Expand Down
45 changes: 45 additions & 0 deletions services/transcode/h264_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package transcode_test

import (
"testing"

"github.com/bcc-code/bcc-media-flows/paths"
"github.com/bcc-code/bcc-media-flows/services/ffmpeg"
"github.com/bcc-code/bcc-media-flows/services/transcode"
"github.com/bcc-code/bcc-media-flows/utils"
"github.com/bcc-code/bcc-media-flows/utils/testutils"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)

func Test_H264_WeirdResolutions(t *testing.T) {
testFile := paths.MustParse("./testdata/generated/h264_weird_resolutions.mov")

testutils.GenerateVideoFile(testFile, testutils.VideoGeneratorParams{
DAR: "16/9",
SAR: "608/405",
Width: 720,
Height: 608,
Duration: 5,
FrameRate: 25,
})

progressCallback := func(i ffmpeg.Progress) {
spew.Dump(i)
}

r, err := transcode.H264(transcode.H264EncodeInput{
Bitrate: "320k",
Resolution: &utils.Resolution{
Width: 320,
Height: 180,
},
FrameRate: 0,
FilePath: testFile.Local(),
OutputDir: testFile.Dir().Local(),
}, progressCallback)

assert.NoError(t, err)
assert.NotNil(t, r)
spew.Dump(r)
}
37 changes: 37 additions & 0 deletions utils/testutils/video.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package testutils

import (
"fmt"
"os/exec"

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

type VideoGeneratorParams struct {
Duration float64
FrameRate int
Width int
Height int
SAR string
DAR string
}

func GenerateVideoFile(outFile paths.Path, videoParams VideoGeneratorParams) paths.Path {
args := []string{
"-f", "lavfi",
"-i", fmt.Sprintf("color=c=blue:s=720x608:d=%f", videoParams.Duration),
"-vf", fmt.Sprintf("setsar=%s, setdar=%s", videoParams.SAR, videoParams.DAR),
"-r", fmt.Sprintf("%d", videoParams.FrameRate),
"-c:v", "prores_ks",
"-profile:v", "3",
"-y", outFile.Local(),
}

cmd := exec.Command("ffmpeg", args...)
err := cmd.Run()
if err != nil {
panic(err)
}

return outFile
}

0 comments on commit 1b6d34f

Please sign in to comment.