Remove newline for linter
[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 loop_recv(pid)
23 end
24
25 defp loop_recv(pid) do
26 loop_recv(pid, <<>>)
27 end
28
29 defp loop_recv(pid, acc) do
30 receive do
31 {^pid, {:data, data}} ->
32 loop_recv(pid, acc <> data)
33
34 {^pid, {:exit_status, 0}} ->
35 {:ok, acc}
36
37 {^pid, {:exit_status, status}} ->
38 {:error, status}
39 end
40 end
41 end