feat: add automatic media normalization, R2 retention cleanup, and seamless playout loops
This commit is contained in:
1 parent
e15d89edfe
commit
06ab5b4655
16 files changed
+1103
-408
No files matched your search
@@ -155,6 +155,71 @@ func ProbeDurationSeconds(mediaPath string) (int, error) {
|
||||
return seconds, nil
|
||||
}
|
||||
|
||||
// Media status values (ADR-006 media library states).
|
||||
const (
|
||||
StatusReady = "ready"
|
||||
StatusNeedsNormalization = "needs_normalization"
|
||||
StatusNormalizing = "normalizing"
|
||||
StatusNormalizationFailed = "normalization_failed"
|
||||
)
|
||||
|
||||
// runCommand executes an external command and returns its combined output on failure.
|
||||
// It is a package variable so tests can substitute a stub.
|
||||
var runCommand = func(ctx context.Context, name string, args ...string) error {
|
||||
out, err := exec.CommandContext(ctx, name, args...).CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s failed: %v (%s)", name, err, strings.TrimSpace(string(out)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// normalizationArgs builds the ffmpeg arguments for the Linear Void normalization profile.
|
||||
func normalizationArgs(srcPath, dstPath string) []string {
|
||||
return []string{
|
||||
"-y",
|
||||
"-i", srcPath,
|
||||
"-map", "0:v:0", "-map", "0:a:0?",
|
||||
"-vf", "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,fps=30,format=yuv420p",
|
||||
"-c:v", "libx264", "-preset", "medium", "-crf", "21",
|
||||
"-pix_fmt", "yuv420p",
|
||||
"-r", "30", "-g", "60", "-keyint_min", "60", "-sc_threshold", "0",
|
||||
"-c:a", "aac", "-b:a", "160k", "-ar", "48000", "-ac", "2",
|
||||
"-avoid_negative_ts", "make_zero", "-max_muxing_queue_size", "1024",
|
||||
"-movflags", "+faststart",
|
||||
dstPath,
|
||||
}
|
||||
}
|
||||
|
||||
// normalizeVideo transcodes srcPath to the Linear Void normalization profile,
|
||||
// re-verifies the output with ffprobe, and returns its duration (seconds) and size (bytes).
|
||||
func normalizeVideo(srcPath, dstPath string) (int, int64, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Hour)
|
||||
defer cancel()
|
||||
|
||||
if err := runCommand(ctx, "ffmpeg", normalizationArgs(srcPath, dstPath)...); err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
if res := ValidateNormalization(dstPath); !res.Valid {
|
||||
return 0, 0, fmt.Errorf("normalized output failed validation: %s", strings.Join(res.Errors, "; "))
|
||||
}
|
||||
|
||||
duration, err := ProbeDurationSeconds(dstPath)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("probe duration failed: %w", err)
|
||||
}
|
||||
|
||||
info, err := os.Stat(dstPath)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("stat output failed: %w", err)
|
||||
}
|
||||
|
||||
return duration, info.Size(), nil
|
||||
}
|
||||
|
||||
// NormalizeVideo is the injectable normalization entry point.
|
||||
var NormalizeVideo = normalizeVideo
|
||||
|
||||
// DetectMime reads the first 512 bytes of a file to detect its MIME type.
|
||||
func DetectMime(filePath string) (string, error) {
|
||||
f, err := os.Open(filePath)
|
||||
|
||||
Reference in new issue
Block a user