aboutsummaryrefslogtreecommitdiff
path: root/pkg/fileop/thumbnail.go
blob: fcdfa12cbc6ae2668eeab49363456c0ecf7be84f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package fileop

import (
	"bytes"
	"fmt"
	"os/exec"
	"strconv"

	"github.com/h2non/bimg"
)

func EncodeImageThumbnail(inputPath string, outputPath string, width, height int) error {
	buffer, err := bimg.Read(inputPath)
	if err != nil {
		return err
	}

	options := bimg.Options{
		Width:         width,
		Height:        height,
		Embed:         true,
		Type:          bimg.JPEG,
		StripMetadata: true,
	}

	newImage, err := bimg.NewImage(buffer).Process(options)
	if err != nil {
		return err
	}

	return bimg.Write(outputPath, newImage)
}

func EncodeVideoThumbnail(inputPath string, outputPath string, width, _ int) error {
	args := []string{
		"-i",
		inputPath,
		"-y",
		"-vframes", "1",
		"-q:v", "1",
		"-vf", "thumbnail,scale=" + strconv.Itoa(width) + ":-1",
		outputPath,
	}

	cmd := exec.Command("ffmpeg", args...)

	var b bytes.Buffer
	cmd.Stderr = &b

	if err := cmd.Run(); err != nil {
		return fmt.Errorf("%s; %w", b.String(), err)
	}

	return nil
}