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
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user