2020-05-20 17:26:43 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Helpers.MediaHelper do
|
|
|
|
@moduledoc """
|
|
|
|
Handles common media-related operations.
|
|
|
|
"""
|
|
|
|
|
2020-08-21 05:59:08 +00:00
|
|
|
def ffmpeg_resize(uri_or_path, %{max_width: max_width, max_height: max_height} = options) do
|
|
|
|
quality = options[:quality] || 1
|
|
|
|
|
2020-05-20 17:26:43 +00:00
|
|
|
cmd = ~s"""
|
2020-08-18 15:23:27 +00:00
|
|
|
ffmpeg -i #{uri_or_path} -f lavfi -i color=c=white \
|
2020-05-21 14:35:42 +00:00
|
|
|
-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" \
|
2020-08-21 05:59:08 +00:00
|
|
|
-loglevel quiet -f image2 -vcodec mjpeg -frames:v 1 -q:v #{quality} pipe:1
|
2020-05-20 17:26:43 +00:00
|
|
|
"""
|
|
|
|
|
2020-08-18 15:23:27 +00:00
|
|
|
pid = Port.open({:spawn, cmd}, [:use_stdio, :in, :stream, :exit_status, :binary])
|
2020-08-21 17:02:57 +00:00
|
|
|
loop_recv(pid)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp loop_recv(pid) do
|
|
|
|
loop_recv(pid, <<>>)
|
|
|
|
end
|
2020-08-18 15:23:27 +00:00
|
|
|
|
2020-08-21 17:02:57 +00:00
|
|
|
defp loop_recv(pid, acc) do
|
2020-08-18 15:23:27 +00:00
|
|
|
receive do
|
|
|
|
{^pid, {:data, data}} ->
|
2020-08-21 17:02:57 +00:00
|
|
|
loop_recv(pid, acc <> data)
|
2020-08-18 15:23:27 +00:00
|
|
|
|
2020-08-21 17:02:57 +00:00
|
|
|
{^pid, {:exit_status, 0}} ->
|
|
|
|
{:ok, acc}
|
|
|
|
|
|
|
|
{^pid, {:exit_status, status}} ->
|
2020-08-18 15:23:27 +00:00
|
|
|
{:error, status}
|
2020-05-20 17:26:43 +00:00
|
|
|
end
|
|
|
|
end
|
2020-08-21 17:02:57 +00:00
|
|
|
|
2020-05-20 17:26:43 +00:00
|
|
|
end
|