Compare commits
2 Commits
c3a9cbd69e
...
ec967d50e7
| Author | SHA1 | Date | |
|---|---|---|---|
| ec967d50e7 | |||
| ce5ad6e7fa |
90
main.go
90
main.go
|
|
@ -3712,6 +3712,31 @@ func (s *appState) executeSnippetJob(ctx context.Context, job *queue.Job, progre
|
|||
center := math.Max(0, src.Duration/2-halfLength)
|
||||
start := fmt.Sprintf("%.2f", center)
|
||||
|
||||
clampSnippetBitrate := func(bitrate string, width int) string {
|
||||
val := strings.TrimSpace(strings.ToLower(bitrate))
|
||||
val = strings.TrimSuffix(val, "bps")
|
||||
val = strings.TrimSuffix(val, "k")
|
||||
n, err := strconv.ParseFloat(val, 64)
|
||||
if err != nil || n <= 0 {
|
||||
n = 3500
|
||||
}
|
||||
capKbps := 5000.0
|
||||
if width >= 3840 {
|
||||
capKbps = 30000
|
||||
} else if width >= 1920 {
|
||||
capKbps = 15000
|
||||
} else if width >= 1280 {
|
||||
capKbps = 8000
|
||||
}
|
||||
if n > capKbps {
|
||||
n = capKbps
|
||||
}
|
||||
if n < 800 {
|
||||
n = 800
|
||||
}
|
||||
return fmt.Sprintf("%.0fk", n)
|
||||
}
|
||||
|
||||
var args []string
|
||||
|
||||
if useSourceFormat {
|
||||
|
|
@ -3791,53 +3816,54 @@ func (s *appState) executeSnippetJob(ctx context.Context, job *queue.Job, progre
|
|||
"-t", fmt.Sprintf("%d", snippetLength),
|
||||
}
|
||||
|
||||
// Apply video codec settings
|
||||
// Apply video codec settings with bitrate/CRF caps to avoid runaway bitrates on short clips
|
||||
targetBitrate := clampSnippetBitrate(strings.TrimSpace(conv.VideoBitrate), src.Width)
|
||||
if targetBitrate == "" {
|
||||
targetBitrate = clampSnippetBitrate(defaultBitrate(conv.VideoCodec, src.Width, src.Bitrate), src.Width)
|
||||
}
|
||||
if targetBitrate == "" {
|
||||
targetBitrate = clampSnippetBitrate("3500k", src.Width)
|
||||
}
|
||||
|
||||
preset := conv.EncoderPreset
|
||||
if preset == "" {
|
||||
preset = "medium"
|
||||
}
|
||||
|
||||
crfVal := conv.CRF
|
||||
if crfVal == "" {
|
||||
crfVal = crfForQuality(conv.Quality)
|
||||
if crfVal == "" {
|
||||
crfVal = "23"
|
||||
}
|
||||
}
|
||||
// Disallow lossless for snippets to avoid runaway bitrates
|
||||
if strings.TrimSpace(crfVal) == "0" {
|
||||
crfVal = "18"
|
||||
}
|
||||
|
||||
videoCodec := strings.ToLower(conv.VideoCodec)
|
||||
switch videoCodec {
|
||||
case "h.264", "":
|
||||
args = append(args, "-c:v", "libx264")
|
||||
if conv.EncoderPreset != "" {
|
||||
args = append(args, "-preset", conv.EncoderPreset)
|
||||
} else {
|
||||
args = append(args, "-preset", "medium")
|
||||
}
|
||||
if conv.CRF != "" {
|
||||
args = append(args, "-crf", conv.CRF)
|
||||
} else {
|
||||
args = append(args, "-crf", "23")
|
||||
}
|
||||
args = append(args, "-preset", preset, "-crf", crfVal, "-maxrate", targetBitrate, "-bufsize", targetBitrate)
|
||||
case "h.265":
|
||||
args = append(args, "-c:v", "libx265")
|
||||
if conv.EncoderPreset != "" {
|
||||
args = append(args, "-preset", conv.EncoderPreset)
|
||||
} else {
|
||||
args = append(args, "-preset", "medium")
|
||||
}
|
||||
if conv.CRF != "" {
|
||||
args = append(args, "-crf", conv.CRF)
|
||||
} else {
|
||||
args = append(args, "-crf", "28")
|
||||
}
|
||||
args = append(args, "-preset", preset, "-crf", crfVal, "-maxrate", targetBitrate, "-bufsize", targetBitrate)
|
||||
case "vp9":
|
||||
args = append(args, "-c:v", "libvpx-vp9")
|
||||
if conv.CRF != "" {
|
||||
args = append(args, "-crf", conv.CRF)
|
||||
} else {
|
||||
args = append(args, "-crf", "31")
|
||||
}
|
||||
args = append(args, "-crf", crfVal, "-maxrate", targetBitrate, "-bufsize", targetBitrate)
|
||||
case "av1":
|
||||
args = append(args, "-c:v", "libsvtav1")
|
||||
if conv.CRF != "" {
|
||||
args = append(args, "-crf", conv.CRF)
|
||||
} else {
|
||||
args = append(args, "-crf", "35")
|
||||
}
|
||||
args = append(args, "-crf", crfVal, "-maxrate", targetBitrate, "-bufsize", targetBitrate)
|
||||
case "copy":
|
||||
args = append(args, "-c:v", "copy")
|
||||
default:
|
||||
// Fallback to h264
|
||||
args = append(args, "-c:v", "libx264", "-preset", "medium", "-crf", "23")
|
||||
args = append(args, "-c:v", "libx264", "-preset", preset, "-crf", crfVal, "-maxrate", targetBitrate, "-bufsize", targetBitrate)
|
||||
}
|
||||
// Ensure standard pixel format
|
||||
args = append(args, "-pix_fmt", "yuv420p")
|
||||
|
||||
// Apply audio codec settings
|
||||
audioCodec := strings.ToLower(conv.AudioCodec)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user