Skip to content

Commit

Permalink
Refactor FFmpeg options and image resizing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptofyre committed Sep 17, 2024
1 parent 262ff15 commit e85793e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,13 @@ func generateArtworkAsync(urlStr, key, gifPath string) error {

err := ffmpeg.Input(urlStr).
Output(tempGifPath, ffmpeg.KwArgs{
"vf": "fps=15,scale=500:-1:flags=lanczos",
"threads": "8",
"preset": "fast",
"multiple_requests": "1",
"buffer_size": "8192k",
"loglevel": "error", // Only log errors
"protocol_whitelist": "file,http,https,tcp,tls,crypto",
"vf": "fps=15,scale=500:-1:flags=lanczos",
"threads": "8",
"preset": "fast",
"multiple_requests": "1",
"buffer_size": "8192k",
"loglevel": "error", // Only log errors
}).
GlobalArgs("-hide_banner"). // Hide the FFmpeg banner
OverWriteOutput().
Expand Down
43 changes: 34 additions & 9 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,44 @@ func createArtistSquare(images []image.Image) (image.Image, error) {
size := 500
background := image.NewRGBA(image.Rect(0, 0, size, size))

resizeAndDraw := func(img image.Image, rect image.Rectangle) {
// Calculate aspect ratio
srcAspect := float64(img.Bounds().Dx()) / float64(img.Bounds().Dy())
dstAspect := float64(rect.Dx()) / float64(rect.Dy())

var resizedImg image.Image
if srcAspect > dstAspect {
// Image is wider, resize based on height
newHeight := uint(rect.Dy())
newWidth := uint(float64(newHeight) * srcAspect)
resizedImg = resize.Resize(newWidth, newHeight, img, resize.Lanczos3)
} else {
// Image is taller, resize based on width
newWidth := uint(rect.Dx())
newHeight := uint(float64(newWidth) / srcAspect)
resizedImg = resize.Resize(newWidth, newHeight, img, resize.Lanczos3)
}

// Calculate positioning to center the image
srcBounds := resizedImg.Bounds()
dx := (srcBounds.Dx() - rect.Dx()) / 2
dy := (srcBounds.Dy() - rect.Dy()) / 2
draw.Draw(background, rect, resizedImg, image.Point{dx, dy}, draw.Src)
}

switch len(images) {
case 2:
draw.Draw(background, image.Rect(0, 0, size/2, size), resize.Resize(uint(size/2), uint(size), images[0], resize.Lanczos3), image.Point{}, draw.Src)
draw.Draw(background, image.Rect(size/2, 0, size, size), resize.Resize(uint(size/2), uint(size), images[1], resize.Lanczos3), image.Point{}, draw.Src)
resizeAndDraw(images[0], image.Rect(0, 0, size/2, size))
resizeAndDraw(images[1], image.Rect(size/2, 0, size, size))
case 3:
draw.Draw(background, image.Rect(0, 0, size, size/2), resize.Resize(uint(size), uint(size/2), images[0], resize.Lanczos3), image.Point{}, draw.Src)
draw.Draw(background, image.Rect(0, size/2, size/2, size), resize.Resize(uint(size/2), uint(size/2), images[1], resize.Lanczos3), image.Point{}, draw.Src)
draw.Draw(background, image.Rect(size/2, size/2, size, size), resize.Resize(uint(size/2), uint(size/2), images[2], resize.Lanczos3), image.Point{}, draw.Src)
resizeAndDraw(images[0], image.Rect(0, 0, size, size/2))
resizeAndDraw(images[1], image.Rect(0, size/2, size/2, size))
resizeAndDraw(images[2], image.Rect(size/2, size/2, size, size))
case 4:
draw.Draw(background, image.Rect(0, 0, size/2, size/2), resize.Resize(uint(size/2), uint(size/2), images[0], resize.Lanczos3), image.Point{}, draw.Src)
draw.Draw(background, image.Rect(size/2, 0, size, size/2), resize.Resize(uint(size/2), uint(size/2), images[1], resize.Lanczos3), image.Point{}, draw.Src)
draw.Draw(background, image.Rect(0, size/2, size/2, size), resize.Resize(uint(size/2), uint(size/2), images[2], resize.Lanczos3), image.Point{}, draw.Src)
draw.Draw(background, image.Rect(size/2, size/2, size, size), resize.Resize(uint(size/2), uint(size/2), images[3], resize.Lanczos3), image.Point{}, draw.Src)
resizeAndDraw(images[0], image.Rect(0, 0, size/2, size/2))
resizeAndDraw(images[1], image.Rect(size/2, 0, size, size/2))
resizeAndDraw(images[2], image.Rect(0, size/2, size/2, size))
resizeAndDraw(images[3], image.Rect(size/2, size/2, size, size))
default:
return nil, fmt.Errorf("unsupported number of images: %d", len(images))
}
Expand Down

0 comments on commit e85793e

Please sign in to comment.