aboutsummaryrefslogtreecommitdiff
path: root/pkg/fileop/thumbnail.go
blob: 32f6064dc93654e14514875551c2c452fbe1d773 (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
56
57
58
59
60
package fileop

import (
	"image"
	"image/jpeg"
	"os"
	"os/exec"

	"github.com/disintegration/imaging"
)

func EncodeImageThumbnail(inputPath string, outputPath string, width, height int) error {
	inputImage, err := imaging.Open(inputPath, imaging.AutoOrientation(true))
	if err != nil {
		return err
	}

	thumbImage := imaging.Fit(inputImage, width, height, imaging.Lanczos)
	if err = encodeImageJPEG(thumbImage, outputPath, 60); err != nil {
		return err
	}

	return nil
}

func encodeImageJPEG(image image.Image, outputPath string, jpegQuality int) error {
	photo_file, err := os.Create(outputPath)
	if err != nil {
		return err
	}
	defer photo_file.Close()

	err = jpeg.Encode(photo_file, image, &jpeg.Options{Quality: jpegQuality})
	if err != nil {
		return err
	}

	return nil
}

func EncodeVideoThumbnail(inputPath string, outputPath string, width, height int) error {
	args := []string{
		"-i",
		inputPath,
		"-vframes", "1", // output one frame
		"-an", // disable audio
		"-vf", "scale='min(1024,iw)':'min(1024,ih)':force_original_aspect_ratio=decrease:force_divisible_by=2",
		"-vf", "select=gte(n\\,100)",
		outputPath,
	}

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

	if err := cmd.Run(); err != nil {
		return err
	}

	return nil

}