main
Raw Download raw file
 1package pipeline
 2
 3import (
 4	"log/slog"
 5)
 6
 7// SpeedConfig holds parameters for speed calculation.
 8type SpeedConfig struct {
 9	TargetSecs   int     // Target output duration (default 600 = 10 min)
10	MinSpeedMult float64 // Minimum speed multiplier (default 1.0)
11	MaxSpeedMult float64 // Maximum speed multiplier (default 2000.0)
12}
13
14// DefaultSpeedConfig returns default speed configuration.
15func DefaultSpeedConfig() SpeedConfig {
16	return SpeedConfig{
17		TargetSecs:   600,
18		MinSpeedMult: 1.0,
19		MaxSpeedMult: 2000.0,
20	}
21}
22
23// CalculateSpeed computes the speed factor to achieve target duration.
24// Returns the clamped speed factor and expected output duration.
25func CalculateSpeed(totalDurationSecs float64, cfg SpeedConfig) (speedFactor float64, outputDuration float64) {
26	if totalDurationSecs <= 0 {
27		return cfg.MinSpeedMult, 0
28	}
29
30	targetDuration := float64(cfg.TargetSecs)
31
32	// speed = total / target
33	speedFactor = totalDurationSecs / targetDuration
34
35	// Clamp to allowed range
36	if speedFactor < cfg.MinSpeedMult {
37		speedFactor = cfg.MinSpeedMult
38	}
39	if speedFactor > cfg.MaxSpeedMult {
40		speedFactor = cfg.MaxSpeedMult
41	}
42
43	// Calculate actual output duration with clamped speed
44	outputDuration = totalDurationSecs / speedFactor
45
46	slog.Info("speed calculated",
47		slog.Float64("total_duration_secs", totalDurationSecs),
48		slog.Int("target_secs", cfg.TargetSecs),
49		slog.Float64("speed_factor", speedFactor),
50		slog.Float64("output_duration_secs", outputDuration))
51
52	return speedFactor, outputDuration
53}