feat: add automatic media normalization, R2 retention cleanup, and seamless playout loops

This commit is contained in:
zwitschi committed 2026-08-13 11:59:46 +02:00
1 parent e15d89edfe
commit 06ab5b4655
16 files changed
+1103 -408

No files matched your search

+86 -4
View File
@@ -976,9 +976,9 @@ func HandleAdminMedia(deps *Dependencies) http.HandlerFunc {
}
normResult := media.ValidateNormalization(targetPath)
status := "ready"
status := media.StatusReady
if !normResult.Valid {
status = "needs_normalization"
status = media.StatusNeedsNormalization
}
filename := filepath.Base(targetPath)
@@ -998,6 +998,10 @@ func HandleAdminMedia(deps *Dependencies) http.HandlerFunc {
return
}
if !normResult.Valid {
go normalizeMediaRecord(deps, mediaRecord)
}
files, err := media.ListFiles(deps.DB)
if err != nil {
http.Error(w, fmt.Sprintf("failed to list media files: %v", err), http.StatusInternalServerError)
@@ -1307,9 +1311,9 @@ func HandleAdminMediaUploads(deps *Dependencies) http.HandlerFunc {
}
normResult := media.ValidateNormalization(targetPath)
status := "ready"
status := media.StatusReady
if !normResult.Valid {
status = "needs_normalization"
status = media.StatusNeedsNormalization
}
filename := filepath.Base(targetPath)
@@ -1329,6 +1333,10 @@ func HandleAdminMediaUploads(deps *Dependencies) http.HandlerFunc {
return
}
if !normResult.Valid {
go normalizeMediaRecord(deps, mediaRecord)
}
_ = os.RemoveAll(media.UploadSessionDir(mediaDir, uploadID))
files, err := media.ListFiles(deps.DB)
@@ -1348,6 +1356,11 @@ func HandleAdminMediaUploads(deps *Dependencies) http.HandlerFunc {
func HandleAdminMediaByID(deps *Dependencies) http.HandlerFunc {
mediaDir := config.MediaDir()
return func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/normalize") {
handleNormalizeMedia(deps, w, r)
return
}
mediaID, err := media.GetMediaIDFromPath(r.URL.Path, "/api/admin/media/")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -1424,6 +1437,75 @@ func HandleAdminMediaByID(deps *Dependencies) http.HandlerFunc {
}
}
// normalizeMediaRecord runs ffmpeg normalization for a media record and updates
// its status, path, duration, and size. Safe to run asynchronously.
func normalizeMediaRecord(deps *Dependencies, record db.Media) {
src := record.Path
dst := strings.TrimSuffix(src, filepath.Ext(src)) + ".normalized.mp4"
if err := deps.DB.Model(&record).Update("status", media.StatusNormalizing).Error; err != nil {
return
}
duration, size, err := media.NormalizeVideo(src, dst)
if err != nil {
_ = deps.DB.Model(&record).Updates(map[string]any{
"status": media.StatusNormalizationFailed,
"tags": `"normalization":false`,
}).Error
return
}
_ = deps.DB.Model(&record).Updates(map[string]any{
"path": dst,
"filename": filepath.Base(dst),
"size_bytes": size,
"duration_seconds": duration,
"status": media.StatusReady,
"tags": `"normalization":true`,
}).Error
}
// handleNormalizeMedia handles POST /api/admin/media/{id}/normalize.
func handleNormalizeMedia(deps *Dependencies, w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
idPart := strings.TrimSuffix(r.URL.Path, "/normalize")
idPart = strings.TrimPrefix(idPart, "/api/admin/media/")
idPart = strings.Trim(idPart, "/")
mediaID, err := strconv.ParseUint(idPart, 10, 64)
if err != nil || mediaID == 0 {
http.Error(w, "invalid media id", http.StatusBadRequest)
return
}
var record db.Media
if err := deps.DB.First(&record, mediaID).Error; err != nil {
http.Error(w, "media item not found", http.StatusNotFound)
return
}
if record.Status == media.StatusNormalizing {
http.Error(w, "normalization already in progress", http.StatusConflict)
return
}
record.Status = media.StatusNormalizing
if err := deps.DB.Model(&record).Update("status", media.StatusNormalizing).Error; err != nil {
http.Error(w, fmt.Sprintf("failed to update media status: %v", err), http.StatusInternalServerError)
return
}
go normalizeMediaRecord(deps, record)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(mediaEntryFromRecord(record))
}
// HandleAdminChatLog lists chat log entries.
func HandleAdminChatLog(deps *Dependencies) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
+131
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
@@ -19,6 +20,7 @@ import (
"chat/src/auth"
"chat/src/config"
"chat/src/db"
"chat/src/media"
"chat/src/ws"
)
@@ -944,4 +946,133 @@ func TestHandleAdminMediaUploads_InvalidFile(t *testing.T) {
if w.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400", w.Code)
}
}
func TestNormalizeMediaRecord_Success(t *testing.T) {
deps, gormDB, _ := setupTestDeps(t)
tmpDir := t.TempDir()
os.Setenv("MEDIA_DIR", tmpDir)
defer os.Unsetenv("MEDIA_DIR")
srcPath := filepath.Join(tmpDir, "source.mov")
if err := os.WriteFile(srcPath, []byte("fake video"), 0o644); err != nil {
t.Fatal(err)
}
gormDB.Create(&db.Media{
Filename: "source.mov",
OriginalName: "source.mov",
Path: srcPath,
SizeBytes: 10,
DurationSeconds: 0,
Status: "needs_normalization",
Title: "source",
})
origNormalize := media.NormalizeVideo
t.Cleanup(func() { media.NormalizeVideo = origNormalize })
media.NormalizeVideo = func(src, dst string) (int, int64, error) {
if err := os.WriteFile(dst, []byte("normalized"), 0o644); err != nil {
return 0, 0, err
}
return 120, 10, nil
}
var record db.Media
gormDB.First(&record, 1)
normalizeMediaRecord(deps, record)
var updated db.Media
gormDB.First(&updated, 1)
if updated.Status != "ready" {
t.Errorf("Status = %q, want ready", updated.Status)
}
if updated.DurationSeconds != 120 {
t.Errorf("DurationSeconds = %d, want 120", updated.DurationSeconds)
}
if !strings.HasSuffix(updated.Path, ".normalized.mp4") {
t.Errorf("Path = %q, want .normalized.mp4 suffix", updated.Path)
}
}
func TestNormalizeMediaRecord_Failure(t *testing.T) {
deps, gormDB, _ := setupTestDeps(t)
tmpDir := t.TempDir()
os.Setenv("MEDIA_DIR", tmpDir)
defer os.Unsetenv("MEDIA_DIR")
srcPath := filepath.Join(tmpDir, "source.mov")
if err := os.WriteFile(srcPath, []byte("fake video"), 0o644); err != nil {
t.Fatal(err)
}
gormDB.Create(&db.Media{
Filename: "source.mov",
OriginalName: "source.mov",
Path: srcPath,
SizeBytes: 10,
DurationSeconds: 0,
Status: "needs_normalization",
Title: "source",
})
origNormalize := media.NormalizeVideo
t.Cleanup(func() { media.NormalizeVideo = origNormalize })
media.NormalizeVideo = func(src, dst string) (int, int64, error) {
return 0, 0, fmt.Errorf("ffmpeg failed")
}
var record db.Media
gormDB.First(&record, 1)
normalizeMediaRecord(deps, record)
var updated db.Media
gormDB.First(&updated, 1)
if updated.Status != "normalization_failed" {
t.Errorf("Status = %q, want normalization_failed", updated.Status)
}
}
func TestHandleAdminMediaByID_Normalize(t *testing.T) {
deps, gormDB, _ := setupTestDeps(t)
tmpDir := t.TempDir()
os.Setenv("MEDIA_DIR", tmpDir)
defer os.Unsetenv("MEDIA_DIR")
srcPath := filepath.Join(tmpDir, "source.mov")
if err := os.WriteFile(srcPath, []byte("fake video"), 0o644); err != nil {
t.Fatal(err)
}
gormDB.Create(&db.Media{
Filename: "source.mov",
OriginalName: "source.mov",
Path: srcPath,
SizeBytes: 10,
DurationSeconds: 0,
Status: "needs_normalization",
Title: "source",
})
origNormalize := media.NormalizeVideo
t.Cleanup(func() { media.NormalizeVideo = origNormalize })
media.NormalizeVideo = func(src, dst string) (int, int64, error) {
if err := os.WriteFile(dst, []byte("normalized"), 0o644); err != nil {
return 0, 0, err
}
return 90, 9, nil
}
handler := HandleAdminMediaByID(deps)
req := httptest.NewRequest("POST", "/api/admin/media/1/normalize", nil)
w := httptest.NewRecorder()
handler(w, req)
if w.Code != http.StatusAccepted {
t.Errorf("status = %d, want 202", w.Code)
}
}
+65
View File
@@ -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)
+42
View File
@@ -1,7 +1,10 @@
package media
import (
"context"
"fmt"
"path/filepath"
"strings"
"testing"
)
@@ -201,4 +204,43 @@ func TestUniqueTargetPath(t *testing.T) {
if path != expected {
t.Errorf("UniqueTargetPath() = %q, want %q", path, expected)
}
}
func TestNormalizationArgs(t *testing.T) {
args := normalizationArgs("/in/movie.mov", "/out/movie.normalized.mp4")
joined := strings.Join(args, " ")
for _, want := range []string{
"-map 0:v:0",
"-map 0:a:0?",
"-c:v libx264",
"-r 30",
"-g 60",
"-keyint_min 60",
"-pix_fmt yuv420p",
"-c:a aac",
"-ar 48000",
"-ac 2",
"-avoid_negative_ts make_zero",
"/out/movie.normalized.mp4",
} {
if !strings.Contains(joined, want) {
t.Errorf("normalization args missing %q: %v", want, args)
}
}
}
func TestNormalizeVideo_FFmpegError(t *testing.T) {
origRun := runCommand
t.Cleanup(func() { runCommand = origRun })
runCommand = func(ctx context.Context, name string, args ...string) error {
if name != "ffmpeg" {
t.Errorf("runCommand name = %q, want ffmpeg", name)
}
return fmt.Errorf("simulated ffmpeg failure")
}
if _, _, err := normalizeVideo("/in/a.mov", "/out/a.normalized.mp4"); err == nil {
t.Fatal("expected error when ffmpeg fails")
}
}