Skip to content

Commit

Permalink
video: Avoid copying source for clips
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Jul 19, 2024
1 parent 616c219 commit 5c4e430
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion transcode/transcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func RunTranscodeProcess(transcodeRequest TranscodeSegmentRequest, streamName st
sourceManifestOSURL := transcodeRequest.SourceManifestURL

// transcodeProfiles are desired constraints for transcoding process
transcodeProfiles, err := video.SetTranscodeProfiles(inputInfo, transcodeRequest.Profiles)
transcodeProfiles, err := video.SetTranscodeProfiles(inputInfo, transcodeRequest.Profiles, transcodeRequest.IsClip)
if err != nil {
return outputs, segmentsCount, fmt.Errorf("failed to set playback profiles: %w", err)
} else if len(transcodeProfiles) == 0 {
Expand Down
7 changes: 4 additions & 3 deletions video/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var DefaultProfile720p = EncodedProfile{
// DefaultTranscodeProfiles defines the default set of encoding profiles to use when none are specified
var DefaultTranscodeProfiles = []EncodedProfile{DefaultProfile360p, DefaultProfile720p}

func SetTranscodeProfiles(inputVideoStats InputVideo, transcodeProfiles []EncodedProfile) ([]EncodedProfile, error) {
func SetTranscodeProfiles(inputVideoStats InputVideo, transcodeProfiles []EncodedProfile, isClip bool) ([]EncodedProfile, error) {
videoTrack, err := inputVideoStats.GetTrack(TrackTypeVideo)
if err != nil {
return nil, fmt.Errorf("no video track found in input video: %w", err)
Expand All @@ -118,8 +118,9 @@ func SetTranscodeProfiles(inputVideoStats InputVideo, transcodeProfiles []Encode
transcodeProfiles = GenerateSingleProfileWithTargetParams(videoTrack, transcodeProfiles[0])
}
}
// Always copy the source video for HLS input
copySource := inputVideoStats.Format == "hls"

// Always copy the source rendition for HLS input unless it's a clip, which needs PTS correction through transcode
copySource := inputVideoStats.Format == "hls" && !isClip
// If Profiles were not specified, use the default set. Notice that they can come
// as an empty array for no transcoding, which is why we check nil instead of len
if transcodeProfiles == nil {
Expand Down
40 changes: 39 additions & 1 deletion video/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func TestSetTranscodeProfiles(t *testing.T) {
tests := []struct {
name string
input InputVideo
isClip bool
transcodeProfiles []EncodedProfile
want []EncodedProfile
}{
Expand Down Expand Up @@ -274,6 +275,23 @@ func TestSetTranscodeProfiles(t *testing.T) {
transcodeProfiles: []EncodedProfile{},
want: []EncodedProfile{{Name: "720p0", Width: 1280, Height: 720, Bitrate: 3_000_001, Copy: true}},
},
{
name: "does not add copy profile if clip",
input: InputVideo{
Format: "hls",
Tracks: []InputTrack{{
Type: "video",
Bitrate: 3_000_001,
VideoTrack: VideoTrack{
Width: 1280,
Height: 720,
},
}},
},
isClip: true,
transcodeProfiles: []EncodedProfile{{Name: "360p0", Width: 640, Height: 360, Bitrate: 900_000, Quality: 5}},
want: []EncodedProfile{{Name: "360p0", Width: 640, Height: 360, Bitrate: 900_000, Quality: 5}},
},
{
name: "includes copy profile in default profiles if hls input",
input: InputVideo{
Expand All @@ -293,10 +311,30 @@ func TestSetTranscodeProfiles(t *testing.T) {
{Name: "720p0", Width: 1280, Height: 720, Bitrate: 3_000_001, Copy: true},
},
},
{
name: "does not include copy profile in default profiles if clip",
input: InputVideo{
Format: "mp4",
Tracks: []InputTrack{{
Type: "video",
Bitrate: 3_000_001,
VideoTrack: VideoTrack{
Width: 1280,
Height: 720,
},
}},
},
isClip: true,
transcodeProfiles: nil,
want: []EncodedProfile{
{Name: "360p0", Width: 640, Height: 360, Bitrate: 900_000, Quality: DefaultQuality},
{Name: "720p0", Width: 1280, Height: 720, Bitrate: 3_600_001, Quality: DefaultQuality},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SetTranscodeProfiles(tt.input, tt.transcodeProfiles)
got, err := SetTranscodeProfiles(tt.input, tt.transcodeProfiles, tt.isClip)
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
Expand Down

0 comments on commit 5c4e430

Please sign in to comment.