2020-05-20 11:26:43 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-05-20 11:26:43 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Helpers.MediaHelper do
|
|
|
|
@moduledoc """
|
|
|
|
Handles common media-related operations.
|
|
|
|
"""
|
|
|
|
|
2020-09-05 07:16:35 -06:00
|
|
|
alias Pleroma.HTTP
|
|
|
|
|
2020-09-26 10:32:16 -06:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
def missing_dependencies do
|
|
|
|
Enum.reduce([imagemagick: "convert", ffmpeg: "ffmpeg"], [], fn {sym, executable}, acc ->
|
|
|
|
if Pleroma.Utils.command_available?(executable) do
|
|
|
|
acc
|
|
|
|
else
|
|
|
|
[sym | acc]
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-08-26 08:12:34 -06:00
|
|
|
def image_resize(url, options) do
|
|
|
|
with executable when is_binary(executable) <- System.find_executable("convert"),
|
2020-08-26 15:40:13 -06:00
|
|
|
{:ok, args} <- prepare_image_resize_args(options),
|
2022-07-04 10:30:38 -06:00
|
|
|
{:ok, env} <- HTTP.get(url, [], []),
|
2020-08-28 13:14:28 -06:00
|
|
|
{:ok, fifo_path} <- mkfifo() do
|
2020-08-27 15:33:37 -06:00
|
|
|
args = List.flatten([fifo_path, args])
|
2020-08-26 08:12:34 -06:00
|
|
|
run_fifo(fifo_path, env, executable, args)
|
|
|
|
else
|
|
|
|
nil -> {:error, {:convert, :command_not_found}}
|
|
|
|
{:error, _} = error -> error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-30 08:17:24 -06:00
|
|
|
defp prepare_image_resize_args(
|
|
|
|
%{max_width: max_width, max_height: max_height, format: "png"} = options
|
|
|
|
) do
|
|
|
|
quality = options[:quality] || 85
|
|
|
|
resize = Enum.join([max_width, "x", max_height, ">"])
|
|
|
|
|
|
|
|
args = [
|
|
|
|
"-resize",
|
|
|
|
resize,
|
|
|
|
"-quality",
|
|
|
|
to_string(quality),
|
|
|
|
"png:-"
|
|
|
|
]
|
|
|
|
|
|
|
|
{:ok, args}
|
|
|
|
end
|
|
|
|
|
2020-08-26 15:40:13 -06:00
|
|
|
defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
|
2020-08-25 16:18:22 -06:00
|
|
|
quality = options[:quality] || 85
|
2020-08-26 08:12:34 -06:00
|
|
|
resize = Enum.join([max_width, "x", max_height, ">"])
|
2020-08-28 13:14:28 -06:00
|
|
|
|
2020-08-26 08:12:34 -06:00
|
|
|
args = [
|
2020-08-28 13:14:28 -06:00
|
|
|
"-interlace",
|
|
|
|
"Plane",
|
|
|
|
"-resize",
|
|
|
|
resize,
|
|
|
|
"-quality",
|
|
|
|
to_string(quality),
|
|
|
|
"jpg:-"
|
2020-08-26 08:12:34 -06:00
|
|
|
]
|
2020-08-28 13:14:28 -06:00
|
|
|
|
2020-08-26 08:12:34 -06:00
|
|
|
{:ok, args}
|
|
|
|
end
|
2020-08-20 23:59:08 -06:00
|
|
|
|
2020-08-26 15:40:13 -06:00
|
|
|
defp prepare_image_resize_args(_), do: {:error, :missing_options}
|
2020-05-20 11:26:43 -06:00
|
|
|
|
2020-09-17 08:13:40 -06:00
|
|
|
# Note: video thumbnail is intentionally not resized (always has original dimensions)
|
2020-08-27 11:31:55 -06:00
|
|
|
def video_framegrab(url) do
|
|
|
|
with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
|
2022-07-04 10:30:38 -06:00
|
|
|
{:ok, env} <- HTTP.get(url, [], []),
|
2020-08-27 11:47:29 -06:00
|
|
|
{:ok, fifo_path} <- mkfifo(),
|
2020-08-27 11:31:55 -06:00
|
|
|
args = [
|
2020-08-27 12:10:40 -06:00
|
|
|
"-y",
|
2020-08-28 13:14:28 -06:00
|
|
|
"-i",
|
|
|
|
fifo_path,
|
|
|
|
"-vframes",
|
|
|
|
"1",
|
|
|
|
"-f",
|
|
|
|
"mjpeg",
|
|
|
|
"-loglevel",
|
|
|
|
"error",
|
|
|
|
"-"
|
2020-08-27 11:47:29 -06:00
|
|
|
] do
|
2020-08-27 11:31:55 -06:00
|
|
|
run_fifo(fifo_path, env, executable, args)
|
|
|
|
else
|
|
|
|
nil -> {:error, {:ffmpeg, :command_not_found}}
|
|
|
|
{:error, _} = error -> error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 08:12:34 -06:00
|
|
|
defp run_fifo(fifo_path, env, executable, args) do
|
2020-08-28 13:14:28 -06:00
|
|
|
pid =
|
|
|
|
Port.open({:spawn_executable, executable}, [
|
|
|
|
:use_stdio,
|
|
|
|
:stream,
|
|
|
|
:exit_status,
|
|
|
|
:binary,
|
|
|
|
args: args
|
|
|
|
])
|
|
|
|
|
2022-12-14 05:38:48 -07:00
|
|
|
fifo = File.open!(fifo_path, [:append, :binary])
|
2020-08-28 13:14:28 -06:00
|
|
|
fix = Pleroma.Helpers.QtFastStart.fix(env.body)
|
2022-12-14 05:38:48 -07:00
|
|
|
IO.binwrite(fifo, fix)
|
|
|
|
File.close(fifo)
|
2020-08-21 11:02:57 -06:00
|
|
|
loop_recv(pid)
|
2020-08-26 08:12:34 -06:00
|
|
|
after
|
|
|
|
File.rm(fifo_path)
|
|
|
|
end
|
|
|
|
|
2020-08-30 08:32:22 -06:00
|
|
|
defp mkfifo do
|
2020-09-16 13:30:42 -06:00
|
|
|
path = Path.join(System.tmp_dir!(), "pleroma-media-preview-pipe-#{Ecto.UUID.generate()}")
|
2020-08-28 13:14:28 -06:00
|
|
|
|
2020-08-26 08:12:34 -06:00
|
|
|
case System.cmd("mkfifo", [path]) do
|
|
|
|
{_, 0} ->
|
|
|
|
spawn(fifo_guard(path))
|
|
|
|
{:ok, path}
|
2020-08-28 13:14:28 -06:00
|
|
|
|
2020-08-26 08:12:34 -06:00
|
|
|
{_, err} ->
|
|
|
|
{:error, {:fifo_failed, err}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fifo_guard(path) do
|
|
|
|
pid = self()
|
2020-08-28 13:14:28 -06:00
|
|
|
|
|
|
|
fn ->
|
2020-08-26 08:12:34 -06:00
|
|
|
ref = Process.monitor(pid)
|
2020-08-28 13:14:28 -06:00
|
|
|
|
2020-08-26 08:12:34 -06:00
|
|
|
receive do
|
|
|
|
{:DOWN, ^ref, :process, ^pid, _} ->
|
|
|
|
File.rm(path)
|
|
|
|
end
|
|
|
|
end
|
2020-08-21 11:02:57 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp loop_recv(pid) do
|
|
|
|
loop_recv(pid, <<>>)
|
|
|
|
end
|
2020-08-18 09:23:27 -06:00
|
|
|
|
2020-08-21 11:02:57 -06:00
|
|
|
defp loop_recv(pid, acc) do
|
2020-08-18 09:23:27 -06:00
|
|
|
receive do
|
|
|
|
{^pid, {:data, data}} ->
|
2020-08-21 11:02:57 -06:00
|
|
|
loop_recv(pid, acc <> data)
|
2020-08-28 13:14:28 -06:00
|
|
|
|
2020-08-21 11:02:57 -06:00
|
|
|
{^pid, {:exit_status, 0}} ->
|
|
|
|
{:ok, acc}
|
2020-08-28 13:14:28 -06:00
|
|
|
|
2020-08-21 11:02:57 -06:00
|
|
|
{^pid, {:exit_status, status}} ->
|
2020-08-18 09:23:27 -06:00
|
|
|
{:error, status}
|
2020-08-28 13:14:28 -06:00
|
|
|
after
|
|
|
|
5000 ->
|
|
|
|
:erlang.port_close(pid)
|
|
|
|
{:error, :timeout}
|
2020-05-20 11:26:43 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|