2020-05-20 17:26:43 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-05-20 17:26:43 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Helpers.MediaHelper do
|
|
|
|
@moduledoc """
|
|
|
|
Handles common media-related operations.
|
|
|
|
"""
|
|
|
|
|
2020-09-05 13:16:35 +00:00
|
|
|
alias Pleroma.HTTP
|
2022-11-09 18:45:57 +00:00
|
|
|
alias Vix.Vips.Operation
|
2020-09-05 13:16:35 +00:00
|
|
|
|
2020-09-26 16:32:16 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
def missing_dependencies do
|
2022-11-09 18:45:57 +00:00
|
|
|
Enum.reduce([ffmpeg: "ffmpeg"], [], fn {sym, executable}, acc ->
|
2020-09-26 16:32:16 +00:00
|
|
|
if Pleroma.Utils.command_available?(executable) do
|
|
|
|
acc
|
|
|
|
else
|
|
|
|
[sym | acc]
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-08-26 14:12:34 +00:00
|
|
|
def image_resize(url, options) do
|
2022-11-09 18:45:57 +00:00
|
|
|
with {:ok, env} <- HTTP.get(url, [], pool: :media),
|
|
|
|
{:ok, resized} <-
|
|
|
|
Operation.thumbnail_buffer(env.body, options.max_width,
|
|
|
|
height: options.max_height,
|
|
|
|
size: :VIPS_SIZE_DOWN
|
|
|
|
) do
|
|
|
|
if options[:format] == "png" do
|
|
|
|
Operation.pngsave_buffer(resized, Q: options[:quality])
|
|
|
|
else
|
|
|
|
Operation.jpegsave_buffer(resized, Q: options[:quality], interlace: true)
|
|
|
|
end
|
2020-08-26 14:12:34 +00:00
|
|
|
else
|
|
|
|
{:error, _} = error -> error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-17 14:13:40 +00:00
|
|
|
# Note: video thumbnail is intentionally not resized (always has original dimensions)
|
2020-08-27 17:31:55 +00:00
|
|
|
def video_framegrab(url) do
|
|
|
|
with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
|
2020-09-09 16:30:42 +00:00
|
|
|
{:ok, env} <- HTTP.get(url, [], pool: :media),
|
2020-08-27 17:47:29 +00:00
|
|
|
{:ok, fifo_path} <- mkfifo(),
|
2020-08-27 17:31:55 +00:00
|
|
|
args = [
|
2020-08-27 18:10:40 +00:00
|
|
|
"-y",
|
2020-08-28 19:14:28 +00:00
|
|
|
"-i",
|
|
|
|
fifo_path,
|
|
|
|
"-vframes",
|
|
|
|
"1",
|
|
|
|
"-f",
|
|
|
|
"mjpeg",
|
|
|
|
"-loglevel",
|
|
|
|
"error",
|
|
|
|
"-"
|
2020-08-27 17:47:29 +00:00
|
|
|
] do
|
2020-08-27 17:31:55 +00:00
|
|
|
run_fifo(fifo_path, env, executable, args)
|
|
|
|
else
|
|
|
|
nil -> {:error, {:ffmpeg, :command_not_found}}
|
|
|
|
{:error, _} = error -> error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 14:12:34 +00:00
|
|
|
defp run_fifo(fifo_path, env, executable, args) do
|
2020-08-28 19:14:28 +00:00
|
|
|
pid =
|
|
|
|
Port.open({:spawn_executable, executable}, [
|
|
|
|
:use_stdio,
|
|
|
|
:stream,
|
|
|
|
:exit_status,
|
|
|
|
:binary,
|
|
|
|
args: args
|
|
|
|
])
|
|
|
|
|
2020-08-26 14:12:34 +00:00
|
|
|
fifo = Port.open(to_charlist(fifo_path), [:eof, :binary, :stream, :out])
|
2020-08-28 19:14:28 +00:00
|
|
|
fix = Pleroma.Helpers.QtFastStart.fix(env.body)
|
|
|
|
true = Port.command(fifo, fix)
|
2020-08-26 14:12:34 +00:00
|
|
|
:erlang.port_close(fifo)
|
2020-08-21 17:02:57 +00:00
|
|
|
loop_recv(pid)
|
2020-08-26 14:12:34 +00:00
|
|
|
after
|
|
|
|
File.rm(fifo_path)
|
|
|
|
end
|
|
|
|
|
2020-08-30 14:32:22 +00:00
|
|
|
defp mkfifo do
|
2020-09-16 19:30:42 +00:00
|
|
|
path = Path.join(System.tmp_dir!(), "pleroma-media-preview-pipe-#{Ecto.UUID.generate()}")
|
2020-08-28 19:14:28 +00:00
|
|
|
|
2020-08-26 14:12:34 +00:00
|
|
|
case System.cmd("mkfifo", [path]) do
|
|
|
|
{_, 0} ->
|
|
|
|
spawn(fifo_guard(path))
|
|
|
|
{:ok, path}
|
2020-08-28 19:14:28 +00:00
|
|
|
|
2020-08-26 14:12:34 +00:00
|
|
|
{_, err} ->
|
|
|
|
{:error, {:fifo_failed, err}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fifo_guard(path) do
|
|
|
|
pid = self()
|
2020-08-28 19:14:28 +00:00
|
|
|
|
|
|
|
fn ->
|
2020-08-26 14:12:34 +00:00
|
|
|
ref = Process.monitor(pid)
|
2020-08-28 19:14:28 +00:00
|
|
|
|
2020-08-26 14:12:34 +00:00
|
|
|
receive do
|
|
|
|
{:DOWN, ^ref, :process, ^pid, _} ->
|
|
|
|
File.rm(path)
|
|
|
|
end
|
|
|
|
end
|
2020-08-21 17:02:57 +00:00
|
|
|
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-28 19:14:28 +00:00
|
|
|
|
2020-08-21 17:02:57 +00:00
|
|
|
{^pid, {:exit_status, 0}} ->
|
|
|
|
{:ok, acc}
|
2020-08-28 19:14:28 +00:00
|
|
|
|
2020-08-21 17:02:57 +00:00
|
|
|
{^pid, {:exit_status, status}} ->
|
2020-08-18 15:23:27 +00:00
|
|
|
{:error, status}
|
2020-08-28 19:14:28 +00:00
|
|
|
after
|
|
|
|
5000 ->
|
|
|
|
:erlang.port_close(pid)
|
|
|
|
{:error, :timeout}
|
2020-05-20 17:26:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|