Attempt at supporting video thumbnails via ffmpeg
[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 args = [
43 "-i", "-",
44 "-vframes", "1",
45 "-f", "mjpeg",
46 "-loglevel", "error",
47 "-"
48 ],
49 url = Pleroma.Web.MediaProxy.url(url),
50 {:ok, env} <- Pleroma.HTTP.get(url),
51 {:ok, fifo_path} <- mkfifo() do
52 run_fifo(fifo_path, env, executable, args)
53 else
54 nil -> {:error, {:ffmpeg, :command_not_found}}
55 {:error, _} = error -> error
56 end
57 end
58
59 defp run_fifo(fifo_path, env, executable, args) do
60 args = List.flatten([fifo_path, args])
61 pid = Port.open({:spawn_executable, executable}, [:use_stdio, :stream, :exit_status, :binary, args: args])
62 fifo = Port.open(to_charlist(fifo_path), [:eof, :binary, :stream, :out])
63 true = Port.command(fifo, env.body)
64 :erlang.port_close(fifo)
65 loop_recv(pid)
66 after
67 File.rm(fifo_path)
68 end
69
70 defp mkfifo() do
71 path = "#{@tmp_base}#{to_charlist(:erlang.phash2(self()))}"
72 case System.cmd("mkfifo", [path]) do
73 {_, 0} ->
74 spawn(fifo_guard(path))
75 {:ok, path}
76 {_, err} ->
77 {:error, {:fifo_failed, err}}
78 end
79 end
80
81 defp fifo_guard(path) do
82 pid = self()
83 fn() ->
84 ref = Process.monitor(pid)
85 receive do
86 {:DOWN, ^ref, :process, ^pid, _} ->
87 File.rm(path)
88 end
89 end
90 end
91
92 defp loop_recv(pid) do
93 loop_recv(pid, <<>>)
94 end
95
96 defp loop_recv(pid, acc) do
97 receive do
98 {^pid, {:data, data}} ->
99 loop_recv(pid, acc <> data)
100 {^pid, {:exit_status, 0}} ->
101 {:ok, acc}
102 {^pid, {:exit_status, status}} ->
103 {:error, status}
104 after
105 5000 ->
106 :erlang.port_close(pid)
107 {:error, :timeout}
108 end
109 end
110 end