Add flags handling to support hwaccel

This commit is contained in:
Zoe Roux 2024-02-23 00:55:42 +01:00
parent a383de971a
commit ff2d077a7f
4 changed files with 66 additions and 9 deletions

32
transcoder/src/hwaccel.go Normal file
View File

@ -0,0 +1,32 @@
package src
func DetectHardwareAccel() HwAccelT {
name := "disabled"
switch name {
case "nvidia":
return HwAccelT{
Name: "nvidia",
DecodeFlags: []string{
// TODO: check if this can always be enabled (for example with weird video formats)
"-hwaccel", "cuda",
// this flag prevents data to go from gpu space to cpu space
// it forces the whole dec/enc to be on the gpu. We want that.
"-hwaccel_output_format", "cuda",
},
EncodeFlags: []string{
"-c:v", "h264_nvenc",
"-preset", "fast",
},
}
default:
return HwAccelT{
Name: "disabled",
DecodeFlags: []string{},
EncodeFlags: []string{
"-c:v", "libx264",
// superfast or ultrafast would produce a file extremly big so we prever veryfast or faster.
"-preset", "faster",
},
}
}
}

View File

@ -13,9 +13,17 @@ func GetEnvOr(env string, def string) string {
type SettingsT struct { type SettingsT struct {
Outpath string Outpath string
Metadata string Metadata string
HwAccel HwAccelT
}
type HwAccelT struct {
Name string
DecodeFlags []string
EncodeFlags []string
} }
var Settings = SettingsT{ var Settings = SettingsT{
Outpath: GetEnvOr("GOCODER_CACHE_ROOT", "/cache"), Outpath: GetEnvOr("GOCODER_CACHE_ROOT", "/cache"),
Metadata: GetEnvOr("GOCODER_METADATA_ROOT", "/metadata"), Metadata: GetEnvOr("GOCODER_METADATA_ROOT", "/metadata"),
HwAccel: DetectHardwareAccel(),
} }

View File

@ -20,6 +20,7 @@ type Flags int32
const ( const (
AudioF Flags = 1 << 0 AudioF Flags = 1 << 0
VideoF Flags = 1 << 1 VideoF Flags = 1 << 1
Transmux Flags = 1 << 3
) )
type StreamHandle interface { type StreamHandle interface {
@ -166,6 +167,12 @@ func (ts *Stream) run(start int32) error {
"-nostats", "-hide_banner", "-loglevel", "warning", "-nostats", "-hide_banner", "-loglevel", "warning",
} }
// do not enabled hardware accelaration when transmuxing
// this can either be slower or fail depending on hardware
if ts.handle.getFlags()&Transmux == 0 {
args = append(args, Settings.HwAccel.DecodeFlags...)
}
if start_ref != 0 { if start_ref != 0 {
if ts.handle.getFlags()&VideoF != 0 { if ts.handle.getFlags()&VideoF != 0 {
// This is the default behavior in transmux mode and needed to force pre/post segment to work // This is the default behavior in transmux mode and needed to force pre/post segment to work

View File

@ -19,6 +19,9 @@ func NewVideoStream(file *FileStream, quality Quality) *VideoStream {
} }
func (vs *VideoStream) getFlags() Flags { func (vs *VideoStream) getFlags() Flags {
if vs.quality == Original {
return VideoF & Transmux
}
return VideoF return VideoF
} }
@ -27,13 +30,19 @@ func (vs *VideoStream) getOutPath(encoder_id int) string {
} }
func (vs *VideoStream) getTranscodeArgs(segments string) []string { func (vs *VideoStream) getTranscodeArgs(segments string) []string {
if vs.quality == Original { args := []string{
return []string{"-map", "0:V:0", "-c:v", "copy"} "-map", "0:V:0",
} }
return []string{ if vs.quality == Original {
// superfast or ultrafast would produce a file extremly big so we prever veryfast or faster. args = append(args,
"-map", "0:V:0", "-c:v", "libx264", "-crf", "21", "-preset", "faster", "-c:v", "copy",
)
return args
}
args = append(args, Settings.HwAccel.EncodeFlags...)
args = append(args,
// resize but keep aspect ratio (also force a width that is a multiple of two else some apps behave badly. // resize but keep aspect ratio (also force a width that is a multiple of two else some apps behave badly.
"-vf", fmt.Sprintf("scale=-2:'min(%d,ih)'", vs.quality.Height()), "-vf", fmt.Sprintf("scale=-2:'min(%d,ih)'", vs.quality.Height()),
// Even less sure but bufsize are 5x the avergae bitrate since the average bitrate is only // Even less sure but bufsize are 5x the avergae bitrate since the average bitrate is only
@ -48,5 +57,6 @@ func (vs *VideoStream) getTranscodeArgs(segments string) []string {
// we disable it to prevents whole scenes from behing removed due to the -f segment failing to find the corresonding keyframe // we disable it to prevents whole scenes from behing removed due to the -f segment failing to find the corresonding keyframe
"-sc_threshold", "0", "-sc_threshold", "0",
"-strict", "-2", "-strict", "-2",
} )
return args
} }