|
| 1 | +package render |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "image/png" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "github.com/hytale-tools/blockymodel-merger/pkg/texture" |
| 12 | +) |
| 13 | + |
| 14 | +// RenderMP4 renders a GLB model to an MP4 video rotating 360 degrees |
| 15 | +func RenderMP4(glbBytes []byte, atlas *texture.Atlas, background string, frames, width, height, fps int) ([]byte, error) { |
| 16 | + // Parse background color |
| 17 | + bgColor, err := ParseHexColor(background) |
| 18 | + if err != nil { |
| 19 | + return nil, fmt.Errorf("invalid background color: %w", err) |
| 20 | + } |
| 21 | + |
| 22 | + // Get atlas image |
| 23 | + var atlasImage = atlas.Image |
| 24 | + |
| 25 | + // Convert GLB to mesh |
| 26 | + mesh, err := GLBToMesh(glbBytes, atlasImage) |
| 27 | + if err != nil { |
| 28 | + return nil, fmt.Errorf("converting GLB to mesh: %w", err) |
| 29 | + } |
| 30 | + |
| 31 | + // Create temp directory for frames |
| 32 | + tempDir, err := os.MkdirTemp("", "blockyserver-mp4-*") |
| 33 | + if err != nil { |
| 34 | + return nil, fmt.Errorf("creating temp directory: %w", err) |
| 35 | + } |
| 36 | + defer os.RemoveAll(tempDir) |
| 37 | + |
| 38 | + // Calculate rotation per frame |
| 39 | + rotationPerFrame := 360.0 / float64(frames) |
| 40 | + |
| 41 | + // Render all frames in parallel |
| 42 | + var wg sync.WaitGroup |
| 43 | + errChan := make(chan error, frames) |
| 44 | + |
| 45 | + for i := 0; i < frames; i++ { |
| 46 | + wg.Add(1) |
| 47 | + go func(frameIdx int) { |
| 48 | + defer wg.Done() |
| 49 | + rotation := float64(frameIdx) * rotationPerFrame |
| 50 | + img := RenderScene(mesh, atlasImage, rotation, width, height, bgColor) |
| 51 | + |
| 52 | + // Write frame to temp file |
| 53 | + framePath := filepath.Join(tempDir, fmt.Sprintf("frame_%04d.png", frameIdx)) |
| 54 | + f, err := os.Create(framePath) |
| 55 | + if err != nil { |
| 56 | + errChan <- fmt.Errorf("creating frame file: %w", err) |
| 57 | + return |
| 58 | + } |
| 59 | + defer f.Close() |
| 60 | + |
| 61 | + if err := png.Encode(f, img); err != nil { |
| 62 | + errChan <- fmt.Errorf("encoding frame PNG: %w", err) |
| 63 | + return |
| 64 | + } |
| 65 | + }(i) |
| 66 | + } |
| 67 | + wg.Wait() |
| 68 | + close(errChan) |
| 69 | + |
| 70 | + // Check for errors |
| 71 | + for err := range errChan { |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Run FFmpeg to encode MP4 |
| 78 | + outputPath := filepath.Join(tempDir, "output.mp4") |
| 79 | + inputPattern := filepath.Join(tempDir, "frame_%04d.png") |
| 80 | + |
| 81 | + cmd := exec.Command("ffmpeg", |
| 82 | + "-y", |
| 83 | + "-framerate", fmt.Sprintf("%d", fps), |
| 84 | + "-i", inputPattern, |
| 85 | + "-c:v", "libx264", |
| 86 | + "-pix_fmt", "yuv420p", |
| 87 | + "-movflags", "+faststart", |
| 88 | + outputPath, |
| 89 | + ) |
| 90 | + |
| 91 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 92 | + return nil, fmt.Errorf("ffmpeg encoding failed: %w\nOutput: %s", err, string(output)) |
| 93 | + } |
| 94 | + |
| 95 | + // Read output file |
| 96 | + mp4Bytes, err := os.ReadFile(outputPath) |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("reading output MP4: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + return mp4Bytes, nil |
| 102 | +} |
0 commit comments