Switch to imagemagick, only support videos
authorMark Felder <feld@FreeBSD.org>
Tue, 25 Aug 2020 22:18:22 +0000 (17:18 -0500)
committerMark Felder <feld@FreeBSD.org>
Tue, 25 Aug 2020 22:18:22 +0000 (17:18 -0500)
config/config.exs
config/description.exs
lib/pleroma/helpers/media_helper.ex
lib/pleroma/web/media_proxy/media_proxy_controller.ex

index e1558e29ef562d6e64820e5d0c67e3f040d05ce7..972b96d2d6cf5fe22618a66f7690f96fc45c9093 100644 (file)
@@ -444,7 +444,7 @@ config :pleroma, :media_preview_proxy,
   enabled: false,
   thumbnail_max_width: 600,
   thumbnail_max_height: 600,
-  quality: 2,
+  image_quality: 85,
   proxy_opts: [
     head_request_max_read_duration: 5_000
   ]
index 0082cc84f8d349cd4c44a06bdf70cb40a829929c..60f76be45ffb294b560af201556e0d67d643fc7e 100644 (file)
@@ -1975,9 +1975,9 @@ config :pleroma, :config_description, [
         description: "Max height of preview thumbnail."
       },
       %{
-        key: :quality,
+        key: :image_quality,
         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,
index 89dd4204bdd6e7c9f508c03f111472a69cf6c5e6..07e6dba5ee7d614ccd606a430ed656c2e85e362c 100644 (file)
@@ -7,18 +7,17 @@ defmodule Pleroma.Helpers.MediaHelper do
   Handles common media-related operations.
   """
 
-  def ffmpeg_resize(uri_or_path, %{max_width: max_width, max_height: max_height} = options) do
-    quality = options[:quality] || 1
+  def image_resize(url, %{max_width: max_width, max_height: max_height} = options) do
+    quality = options[:quality] || 85
 
     cmd = ~s"""
-    ffmpeg -i #{uri_or_path} -f lavfi -i color=c=white \
-      -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
+    convert - -resize '#{max_width}x#{max_height}>' -quality #{quality} -
     """
 
     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)
   end
 
index 1c51aa5e33422908b10e9f22175b1aa1a4ef87c8..b925973ba6cf587683a781da759b2c93397e4d20 100644 (file)
@@ -66,25 +66,20 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
   end
 
   defp handle_preview("image/" <> _ = _content_type, conn, url) do
-    handle_image_or_video_preview(conn, url)
-  end
-
-  defp handle_preview("video/" <> _ = _content_type, conn, url) do
-    handle_image_or_video_preview(conn, url)
+    handle_image_preview(conn, url)
   end
 
   defp handle_preview(content_type, conn, _url) do
     send_resp(conn, :unprocessable_entity, "Unsupported content type: #{content_type}.")
   end
 
-  defp handle_image_or_video_preview(%{params: params} = conn, url) do
-    quality = Config.get!([:media_preview_proxy, :quality])
+  defp handle_image_preview(%{params: params} = conn, url) do
+    quality = Config.get!([:media_preview_proxy, :image_quality])
 
     with {thumbnail_max_width, thumbnail_max_height} <- thumbnail_max_dimensions(params),
-         media_proxy_url <- MediaProxy.url(url),
          {:ok, thumbnail_binary} <-
-           MediaHelper.ffmpeg_resize(
-             media_proxy_url,
+           MediaHelper.image_resize(
+             url,
              %{max_width: thumbnail_max_width, max_height: thumbnail_max_height, quality: quality}
            ) do
       conn