[#2497] Media preview proxy: added `quality` config setting, adjusted width/height...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 21 Aug 2020 05:59:08 +0000 (08:59 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 21 Aug 2020 05:59:08 +0000 (08:59 +0300)
config/config.exs
config/description.exs
lib/pleroma/helpers/media_helper.ex
lib/pleroma/web/media_proxy/media_proxy_controller.ex

index 6e6231cf8a16b1e975ed7162be08f242bdd165da..b399ce6d7c5f19ad65cfecdbac8054e72f965c24 100644 (file)
@@ -441,8 +441,9 @@ config :pleroma, Pleroma.Web.MediaProxy.Invalidation.Script, script_path: nil
 # Note: media preview proxy depends on media proxy to be enabled
 config :pleroma, :media_preview_proxy,
   enabled: false,
-  thumbnail_max_width: 400,
-  thumbnail_max_height: 200,
+  thumbnail_max_width: 600,
+  thumbnail_max_height: 600,
+  quality: 2,
   proxy_opts: [
     head_request_max_read_duration: 5_000
   ]
index 90d8eca65b0da2ddbd66c0275e631aca2a98b558..22da60900b29f3908456a0ecf1e7276cfccf1a18 100644 (file)
@@ -1925,6 +1925,11 @@ config :pleroma, :config_description, [
         type: :integer,
         description: "Max height of preview thumbnail."
       },
+      %{
+        key: :quality,
+        type: :integer,
+        description: "Quality of the output. Ranges from 1 (max quality) to 31 (lowest quality)."
+      },
       %{
         key: :proxy_opts,
         type: :keyword,
index ca46698ccfb98a296d52a102a1a030f9e57e831e..e11038052d6bd5df2c99c91617bf13cfd85901ac 100644 (file)
@@ -7,13 +7,15 @@ defmodule Pleroma.Helpers.MediaHelper do
   Handles common media-related operations.
   """
 
-  def ffmpeg_resize(uri_or_path, %{max_width: max_width, max_height: max_height}) do
+  def ffmpeg_resize(uri_or_path, %{max_width: max_width, max_height: max_height} = options) do
+    quality = options[:quality] || 1
+
     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 pipe: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])
index 5513432f0bad9b6da3351523bd93cc9a91fb541d..1c51aa5e33422908b10e9f22175b1aa1a4ef87c8 100644 (file)
@@ -78,12 +78,14 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do
   end
 
   defp handle_image_or_video_preview(%{params: params} = conn, url) do
+    quality = Config.get!([:media_preview_proxy, :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,
-             %{max_width: thumbnail_max_width, max_height: thumbnail_max_height}
+             %{max_width: thumbnail_max_width, max_height: thumbnail_max_height, quality: quality}
            ) do
       conn
       |> put_resp_header("content-type", "image/jpeg")