logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

media_helper.ex (1871B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Helpers.MediaHelper do
  5. @moduledoc """
  6. Handles common media-related operations.
  7. """
  8. alias Pleroma.HTTP
  9. alias Vix.Vips.Operation
  10. require Logger
  11. def missing_dependencies do
  12. Enum.reduce([ffmpeg: "ffmpeg"], [], fn {sym, executable}, acc ->
  13. if Pleroma.Utils.command_available?(executable) do
  14. acc
  15. else
  16. [sym | acc]
  17. end
  18. end)
  19. end
  20. def image_resize(url, options) do
  21. with {:ok, env} <- HTTP.get(url, [], pool: :media),
  22. {:ok, resized} <-
  23. Operation.thumbnail_buffer(env.body, options.max_width,
  24. height: options.max_height,
  25. size: :VIPS_SIZE_DOWN
  26. ) do
  27. if options[:format] == "png" do
  28. Operation.pngsave_buffer(resized, Q: options[:quality])
  29. else
  30. Operation.jpegsave_buffer(resized, Q: options[:quality], interlace: true)
  31. end
  32. else
  33. {:error, _} = error -> error
  34. end
  35. end
  36. # Note: video thumbnail is intentionally not resized (always has original dimensions)
  37. def video_framegrab(url) do
  38. with executable when is_binary(executable) <- System.find_executable("ffmpeg"),
  39. {:ok, env} <- HTTP.get(url, [], pool: :media),
  40. {:ok, pid} <- StringIO.open(env.body) do
  41. body_stream = IO.binstream(pid, 1)
  42. Exile.stream!(
  43. [
  44. executable,
  45. "-i",
  46. "pipe:0",
  47. "-vframes",
  48. "1",
  49. "-f",
  50. "mjpeg",
  51. "pipe:1"
  52. ],
  53. input: body_stream,
  54. ignore_epipe: true,
  55. stderr: :disable
  56. )
  57. |> Enum.into(<<>>)
  58. else
  59. nil -> {:error, {:ffmpeg, :command_not_found}}
  60. {:error, _} = error -> error
  61. end
  62. end
  63. end