1package fileop
2
3import (
4 "image"
5 "image/jpeg"
6 "os"
7 "os/exec"
8
9 "github.com/disintegration/imaging"
10)
11
12func EncodeImageThumbnail(inputPath string, outputPath string, width, height int) error {
13 inputImage, err := imaging.Open(inputPath, imaging.AutoOrientation(true))
14 if err != nil {
15 return err
16 }
17
18 thumbImage := imaging.Fit(inputImage, width, height, imaging.Lanczos)
19 if err = encodeImageJPEG(thumbImage, outputPath, 60); err != nil {
20 return err
21 }
22
23 return nil
24}
25
26func encodeImageJPEG(image image.Image, outputPath string, jpegQuality int) error {
27 photo_file, err := os.Create(outputPath)
28 if err != nil {
29 return err
30 }
31 defer photo_file.Close()
32
33 err = jpeg.Encode(photo_file, image, &jpeg.Options{Quality: jpegQuality})
34 if err != nil {
35 return err
36 }
37
38 return nil
39}
40
41func EncodeVideoThumbnail(inputPath string, outputPath string, width, height int) error {
42 args := []string{
43 "-i",
44 inputPath,
45 "-vframes", "1", // output one frame
46 "-an", // disable audio
47 "-vf", "scale='min(1024,iw)':'min(1024,ih)':force_original_aspect_ratio=decrease:force_divisible_by=2",
48 "-vf", "select=gte(n\\,100)",
49 outputPath,
50 }
51
52 cmd := exec.Command("ffmpeg", args...)
53
54 if err := cmd.Run(); err != nil {
55 return err
56 }
57
58 return nil
59
60}