Attempt at supporting video thumbnails via ffmpeg

This commit is contained in:
Mark Felder 2020-08-27 12:31:55 -05:00
parent 157ecf4022
commit ef9d12fcc5
2 changed files with 33 additions and 3 deletions

View file

@ -37,6 +37,25 @@ defp prepare_image_resize_args(%{max_width: max_width, max_height: max_height} =
defp prepare_image_resize_args(_), do: {:error, :missing_options} defp prepare_image_resize_args(_), do: {:error, :missing_options}
def video_framegrab(url) do
with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
args = [
"-i", "-",
"-vframes", "1",
"-f", "mjpeg",
"-loglevel", "error",
"-"
],
url = Pleroma.Web.MediaProxy.url(url),
{:ok, env} <- Pleroma.HTTP.get(url),
{:ok, fifo_path} <- mkfifo() do
run_fifo(fifo_path, env, executable, args)
else
nil -> {:error, {:ffmpeg, :command_not_found}}
{:error, _} = error -> error
end
end
defp run_fifo(fifo_path, env, executable, args) do defp run_fifo(fifo_path, env, executable, args) do
args = List.flatten([fifo_path, args]) args = List.flatten([fifo_path, args])
pid = Port.open({:spawn_executable, executable}, [:use_stdio, :stream, :exit_status, :binary, args: args]) pid = Port.open({:spawn_executable, executable}, [:use_stdio, :stream, :exit_status, :binary, args: args])

View file

@ -78,9 +78,7 @@ defp handle_preview("image/" <> _ = _content_type, conn, url) do
end end
defp handle_preview("video/" <> _ = _content_type, conn, url) do defp handle_preview("video/" <> _ = _content_type, conn, url) do
mediaproxy_url = url |> MediaProxy.url() handle_video_preview(conn, url)
redirect(conn, external: mediaproxy_url)
end end
defp handle_preview(content_type, conn, _url) do defp handle_preview(content_type, conn, _url) do
@ -106,6 +104,19 @@ defp handle_image_preview(%{params: params} = conn, url) do
end end
end end
defp handle_video_preview(conn, url) do
with {:ok, thumbnail_binary} <-
MediaHelper.video_framegrab(url) do
conn
|> put_resp_header("content-type", "image/jpeg")
|> put_resp_header("content-disposition", "inline; filename=\"preview.jpg\"")
|> send_resp(200, thumbnail_binary)
else
_ ->
send_resp(conn, :failed_dependency, "Can't handle preview.")
end
end
defp thumbnail_max_dimensions(params) do defp thumbnail_max_dimensions(params) do
config = Config.get([:media_preview_proxy], []) config = Config.get([:media_preview_proxy], [])