Revamp the transcoding decision tree

This commit is contained in:
fijam
2020-09-20 10:49:16 +02:00
committed by Senan Kelly
parent d6875a184e
commit 74f3c5a015
2 changed files with 16 additions and 9 deletions

View File

@@ -146,14 +146,6 @@ func cacheKey(sourcePath string, profileName string, profile Profile) string {
)
}
// getBitrate checks if the client forces bitrate lower than set in profile
func getBitrate(preferred, defined int) int {
if preferred != 0 && preferred < defined {
return preferred
}
return defined
}
type (
OnInvalidProfileFunc func() error
OnCacheHitFunc func(Profile, string) error
@@ -162,6 +154,7 @@ type (
type Options struct {
TrackPath string
TrackBitrate int
CachePath string
ProfileName string
PreferredBitrate int
@@ -175,7 +168,20 @@ func Encode(opts Options) error {
if !ok {
return opts.OnInvalidProfile()
}
profile.Bitrate = getBitrate(opts.PreferredBitrate, profile.Bitrate)
log.Printf("client requests %dk, transcoding profile %dk, track bitrate %dk \n", opts.PreferredBitrate, profile.Bitrate, opts.TrackBitrate)
if opts.PreferredBitrate != 0 && opts.PreferredBitrate >= opts.TrackBitrate {
log.Printf("Not transcoding, requested bitrate larger or equal to track bitrate \n")
return opts.OnInvalidProfile()
} else if opts.PreferredBitrate != 0 && opts.PreferredBitrate < opts.TrackBitrate {
profile.Bitrate = opts.PreferredBitrate
log.Printf("Transcoding according to client request of %dk \n", profile.Bitrate)
} else if opts.PreferredBitrate == 0 && profile.Bitrate >= opts.TrackBitrate {
log.Printf("Not transcoding, transcoding profile bitrate larger or equal to track bitrate \n")
return opts.OnInvalidProfile()
} else {
log.Printf("Transcoding according to transcoding profile of %dk \n", profile.Bitrate)
}
cacheKey := cacheKey(opts.TrackPath, opts.ProfileName, profile)
cachePath := path.Join(opts.CachePath, cacheKey)
if fileExists(cachePath) {