[#2497] Added video preview proxy. Switched from exexec to Port.
[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}) do
11 cmd = ~s"""
12 ffmpeg -i #{uri_or_path} -f lavfi -i color=c=white \
13 -filter_complex "[0:v] scale='min(#{max_width},iw)':'min(#{max_height},ih)': \
14 force_original_aspect_ratio=decrease [scaled]; \
15 [1][scaled] scale2ref [bg][img]; [bg] setsar=1 [bg]; [bg][img] overlay=shortest=1" \
16 -loglevel quiet -f image2 -vcodec mjpeg -frames:v 1 pipe:1
17 """
18
19 pid = Port.open({:spawn, cmd}, [:use_stdio, :in, :stream, :exit_status, :binary])
20
21 receive do
22 {^pid, {:data, data}} ->
23 send(pid, {self(), :close})
24 {:ok, data}
25
26 {^pid, {:exit_status, status}} when status > 0 ->
27 {:error, status}
28 end
29 end
30 end