Skip to content

Commit

Permalink
CBR for streams (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostbyte73 authored Oct 4, 2024
1 parent 879d7e5 commit 9a4c230
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pkg/pipeline/builder/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,19 +536,20 @@ func (b *VideoBin) addEncoder() error {
if err != nil {
return errors.ErrGstPipelineError(err)
}

x264Enc.SetArg("speed-preset", "veryfast")
if b.conf.KeyFrameInterval != 0 {
keyframeInterval := uint(b.conf.KeyFrameInterval * float64(b.conf.Framerate))
if err = x264Enc.SetProperty("key-int-max", keyframeInterval); err != nil {
return errors.ErrGstPipelineError(err)
}
}

var options []string
bufCapacity := uint(2000) // 2s
if b.conf.GetSegmentConfig() != nil {
// avoid key frames other than at segments boundaries as splitmuxsink can become inconsistent otherwise
if err = x264Enc.SetProperty("option-string", "scenecut=0"); err != nil {
return errors.ErrGstPipelineError(err)
}
options = append(options, "scenecut=0")
bufCapacity = uint(time.Duration(b.conf.GetSegmentConfig().SegmentDuration) * (time.Second / time.Millisecond))
}
if bufCapacity > 10000 {
Expand All @@ -558,13 +559,21 @@ func (b *VideoBin) addEncoder() error {
if err = x264Enc.SetProperty("vbv-buf-capacity", bufCapacity); err != nil {
return errors.ErrGstPipelineError(err)
}
if b.conf.GetStreamConfig() != nil {
x264Enc.SetArg("pass", "cbr")
}

if err = x264Enc.SetProperty("bitrate", uint(b.conf.VideoBitrate)); err != nil {
return errors.ErrGstPipelineError(err)
}

if b.conf.GetStreamConfig() != nil {
options = append(options, "nal-hrd=cbr")
}
if len(options) > 0 {
optionString := strings.Join(options, ":")
if err = x264Enc.SetProperty("option-string", optionString); err != nil {
return errors.ErrGstPipelineError(err)
}
}

caps, err := gst.NewElement("capsfilter")
if err != nil {
return errors.ErrGstPipelineError(err)
Expand Down

0 comments on commit 9a4c230

Please sign in to comment.