1package fileop
2
3import (
4 "bytes"
5 "fmt"
6 "os/exec"
7 "strconv"
8
9 "github.com/h2non/bimg"
10)
11
12func EncodeImageThumbnail(inputPath string, outputPath string, width, height int) error {
13 buffer, err := bimg.Read(inputPath)
14 if err != nil {
15 return err
16 }
17
18 options := bimg.Options{
19 Width: width,
20 Height: height,
21 Embed: true,
22 Type: bimg.JPEG,
23 StripMetadata: true,
24 }
25
26 newImage, err := bimg.NewImage(buffer).Process(options)
27 if err != nil {
28 return err
29 }
30
31 return bimg.Write(outputPath, newImage)
32}
33
34func EncodeVideoThumbnail(inputPath string, outputPath string, width, _ int) error {
35 args := []string{
36 "-i",
37 inputPath,
38 "-y",
39 "-vframes", "1",
40 "-q:v", "1",
41 "-vf", "thumbnail,scale=" + strconv.Itoa(width) + ":-1",
42 outputPath,
43 }
44
45 cmd := exec.Command("ffmpeg", args...)
46
47 var b bytes.Buffer
48 cmd.Stderr = &b
49
50 if err := cmd.Run(); err != nil {
51 return fmt.Errorf("%s; %w", b.String(), err)
52 }
53
54 return nil
55}