avif encoding speed, disable AVIF by default

This commit is contained in:
BennyThink 2021-12-06 21:44:33 +08:00
parent 5e033a8f72
commit 7f1c441b6f
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
3 changed files with 19 additions and 13 deletions

View File

@ -11,6 +11,7 @@ type Config struct {
Quality float32 `json:"QUALITY,string"` Quality float32 `json:"QUALITY,string"`
AllowedTypes []string `json:"ALLOWED_TYPES"` AllowedTypes []string `json:"ALLOWED_TYPES"`
ExhaustPath string `json:"EXHAUST_PATH"` ExhaustPath string `json:"EXHAUST_PATH"`
EnableAVIF bool `json:"ENABLE_AVIF"`
} }
var ( var (
@ -33,7 +34,8 @@ const (
"QUALITY": "80", "QUALITY": "80",
"IMG_PATH": "./pics", "IMG_PATH": "./pics",
"EXHAUST_PATH": "./exhaust", "EXHAUST_PATH": "./exhaust",
"ALLOWED_TYPES": ["jpg","png","jpeg","bmp"] "ALLOWED_TYPES": ["jpg","png","jpeg","bmp"],
"ENABLE_AVIF": false
}` }`
sampleSystemd = ` sampleSystemd = `

View File

@ -4,6 +4,10 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"github.com/Kagami/go-avif"
"github.com/chai2010/webp"
log "github.com/sirupsen/logrus"
"golang.org/x/image/bmp"
"image" "image"
"image/jpeg" "image/jpeg"
"image/png" "image/png"
@ -11,19 +15,14 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
log "github.com/sirupsen/logrus"
"github.com/Kagami/go-avif"
"github.com/chai2010/webp"
"golang.org/x/image/bmp"
) )
func convertFilter(raw, avifPath, webpPath string, c chan int) { func convertFilter(raw, avifPath, webpPath string, c chan int) {
// all absolute paths // all absolute paths
if !imageExists(avifPath) { if !imageExists(avifPath) && config.EnableAVIF {
convertImage(raw, avifPath, "avif") convertImage(raw, avifPath, "avif")
} }
@ -109,10 +108,15 @@ func avifEncoder(p1, p2 string, quality float32) {
return return
} }
var avifQuality = int((100 - quality) / 100 * avif.MaxQuality) err = avif.Encode(dst, img, &avif.Options{
err = avif.Encode(dst, img, &avif.Options{Quality: avifQuality}) Threads: runtime.NumCPU(),
Speed: avif.MaxSpeed,
Quality: int((100 - quality) / 100 * avif.MaxQuality),
SubsampleRatio: nil,
})
if err != nil { if err != nil {
log.Fatalf("Can't encode source image: %v to AVIF", err) log.Warnf("Can't encode source image: %v to AVIF", err)
} }
convertLog("AVIF", p1, p2, quality) convertLog("AVIF", p1, p2, quality)
@ -130,7 +134,7 @@ func webpEncoder(p1, p2 string, quality float32) {
err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality}) err = webp.Encode(&buf, img, &webp.Options{Lossless: false, Quality: quality})
if err != nil { if err != nil {
log.Fatalf("Can't encode source image: %v to WebP", err) log.Warnf("Can't encode source image: %v to WebP", err)
} }
if err := ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil { if err := ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil {

View File

@ -15,7 +15,7 @@ func TestPrefetchImages(t *testing.T) {
_ = os.Mkdir(fp, 0755) _ = os.Mkdir(fp, 0755)
prefetchImages("./pics/dir1/", "./prefetch") prefetchImages("./pics/dir1/", "./prefetch")
count := fileCount("./prefetch") count := fileCount("./prefetch")
assert.Equal(t, int64(2), count) assert.Equal(t, int64(1), count)
_ = os.RemoveAll(fp) _ = os.RemoveAll(fp)
} }