Add hwaccelerated scalling flags

This commit is contained in:
Zoe Roux 2024-02-23 02:08:31 +01:00
parent ae1dee9d51
commit 43350ee1fd
6 changed files with 36 additions and 3 deletions

12
docker-compose.nvidia.yml Normal file
View File

@ -0,0 +1,12 @@
version: "3.8"
services:
transcoder:
runtime: nvidia
deploy:
resources:
reservations:
devices:
- capabilities: [gpu]
environment:
- GOTRANSCODER_HWACCEL=nvidia

View File

@ -2,6 +2,7 @@ FROM golang:1.21 as build
RUN apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
ffmpeg libavformat-dev libavutil-dev libswscale-dev libmediainfo-dev \
&& apt-get clean autoclean -y \
&& apt-get autoremove -y
WORKDIR /app
COPY go.mod go.sum ./

View File

@ -2,6 +2,7 @@ FROM golang:1.21
RUN apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
ffmpeg libavformat-dev libavutil-dev libswscale-dev libmediainfo-dev \
&& apt-get clean autoclean -y \
&& apt-get autoremove -y
RUN go install github.com/bokwoon95/wgo@latest
WORKDIR /app

View File

@ -1,7 +1,11 @@
package src
import "log"
func DetectHardwareAccel() HwAccelT {
name := "disabled"
name := GetEnvOr("GOTRANSCODER_HWACCEL", "disabled")
log.Printf("Using hardware acceleration: %s", name)
switch name {
case "nvidia":
return HwAccelT{
@ -17,6 +21,7 @@ func DetectHardwareAccel() HwAccelT {
"-c:v", "h264_nvenc",
"-preset", "fast",
},
ScaleFilter: "hwupload_cuda,scale_cuda=%d:%d:force_original_aspect_ratio=decrease",
}
default:
return HwAccelT{
@ -27,6 +32,7 @@ func DetectHardwareAccel() HwAccelT {
// superfast or ultrafast would produce a file extremly big so we prever veryfast or faster.
"-preset", "faster",
},
ScaleFilter: "scale=%d:%d:force_original_aspect_ratio=decrease",
}
}
}

View File

@ -20,6 +20,7 @@ type HwAccelT struct {
Name string
DecodeFlags []string
EncodeFlags []string
ScaleFilter string
}
var Settings = SettingsT{

View File

@ -29,6 +29,16 @@ func (vs *VideoStream) getOutPath(encoder_id int) string {
return fmt.Sprintf("%s/segment-%s-%d-%%d.ts", vs.file.Out, vs.quality, encoder_id)
}
func closestMultiple(n int32, x int32) int32 {
if x > n {
return x
}
n = n + x/2
n = n - (n % x)
return n
}
func (vs *VideoStream) getTranscodeArgs(segments string) []string {
args := []string{
"-map", "0:V:0",
@ -42,9 +52,11 @@ func (vs *VideoStream) getTranscodeArgs(segments string) []string {
}
args = append(args, Settings.HwAccel.EncodeFlags...)
width := int32(float64(vs.quality.Height()) / float64(vs.file.Info.Video.Height) * float64(vs.file.Info.Video.Width))
// force a width that is a multiple of two else some apps behave badly.
width = closestMultiple(width, 2)
args = append(args,
// 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(Settings.HwAccel.ScaleFilter, width, vs.quality.Height()),
// Even less sure but bufsize are 5x the avergae bitrate since the average bitrate is only
// useful for hls segments.
"-bufsize", fmt.Sprint(vs.quality.MaxBitrate()*5),