[#2497] Image preview proxy: image resize & background color fix with ffmpeg -filter_...
[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 @ffmpeg_opts [{:sync, true}, {:stdout, true}]
11
12 def ffmpeg_resize_remote(uri, %{max_width: max_width, max_height: max_height}) do
13 cmd = ~s"""
14 curl -L "#{uri}" |
15 ffmpeg -i pipe:0 -f lavfi -i color=c=white \
16 -filter_complex "[0:v] scale='min(#{max_width},iw)':'min(#{max_height},ih)': \
17 force_original_aspect_ratio=decrease [scaled]; \
18 [1][scaled] scale2ref [bg][img]; [bg] setsar=1 [bg]; [bg][img] overlay=shortest=1" \
19 -f image2 -vcodec mjpeg -frames:v 1 pipe:1 | \
20 cat
21 """
22
23 with {:ok, [stdout: stdout_list]} <- Exexec.run(cmd, @ffmpeg_opts) do
24 {:ok, Enum.join(stdout_list)}
25 end
26 end
27 end