From 41b3078da8851d62ab30a584457dbeb69545e98c Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 6 Jul 2020 14:10:35 -0400 Subject: [PATCH] fix apng encoding --- lib/philomena/processors/png.ex | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/philomena/processors/png.ex b/lib/philomena/processors/png.ex index 32d5d240..5a536dca 100644 --- a/lib/philomena/processors/png.ex +++ b/lib/philomena/processors/png.ex @@ -3,10 +3,11 @@ defmodule Philomena.Processors.Png do def process(analysis, file, versions) do dimensions = analysis.dimensions + animated? = analysis.animated? {:ok, intensities} = Intensities.file(file) - scaled = Enum.flat_map(versions, &scale_if_smaller(file, dimensions, &1)) + scaled = Enum.flat_map(versions, &scale_if_smaller(file, animated?, dimensions, &1)) %{ intensities: intensities, @@ -35,13 +36,13 @@ defmodule Philomena.Processors.Png do optimized end - defp scale_if_smaller(_file, _dimensions, {:full, _target_dim}) do + defp scale_if_smaller(_file, _animated?, _dimensions, {:full, _target_dim}) do [{:symlink_original, "full.png"}] end - defp scale_if_smaller(file, {width, height}, {thumb_name, {target_width, target_height}}) do + defp scale_if_smaller(file, animated?, {width, height}, {thumb_name, {target_width, target_height}}) do if width > target_width or height > target_height do - scaled = scale(file, {target_width, target_height}) + scaled = scale(file, animated?, {target_width, target_height}) [{:copy, scaled, "#{thumb_name}.png"}] else @@ -49,16 +50,22 @@ defmodule Philomena.Processors.Png do end end - defp scale(file, {width, height}) do + defp scale(file, animated?, {width, height}) do scaled = Briefly.create!(extname: ".png") scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease,format=rgb32" {_output, 0} = - System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", scale_filter, scaled]) + cond do + animated? -> + System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-plays", "0", "-vf", scale_filter, "-f", "apng", scaled]) - {_output, 0} = System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled]) + true -> + System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", scale_filter, scaled]) + end + + System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled]) scaled end