Try to trick ffmpeg into working with this named pipe
[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 @tmp_base "/tmp/pleroma-media_preview-pipe"
11
12 def image_resize(url, options) do
13 with executable when is_binary(executable) <- System.find_executable("convert"),
14 {:ok, args} <- prepare_image_resize_args(options),
15 url = Pleroma.Web.MediaProxy.url(url),
16 {:ok, env} <- Pleroma.HTTP.get(url),
17 {:ok, fifo_path} <- mkfifo()
18 do
19 run_fifo(fifo_path, env, executable, args)
20 else
21 nil -> {:error, {:convert, :command_not_found}}
22 {:error, _} = error -> error
23 end
24 end
25
26 defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} = options) do
27 quality = options[:quality] || 85
28 resize = Enum.join([max_width, "x", max_height, ">"])
29 args = [
30 "-interlace", "Plane",
31 "-resize", resize,
32 "-quality", to_string(quality),
33 "jpg:-"
34 ]
35 {:ok, args}
36 end
37
38 defp prepare_image_resize_args(_), do: {:error, :missing_options}
39
40 def video_framegrab(url) do
41 with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
42 url = Pleroma.Web.MediaProxy.url(url),
43 {:ok, env} <- Pleroma.HTTP.get(url),
44 {:ok, fifo_path} <- mkfifo(),
45 args = [
46 "-y",
47 "-i", fifo_path,
48 "-vframes", "1",
49 "-f", "mjpeg",
50 "-loglevel", "error",
51 "pipe:"
52 ] do
53 run_fifo(fifo_path, env, executable, args)
54 else
55 nil -> {:error, {:ffmpeg, :command_not_found}}
56 {:error, _} = error -> error
57 end
58 end
59
60 defp run_fifo(fifo_path, env, executable, args) do
61 args =
62 if _executable = System.find_executable("convert") do
63 List.flatten([fifo_path, args])
64 else
65 args
66 end
67 pid = Port.open({:spawn_executable, executable}, [:use_stdio, :stream, :exit_status, :binary, args: args])
68 fifo = Port.open(to_charlist(fifo_path), [:eof, :binary, :stream, :out])
69 true = Port.command(fifo, env.body)
70 :erlang.port_close(fifo)
71 loop_recv(pid)
72 after
73 File.rm(fifo_path)
74 end
75
76 defp mkfifo() do
77 path = "#{@tmp_base}#{to_charlist(:erlang.phash2(self()))}"
78 case System.cmd("mkfifo", [path]) do
79 {_, 0} ->
80 spawn(fifo_guard(path))
81 {:ok, path}
82 {_, err} ->
83 {:error, {:fifo_failed, err}}
84 end
85 end
86
87 defp fifo_guard(path) do
88 pid = self()
89 fn() ->
90 ref = Process.monitor(pid)
91 receive do
92 {:DOWN, ^ref, :process, ^pid, _} ->
93 File.rm(path)
94 end
95 end
96 end
97
98 defp loop_recv(pid) do
99 loop_recv(pid, <<>>)
100 end
101
102 defp loop_recv(pid, acc) do
103 receive do
104 {^pid, {:data, data}} ->
105 loop_recv(pid, acc <> data)
106 {^pid, {:exit_status, 0}} ->
107 {:ok, acc}
108 {^pid, {:exit_status, status}} ->
109 {:error, status}
110 after
111 5000 ->
112 :erlang.port_close(pid)
113 {:error, :timeout}
114 end
115 end
116 end