| 1 |
// Copyright (c) 2024 Sumner Evans |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package lottie |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"os" |
| 14 |
"os/exec" |
| 15 |
"path/filepath" |
| 16 |
"strconv" |
| 17 |
|
| 18 |
"github.com/rs/zerolog" |
| 19 |
|
| 20 |
"go.mau.fi/util/exzerolog" |
| 21 |
"go.mau.fi/util/ffmpeg" |
| 22 |
) |
| 23 |
|
| 24 |
var lottieconverterPath string |
| 25 |
|
| 26 |
func init() { |
| 27 |
lottieconverterPath, _ = exec.LookPath("lottieconverter") |
| 28 |
} |
| 29 |
|
| 30 |
// Supported returns whether lottieconverter is available on the system. |
| 31 |
// |
| 32 |
// lottieconverter is considered to be available if a binary called |
| 33 |
// lottieconverter is found in $PATH, or if [SetPath] has been called |
| 34 |
// explicitly with a non-empty path. |
| 35 |
func Supported() bool { |
| 36 |
return lottieconverterPath != "" |
| 37 |
} |
| 38 |
|
| 39 |
// SetPath overrides the path to the lottieconverter binary. |
| 40 |
func SetPath(path string) { |
| 41 |
lottieconverterPath = path |
| 42 |
} |
| 43 |
|
| 44 |
// Convert converts lottie data an image or image(s) using lottieconverter. |
| 45 |
// |
| 46 |
// Args: |
| 47 |
// - input: an io.Reader containing the lottie data to convert. |
| 48 |
// - outputFilename: the filename to write the output to. |
| 49 |
// - outputWriter: an io.Writer to write the output to. |
| 50 |
// - format: the output format. Can be one of: png, gif, or pngs. |
| 51 |
// - width: the width of the output image(s). |
| 52 |
// - height: the height of the output image(s). |
| 53 |
// - extraArgs: additional arguments to pass to lottieconverter. |
| 54 |
// |
| 55 |
// The outputFilename and outputWriter parameters are mutually exclusive. |
| 56 |
func Convert(ctx context.Context, input io.Reader, outputFilename string, outputWriter io.Writer, format string, width, height int, extraArgs ...string) error { |
| 57 |
// Verify the input parameters and calculate the actual outputFilename that |
| 58 |
// will be used when shelling out to lottieconverter. |
| 59 |
// |
| 60 |
// We are panicking here because it's a programming error to call this |
| 61 |
// function with invalid parameters. |
| 62 |
if outputFilename == "" && outputWriter == nil { |
| 63 |
panic("lottie.Convert: either outputFile or outputWriter must be provided") |
| 64 |
} else if outputWriter != nil { |
| 65 |
if outputFilename != "" { |
| 66 |
panic("lottie.Convert: only one of outputFile or outputWriter can be provided") |
| 67 |
} |
| 68 |
outputFilename = "-" |
| 69 |
} |
| 70 |
|
| 71 |
args := []string{"-", outputFilename, format, fmt.Sprintf("%dx%d", width, height)} |
| 72 |
args = append(args, extraArgs...) |
| 73 |
|
| 74 |
cmd := exec.CommandContext(ctx, lottieconverterPath, args...) |
| 75 |
cmd.Stdin = input |
| 76 |
cmd.Stdout = outputWriter |
| 77 |
log := zerolog.Ctx(ctx).With().Str("command", "lottieconverter").Logger() |
| 78 |
cmd.Stderr = exzerolog.NewLogWriter(log).WithLevel(zerolog.WarnLevel) |
| 79 |
|
| 80 |
if err := cmd.Run(); err != nil { |
| 81 |
return fmt.Errorf("lottieconverter error: %w", err) |
| 82 |
} |
| 83 |
return nil |
| 84 |
} |
| 85 |
|
| 86 |
// FFmpegConvert converts lottie data to a video or image using ffmpeg. |
| 87 |
// |
| 88 |
// This function should only be called if [ffmpeg.Supported] returns true. |
| 89 |
// |
| 90 |
// Args: |
| 91 |
// - input: an io.Reader containing the lottie data to convert. |
| 92 |
// - outputFile: the filename to write the output to. Must have .webp or .webm extension. |
| 93 |
// - width: the width of the output video or image. |
| 94 |
// - height: the height of the output video or image. |
| 95 |
// - fps: the framerate of the output video. |
| 96 |
// |
| 97 |
// Returns: the converted data as a *bytes.Buffer, the mimetype of the output, |
| 98 |
// and the thumbnail data as a PNG. |
| 99 |
func FFmpegConvert(ctx context.Context, input io.Reader, outputFile string, width, height, fps int) (thumbnailData []byte, err error) { |
| 100 |
if !ffmpeg.Supported() { |
| 101 |
return nil, fmt.Errorf("ffmpeg is not available") |
| 102 |
} |
| 103 |
|
| 104 |
tmpDir, err := os.MkdirTemp("", "lottieconvert") |
| 105 |
if err != nil { |
| 106 |
return |
| 107 |
} |
| 108 |
defer os.RemoveAll(tmpDir) |
| 109 |
|
| 110 |
err = Convert(ctx, input, tmpDir+"/out_", nil, "pngs", width, height, strconv.Itoa(fps)) |
| 111 |
if err != nil { |
| 112 |
return |
| 113 |
} |
| 114 |
|
| 115 |
files, err := os.ReadDir(tmpDir) |
| 116 |
if err != nil { |
| 117 |
return |
| 118 |
} |
| 119 |
|
| 120 |
var firstFrameName string |
| 121 |
for _, file := range files { |
| 122 |
if firstFrameName == "" || file.Name() < firstFrameName { |
| 123 |
firstFrameName = file.Name() |
| 124 |
} |
| 125 |
} |
| 126 |
thumbnailData, err = os.ReadFile(fmt.Sprintf("%s/%s", tmpDir, firstFrameName)) |
| 127 |
if err != nil { |
| 128 |
return |
| 129 |
} |
| 130 |
|
| 131 |
var outputArgs []string |
| 132 |
switch filepath.Ext(outputFile) { |
| 133 |
case ".webm": |
| 134 |
outputArgs = []string{"-c:v", "libvpx-vp9", "-pix_fmt", "yuva420p", "-f", "webm"} |
| 135 |
case ".webp": |
| 136 |
outputArgs = []string{"-c:v", "libwebp_anim", "-pix_fmt", "yuva420p", "-f", "webp", "-loop", "0"} |
| 137 |
default: |
| 138 |
err = fmt.Errorf("unsupported extension %s", filepath.Ext(outputFile)) |
| 139 |
return |
| 140 |
} |
| 141 |
err = ffmpeg.ConvertPathWithDestination( |
| 142 |
ctx, |
| 143 |
tmpDir+"/out_*.png", |
| 144 |
outputFile, |
| 145 |
[]string{"-framerate", strconv.Itoa(fps), "-pattern_type", "glob"}, |
| 146 |
outputArgs, |
| 147 |
false, |
| 148 |
) |
| 149 |
return |
| 150 |
} |