[#2497] Media preview proxy: added `quality` config setting, adjusted width/height...
[akkoma] / lib / pleroma / helpers / media_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Helpers.MediaHelper do
6 @moduledoc """
7 Handles common media-related operations.
8 """
9
10 def ffmpeg_resize(uri_or_path, %{max_width: max_width, max_height: max_height} = options) do
11 quality = options[:quality] || 1
12
13 cmd = ~s"""
14 ffmpeg -i #{uri_or_path} -f lavfi -i color=c=white \
15 -filter_complex "[0:v] scale='min(#{max_width},iw)':'min(#{max_height},ih)': \
16 force_original_aspect_ratio=decrease [scaled]; \
17 [1][scaled] scale2ref [bg][img]; [bg] setsar=1 [bg]; [bg][img] overlay=shortest=1" \
18 -loglevel quiet -f image2 -vcodec mjpeg -frames:v 1 -q:v #{quality} pipe:1
19 """
20
21 pid = Port.open({:spawn, cmd}, [:use_stdio, :in, :stream, :exit_status, :binary])
22
23 receive do
24 {^pid, {:data, data}} ->
25 send(pid, {self(), :close})
26 {:ok, data}
27
28 {^pid, {:exit_status, status}} when status > 0 ->
29 {:error, status}
30 end
31 end
32 end