From 6e84441dab0a2b89869e33d7e89d14189d9b67c0 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sat, 1 Jul 2023 17:55:50 +0200 Subject: feat: Add thumbnailer --- pkg/fileop/file.go | 17 ++++++++++++++ pkg/fileop/thumbnail.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 pkg/fileop/file.go create mode 100644 pkg/fileop/thumbnail.go (limited to 'pkg/fileop') diff --git a/pkg/fileop/file.go b/pkg/fileop/file.go new file mode 100644 index 0000000..07c08e5 --- /dev/null +++ b/pkg/fileop/file.go @@ -0,0 +1,17 @@ +package fileop + +import ( + "crypto/md5" + "encoding/hex" + "strings" +) + +func GetHashFromPath(path string) string { + hash := md5.Sum([]byte(path)) + return hex.EncodeToString(hash[:]) +} + +func IsMimeTypeSupported(mimetype string) bool { + return strings.HasPrefix(mimetype, "video") && + strings.HasPrefix(mimetype, "image") +} diff --git a/pkg/fileop/thumbnail.go b/pkg/fileop/thumbnail.go new file mode 100644 index 0000000..32f6064 --- /dev/null +++ b/pkg/fileop/thumbnail.go @@ -0,0 +1,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 + +} -- cgit v1.2.3