Rewrite MP4/MOV binaries to be faststart
[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() do
18 args = List.flatten([fifo_path, args])
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
30 args = [
31 "-interlace",
32 "Plane",
33 "-resize",
34 resize,
35 "-quality",
36 to_string(quality),
37 "jpg:-"
38 ]
39
40 {:ok, args}
41 end
42
43 defp prepare_image_resize_args(_), do: {:error, :missing_options}
44
45 def video_framegrab(url) do
46 with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
47 url = Pleroma.Web.MediaProxy.url(url),
48 {:ok, env} <- Pleroma.HTTP.get(url),
49 {:ok, fifo_path} <- mkfifo(),
50 args = [
51 "-y",
52 "-i",
53 fifo_path,
54 "-vframes",
55 "1",
56 "-f",
57 "mjpeg",
58 "-loglevel",
59 "error",
60 "-"
61 ] do
62 run_fifo(fifo_path, env, executable, args)
63 else
64 nil -> {:error, {:ffmpeg, :command_not_found}}
65 {:error, _} = error -> error
66 end
67 end
68
69 defp run_fifo(fifo_path, env, executable, args) do
70 pid =
71 Port.open({:spawn_executable, executable}, [
72 :use_stdio,
73 :stream,
74 :exit_status,
75 :binary,
76 args: args
77 ])
78
79 fifo = Port.open(to_charlist(fifo_path), [:eof, :binary, :stream, :out])
80 fix = Pleroma.Helpers.QtFastStart.fix(env.body)
81 true = Port.command(fifo, fix)
82 :erlang.port_close(fifo)
83 loop_recv(pid)
84 after
85 File.rm(fifo_path)
86 end
87
88 defp mkfifo() do
89 path = "#{@tmp_base}#{to_charlist(:erlang.phash2(self()))}"
90
91 case System.cmd("mkfifo", [path]) do
92 {_, 0} ->
93 spawn(fifo_guard(path))
94 {:ok, path}
95
96 {_, err} ->
97 {:error, {:fifo_failed, err}}
98 end
99 end
100
101 defp fifo_guard(path) do
102 pid = self()
103
104 fn ->
105 ref = Process.monitor(pid)
106
107 receive do
108 {:DOWN, ^ref, :process, ^pid, _} ->
109 File.rm(path)
110 end
111 end
112 end
113
114 defp loop_recv(pid) do
115 loop_recv(pid, <<>>)
116 end
117
118 defp loop_recv(pid, acc) do
119 receive do
120 {^pid, {:data, data}} ->
121 loop_recv(pid, acc <> data)
122
123 {^pid, {:exit_status, 0}} ->
124 {:ok, acc}
125
126 {^pid, {:exit_status, status}} ->
127 {:error, status}
128 after
129 5000 ->
130 :erlang.port_close(pid)
131 {:error, :timeout}
132 end
133 end
134 end