diff --git a/video/profiles.go b/video/profiles.go index 90a9199b..178f60d2 100644 --- a/video/profiles.go +++ b/video/profiles.go @@ -13,6 +13,9 @@ const ( TrackTypeVideo = "video" TrackTypeAudio = "audio" defaultQuality = 27 + // Livepeer network uses bitrate to set max bitrate for encoding, so for the video to look good, we multiply + // it by a factor of 1.2. + maxBitrateFactor = 1.2 ) type InputVideo struct { @@ -143,9 +146,7 @@ func GetDefaultPlaybackProfiles(video InputTrack) ([]EncodedProfile, error) { lowerQualityThanSrc := profile.Height < video.Height && profile.Bitrate < video.Bitrate if lowerQualityThanSrc { // relativeBitrate needs to be slightly higher than the proportional average bitrate of the source video. - // Livepeer network uses bitrate to set max bitrate for encoding, so for the video to look good, we multiply - // it by a factor of 1.2. - relativeBitrate := 1.2 * float64(profile.Width*profile.Height) * (float64(videoBitrate) / float64(video.Width*video.Height)) + relativeBitrate := maxBitrateFactor * float64(profile.Width*profile.Height) * (float64(videoBitrate) / float64(video.Width*video.Height)) br := math.Min(relativeBitrate, float64(profile.Bitrate)) profile.Bitrate = int64(br) profiles = append(profiles, profile) @@ -156,7 +157,7 @@ func GetDefaultPlaybackProfiles(video InputTrack) ([]EncodedProfile, error) { } profiles = append(profiles, EncodedProfile{ Name: strconv.FormatInt(nearestEven(video.Height), 10) + "p0", - Bitrate: videoBitrate, + Bitrate: int64(math.Min(maxBitrateFactor*float64(videoBitrate), MaxVideoBitrate)), FPS: 0, Width: nearestEven(video.Width), Height: nearestEven(video.Height), diff --git a/video/profiles_test.go b/video/profiles_test.go index 2428ba2e..f4c5c896 100644 --- a/video/profiles_test.go +++ b/video/profiles_test.go @@ -27,7 +27,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) { }, want: []EncodedProfile{ {Name: "low-bitrate", Width: 640, Height: 360, Bitrate: 500_000, Quality: defaultQuality}, - {Name: "360p0", Width: 640, Height: 360, Bitrate: 1_000_001, Quality: defaultQuality}, + {Name: "360p0", Width: 640, Height: 360, Bitrate: 1_200_001, Quality: defaultQuality}, }, }, { @@ -42,7 +42,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) { }, want: []EncodedProfile{ {Name: "low-bitrate", Width: 640, Height: 360, Bitrate: 250_000, Quality: defaultQuality}, - {Name: "360p0", Width: 640, Height: 360, Bitrate: 500_000, Quality: defaultQuality}, + {Name: "360p0", Width: 640, Height: 360, Bitrate: 600_000, Quality: defaultQuality}, }, }, { @@ -57,7 +57,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) { }, want: []EncodedProfile{ {Name: "360p0", Width: 640, Height: 360, Bitrate: 1_000_000, Quality: defaultQuality}, - {Name: "720p0", Width: 1280, Height: 720, Bitrate: 4_000_001, Quality: defaultQuality}, + {Name: "720p0", Width: 1280, Height: 720, Bitrate: 4_800_001, Quality: defaultQuality}, }, }, { @@ -72,7 +72,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) { }, want: []EncodedProfile{ {Name: "360p0", Width: 640, Height: 360, Bitrate: 320_000, Quality: defaultQuality}, - {Name: "720p0", Width: 1200, Height: 720, Bitrate: 1_000_001, Quality: defaultQuality}, + {Name: "720p0", Width: 1200, Height: 720, Bitrate: 1_200_001, Quality: defaultQuality}, }, }, { @@ -88,7 +88,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) { want: []EncodedProfile{ {Name: "360p0", Width: 640, Height: 360, Bitrate: 666_666, Quality: defaultQuality}, {Name: "720p0", Width: 1280, Height: 720, Bitrate: 2_666_666, Quality: defaultQuality}, - {Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 5_000_000, Quality: defaultQuality}, + {Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 6_000_000, Quality: defaultQuality}, }, }, { @@ -103,7 +103,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) { }, want: []EncodedProfile{ {Name: "low-bitrate", Width: 400, Height: 240, Bitrate: 258549, Quality: defaultQuality}, - {Name: "240p0", Width: 400, Height: 240, Bitrate: 517099, Quality: defaultQuality}, + {Name: "240p0", Width: 400, Height: 240, Bitrate: 620518, Quality: defaultQuality}, }, }, { @@ -134,7 +134,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) { }, want: []EncodedProfile{ {Name: "360p0", Width: 640, Height: 360, Bitrate: 146_666, Quality: defaultQuality}, - {Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 1_100_000, Quality: defaultQuality}, + {Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 1_320_000, Quality: defaultQuality}, }, }, { @@ -149,7 +149,7 @@ func TestGetDefaultPlaybackProfiles(t *testing.T) { }, want: []EncodedProfile{ {Name: "360p0", Width: 640, Height: 360, Bitrate: 146_666, Quality: defaultQuality}, - {Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 1_100_000, Quality: defaultQuality}, + {Name: "1080p0", Width: 1920, Height: 1080, Bitrate: 1_320_000, Quality: defaultQuality}, }, }, }