From c59fdc7252d3ceaaa223d7518348d7ae1dfcd301 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 16 May 2020 13:48:54 -0400 Subject: [PATCH] update mediatools to b36a7c3 --- docker/app/Dockerfile | 4 +-- lib/philomena/analyzers/gif.ex | 62 ++++++--------------------------- lib/philomena/analyzers/jpeg.ex | 19 +++++----- lib/philomena/analyzers/png.ex | 45 ++++++------------------ lib/philomena/analyzers/svg.ex | 18 +++++----- 5 files changed, 41 insertions(+), 107 deletions(-) diff --git a/docker/app/Dockerfile b/docker/app/Dockerfile index 7d85b061..701df499 100644 --- a/docker/app/Dockerfile +++ b/docker/app/Dockerfile @@ -11,8 +11,8 @@ RUN apt-get update; \ cd /tmp; \ git clone https://github.com/derpibooru/cli_intensities; \ git clone https://github.com/derpibooru/mediatools; \ - (cd cli_intensities && make install); \ - (cd mediatools && make install); \ + (cd cli_intensities; make install); \ + (cd mediatools; make install); \ mix local.hex --force; \ mix local.rebar --force diff --git a/lib/philomena/analyzers/gif.ex b/lib/philomena/analyzers/gif.ex index 4685a23f..2253ac04 100644 --- a/lib/philomena/analyzers/gif.ex +++ b/lib/philomena/analyzers/gif.ex @@ -1,71 +1,29 @@ defmodule Philomena.Analyzers.Gif do def analyze(file) do - animated? = animated?(file) - duration = duration(animated?, file) + stats = stats(file) %{ extension: "gif", mime_type: "image/gif", - animated?: animated?, - duration: duration, - dimensions: dimensions(file) + animated?: stats.animated?, + duration: stats.duration, + dimensions: stats.dimensions } end - defp animated?(file) do - System.cmd("identify", [file]) - |> case do + defp stats(file) do + case System.cmd("mediastat", [file]) do {output, 0} -> - len = + [_size, frames, width, height, num, den] = output - |> String.split("\n", parts: 2, trim: true) - |> length() - - len > 1 - - _error -> - nil - end - end - - defp duration(false, _file), do: 0.0 - - defp duration(true, file) do - with {output, 0} <- - System.cmd("ffprobe", [ - "-i", - file, - "-show_entries", - "format=duration", - "-v", - "quiet", - "-of", - "csv=p=0" - ]), - {duration, _} <- Float.parse(output) do - duration - else - _ -> - 0.0 - end - end - - defp dimensions(file) do - System.cmd("identify", ["-format", "%W %H\n", file]) - |> case do - {output, 0} -> - [width, height] = - output - |> String.split("\n", trim: true) - |> hd() |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) - {width, height} + %{animated?: frames > 1, dimensions: {width, height}, duration: num / den} - _error -> - {0, 0} + _ -> + %{animated?: false, dimensions: {0, 0}, duration: 0.0} end end end diff --git a/lib/philomena/analyzers/jpeg.ex b/lib/philomena/analyzers/jpeg.ex index a3a52a22..c73564b7 100644 --- a/lib/philomena/analyzers/jpeg.ex +++ b/lib/philomena/analyzers/jpeg.ex @@ -1,28 +1,29 @@ defmodule Philomena.Analyzers.Jpeg do def analyze(file) do + stats = stats(file) + %{ extension: "jpg", mime_type: "image/jpeg", animated?: false, - duration: 0.0, - dimensions: dimensions(file) + duration: stats.duration, + dimensions: stats.dimensions } end - defp dimensions(file) do - System.cmd("identify", ["-format", "%W %H\n", file]) - |> case do + defp stats(file) do + case System.cmd("mediastat", [file]) do {output, 0} -> - [width, height] = + [_size, _frames, width, height, num, den] = output |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) - {width, height} + %{dimensions: {width, height}, duration: num / den} - _error -> - {0, 0} + _ -> + %{dimensions: {0, 0}, duration: 0.0} end end end diff --git a/lib/philomena/analyzers/png.ex b/lib/philomena/analyzers/png.ex index 6e7e78c7..30d3bc9b 100644 --- a/lib/philomena/analyzers/png.ex +++ b/lib/philomena/analyzers/png.ex @@ -1,54 +1,29 @@ defmodule Philomena.Analyzers.Png do def analyze(file) do - animated? = animated?(file) - duration = duration(animated?, file) + stats = stats(file) %{ extension: "png", mime_type: "image/png", - animated?: animated?, - duration: duration, - dimensions: dimensions(file) + animated?: stats.animated?, + duration: stats.duration, + dimensions: stats.dimensions } end - defp animated?(file) do - System.cmd("ffprobe", [ - "-i", - file, - "-v", - "quiet", - "-show_entries", - "stream=codec_name", - "-of", - "csv=p=0" - ]) - |> case do - {"apng\n", 0} -> - true - - _other -> - false - end - end - - # No tooling available for this yet. - defp duration(_animated?, _file), do: 0.0 - - defp dimensions(file) do - System.cmd("identify", ["-format", "%W %H\n", file]) - |> case do + defp stats(file) do + case System.cmd("mediastat", [file]) do {output, 0} -> - [width, height] = + [_size, frames, width, height, num, den] = output |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) - {width, height} + %{animated?: frames > 1, dimensions: {width, height}, duration: num / den} - _error -> - {0, 0} + _ -> + %{animated?: false, dimensions: {0, 0}, duration: 0.0} end end end diff --git a/lib/philomena/analyzers/svg.ex b/lib/philomena/analyzers/svg.ex index 37036150..c6b41591 100644 --- a/lib/philomena/analyzers/svg.ex +++ b/lib/philomena/analyzers/svg.ex @@ -1,29 +1,29 @@ defmodule Philomena.Analyzers.Svg do def analyze(file) do + stats = stats(file) + %{ extension: "svg", mime_type: "image/svg+xml", animated?: false, duration: 0.0, - dimensions: dimensions(file) + dimensions: stats.dimensions } end - # Force use of MSVG to prevent invoking inkscape - defp dimensions(file) do - System.cmd("identify", ["-format", "%W %H\n", "msvg:#{file}"]) - |> case do + defp stats(file) do + case System.cmd("mediastat", [file]) do {output, 0} -> - [width, height] = + [_size, _frames, width, height, _num, _den] = output |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1) - {width, height} + %{dimensions: {width, height}} - _error -> - {0, 0} + _ -> + %{dimensions: {0, 0}} end end end