fix apng encoding

This commit is contained in:
byte[] 2020-07-06 14:10:35 -04:00
parent be10ad4663
commit 41b3078da8

View file

@ -3,10 +3,11 @@ defmodule Philomena.Processors.Png do
def process(analysis, file, versions) do def process(analysis, file, versions) do
dimensions = analysis.dimensions dimensions = analysis.dimensions
animated? = analysis.animated?
{:ok, intensities} = Intensities.file(file) {: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, intensities: intensities,
@ -35,13 +36,13 @@ defmodule Philomena.Processors.Png do
optimized optimized
end 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"}] [{:symlink_original, "full.png"}]
end 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 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"}] [{:copy, scaled, "#{thumb_name}.png"}]
else else
@ -49,16 +50,22 @@ defmodule Philomena.Processors.Png do
end end
end end
defp scale(file, {width, height}) do defp scale(file, animated?, {width, height}) do
scaled = Briefly.create!(extname: ".png") scaled = Briefly.create!(extname: ".png")
scale_filter = scale_filter =
"scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease,format=rgb32" "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease,format=rgb32"
{_output, 0} = {_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 scaled
end end