Switch to imagemagick, only support videos

This commit is contained in:
Mark Felder 2020-08-25 17:18:22 -05:00
parent 479578b148
commit 899ea2da3e
4 changed files with 14 additions and 20 deletions

View file

@ -444,7 +444,7 @@
enabled: false, enabled: false,
thumbnail_max_width: 600, thumbnail_max_width: 600,
thumbnail_max_height: 600, thumbnail_max_height: 600,
quality: 2, image_quality: 85,
proxy_opts: [ proxy_opts: [
head_request_max_read_duration: 5_000 head_request_max_read_duration: 5_000
] ]

View file

@ -1975,9 +1975,9 @@
description: "Max height of preview thumbnail." description: "Max height of preview thumbnail."
}, },
%{ %{
key: :quality, key: :image_quality,
type: :integer, type: :integer,
description: "Quality of the output. Ranges from 1 (max quality) to 31 (lowest quality)." description: "Quality of the output. Ranges from 0 (min quality) to 100 (max quality)."
}, },
%{ %{
key: :proxy_opts, key: :proxy_opts,

View file

@ -7,18 +7,17 @@ defmodule Pleroma.Helpers.MediaHelper do
Handles common media-related operations. Handles common media-related operations.
""" """
def ffmpeg_resize(uri_or_path, %{max_width: max_width, max_height: max_height} = options) do def image_resize(url, %{max_width: max_width, max_height: max_height} = options) do
quality = options[:quality] || 1 quality = options[:quality] || 85
cmd = ~s""" cmd = ~s"""
ffmpeg -i #{uri_or_path} -f lavfi -i color=c=white \ convert - -resize '#{max_width}x#{max_height}>' -quality #{quality} -
-filter_complex "[0:v] scale='min(#{max_width},iw)':'min(#{max_height},ih)': \
force_original_aspect_ratio=decrease [scaled]; \
[1][scaled] scale2ref [bg][img]; [bg] setsar=1 [bg]; [bg][img] overlay=shortest=1" \
-loglevel quiet -f image2 -vcodec mjpeg -frames:v 1 -q:v #{quality} pipe:1
""" """
pid = Port.open({:spawn, cmd}, [:use_stdio, :in, :stream, :exit_status, :binary]) pid = Port.open({:spawn, cmd}, [:use_stdio, :in, :stream, :exit_status, :binary])
{:ok, env} = url |> Pleroma.Web.MediaProxy.url() |> Pleroma.HTTP.get()
image = env.body
Port.command(pid, image)
loop_recv(pid) loop_recv(pid)
end end

View file

@ -66,25 +66,20 @@ defp handle_preview(conn, url) do
end end
defp handle_preview("image/" <> _ = _content_type, conn, url) do defp handle_preview("image/" <> _ = _content_type, conn, url) do
handle_image_or_video_preview(conn, url) handle_image_preview(conn, url)
end
defp handle_preview("video/" <> _ = _content_type, conn, url) do
handle_image_or_video_preview(conn, url)
end end
defp handle_preview(content_type, conn, _url) do defp handle_preview(content_type, conn, _url) do
send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.") send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
end end
defp handle_image_or_video_preview(%{params: params} = conn, url) do defp handle_image_preview(%{params: params} = conn, url) do
quality = Config.get!([:media_preview_proxy, :quality]) quality = Config.get!([:media_preview_proxy, :image_quality])
with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params), with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),
media_proxy_url <- MediaProxy.url(url),
{:ok, thumbnail_binary} <- {:ok, thumbnail_binary} <-
MediaHelper.ffmpeg_resize( MediaHelper.image_resize(
media_proxy_url, url,
%{max_width: thumbnail_max_width, max_height: thumbnail_max_height, quality: quality} %{max_width: thumbnail_max_width, max_height: thumbnail_max_height, quality: quality}
) do ) do
conn conn