Files
flatrender/services/node-agent/internal/runner/runner.go
T
soroush.asadi 1ff6e494c0
Build backend images / build content-svc (push) Failing after 19s
Build backend images / build file-svc (push) Failing after 1m53s
Build backend images / build gateway (push) Failing after 16s
Build backend images / build identity-svc (push) Failing after 7m1s
Build backend images / build notification-svc (push) Failing after 7m24s
Build backend images / build render-svc (push) Failing after 3m12s
Build backend images / build studio-svc (push) Failing after 43s
@
feat: AE template scanner + scene editor + AEP bundle pipeline

Scene editor (admin): per-project Scenes / Shared Colors / Color Presets
manager (ProjectScenes) reachable from each project.

AEP bundle pipeline: upload .aep or .zip → stored once per template at
templates/{project_id}/(bundle.zip|template.aep); render claim probes and
returns is_bundle+md5; node-agent extracts the bundle, locates the .aep
(zip-slip guarded), and caches by md5 so repeated renders extract once.

AE template scanner ("read scenes/colours/configs from the AEP"):
- content-svc importer: POST /v1/projects/{id}/scan/{preview,apply} —
  review-diff-then-merge into scenes/elements/colours (manual edits kept).
- render-svc Go quick-scan: stdlib RIFX parser extracts comp names+durations
  (no AE) → POST /v1/template-scans/{id}/quick.
- render-svc AE scan jobs + node-agent runner: queue → node runs scan.jsx
  (reverse of legacy JSXGenerator conventions: frfinal/frshare/frl_/frd_) →
  posts ScanResult back. Migration 26_render_scan_jobs.
- admin UI: "اسکن از افترافکت" with quick/full engines + diff-review modal.

Verified: importer preview/apply, Go quick-scan end-to-end (synthetic .aep →
scene imported), bundle extract unit tests, RIFX parser unit tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@
2026-06-04 10:39:45 +03:30

165 lines
5.3 KiB
Go

// Package runner executes After Effects render jobs and streams progress back
// via the provided callbacks. When AE_PATH is empty, a mock render is used
// (useful for CI and dev environments without a licensed AE installation).
package runner
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
)
// ProgressFn is called periodically during rendering with (percent 0-100, message).
type ProgressFn func(ctx context.Context, percent int, message string) error
// PreviewFn is called each time a new preview frame is ready.
// The argument is a base64-encoded PNG. Errors are non-fatal.
type PreviewFn func(ctx context.Context, imageB64 string) error
// Job holds the parameters for a single render.
type Job struct {
JobID string
SavedProjectID string
Quality string
Resolution string
FrameRate int
HasMusic bool
HasVoiceover bool
// AEPFilePath is the local path to the downloaded .aep project file.
// In a full implementation the agent downloads this from MinIO before calling Run.
AEPFilePath string
}
// Run executes the render job, calling onProgress and onPreview as it advances.
// Returns the path to the output MP4 file on success.
func Run(ctx context.Context, aePath, workDir string, job *Job, onProgress ProgressFn, onPreview PreviewFn) (string, error) {
outputDir := filepath.Join(workDir, "renders", job.JobID)
if err := os.MkdirAll(outputDir, 0o755); err != nil {
return "", fmt.Errorf("create output dir: %w", err)
}
outputPath := filepath.Join(outputDir, "output.mp4")
if aePath == "" {
return mockRender(ctx, job, outputPath, onProgress, onPreview)
}
return aeRender(ctx, aePath, job, outputPath, onProgress, onPreview)
}
// ── Mock render (no AE installed) ────────────────────────────────────────────
func mockRender(ctx context.Context, job *Job, outputPath string, onProgress ProgressFn, onPreview PreviewFn) (string, error) {
log.Printf("[mock] starting render for job %s (%s %s %dfps)", job.JobID, job.Quality, job.Resolution, job.FrameRate)
steps := []struct {
pct int
msg string
}{
{5, "Preparing project…"},
{15, "Loading template…"},
{30, "Rendering frames…"},
{50, "Rendering frames… (50%)"},
{70, "Rendering frames… (70%)"},
{85, "Encoding MP4…"},
{95, "Uploading output…"},
}
for _, s := range steps {
select {
case <-ctx.Done():
return "", ctx.Err()
case <-time.After(800 * time.Millisecond):
}
if err := onProgress(ctx, s.pct, s.msg); err != nil {
log.Printf("[mock] progress callback error: %v", err)
}
// Generate and push a preview frame at each step
if onPreview != nil {
b64 := GeneratePreviewB64(s.pct, job.Quality, job.Resolution)
if err := onPreview(ctx, b64); err != nil {
log.Printf("[mock] preview callback error: %v", err)
}
}
log.Printf("[mock] %d%% — %s", s.pct, s.msg)
}
// Write a placeholder file so the path is valid
if err := os.WriteFile(outputPath, []byte("mock-render-output"), 0o644); err != nil {
return "", fmt.Errorf("write mock output: %w", err)
}
log.Printf("[mock] render complete: %s", outputPath)
return outputPath, nil
}
// ── Real AE render via aerender.exe ──────────────────────────────────────────
func aeRender(ctx context.Context, aePath string, job *Job, outputPath string, onProgress ProgressFn, onPreview PreviewFn) (string, error) {
if job.AEPFilePath == "" {
return "", fmt.Errorf("AEPFilePath is required for real AE render")
}
// aerender flags:
// -project <path.aep>
// -output <output.mp4>
args := []string{
"-project", job.AEPFilePath,
"-output", outputPath,
}
log.Printf("[ae] running: %s %v", aePath, args)
cmd := exec.CommandContext(ctx, aePath, args...)
// Run from the project's folder so a .zip bundle's relative footage/font paths
// resolve correctly (the .aep sits alongside its assets after extraction).
cmd.Dir = filepath.Dir(job.AEPFilePath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("start aerender: %w", err)
}
// Poll process while alive — aerender does not expose machine-readable progress.
// We advance the progress indicator every 10 seconds until the process exits.
done := make(chan error, 1)
go func() { done <- cmd.Wait() }()
_ = onProgress(ctx, 10, "After Effects starting…")
pct := 10
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
// Generate preview frames every 30 seconds during real AE render.
// In a full implementation this would screenshot the AE composition output.
previewTicker := time.NewTicker(30 * time.Second)
defer previewTicker.Stop()
for {
select {
case err := <-done:
if err != nil {
return "", fmt.Errorf("aerender exit: %w", err)
}
_ = onProgress(ctx, 95, "Encoding complete")
return outputPath, nil
case <-ticker.C:
if pct < 90 {
pct += 5
}
_ = onProgress(ctx, pct, fmt.Sprintf("Rendering… %d%%", pct))
case <-previewTicker.C:
if onPreview != nil {
b64 := GeneratePreviewB64(pct, job.Quality, job.Resolution)
if err := onPreview(ctx, b64); err != nil {
log.Printf("[ae] preview push error: %v", err)
}
}
case <-ctx.Done():
_ = cmd.Process.Kill()
return "", ctx.Err()
}
}
}