stop generating enormous gif thumbnails for videos

This commit is contained in:
byte[] 2019-12-31 23:11:17 -05:00
parent d783417608
commit b9ad5643dc
3 changed files with 20 additions and 11 deletions

View file

@ -10,7 +10,7 @@ defmodule Philomena.Processors.Webm do
{:ok, intensities} = Intensities.file(preview)
scaled = Enum.flat_map(versions, &scale_if_smaller(file, palette, dimensions, &1))
scaled = Enum.flat_map(versions, &scale_if_smaller(file, palette, duration, dimensions, &1))
%{
intensities: intensities,
@ -35,7 +35,7 @@ defmodule Philomena.Processors.Webm do
preview
end
defp scale_if_smaller(file, palette, dimensions, {:full, _target_dim}) do
defp scale_if_smaller(file, palette, _duration, dimensions, {:full, _target_dim}) do
{_webm, mp4} = scale_videos(file, palette, dimensions, dimensions)
[
@ -44,12 +44,12 @@ defmodule Philomena.Processors.Webm do
]
end
defp scale_if_smaller(file, palette, dimensions, {thumb_name, {target_width, target_height}}) do
defp scale_if_smaller(file, palette, duration, dimensions, {thumb_name, {target_width, target_height}}) do
{webm, mp4} = scale_videos(file, palette, dimensions, {target_width, target_height})
cond do
thumb_name in [:thumb, :thumb_small, :thumb_tiny] ->
gif = scale_gif(file, palette, {target_width, target_height})
gif = scale_gif(file, palette, duration, {target_width, target_height})
[
{:copy, webm, "#{thumb_name}.webm"},
@ -79,14 +79,15 @@ defmodule Philomena.Processors.Webm do
{webm, mp4}
end
defp scale_gif(file, palette, {width, height}) do
defp scale_gif(file, palette, duration, {width, height}) do
gif = Briefly.create!(extname: ".gif")
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"
palette_filter = "paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle"
filter_graph = "[0:v] #{scale_filter} [x]; [x][1:v] #{palette_filter}"
rate_filter = "fps=1/#{duration},settb=1/2,setpts=N"
filter_graph = "[0:v] #{scale_filter},#{rate_filter} [x]; [x][1:v] #{palette_filter}"
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-i", palette, "-lavfi", filter_graph, gif])
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-i", palette, "-lavfi", filter_graph, "-r", "2", gif])
gif
end

View file

@ -38,6 +38,7 @@ defmodule PhilomenaWeb.SettingController do
conn
|> set_cookie(user_params, "hidpi", "hidpi")
|> set_cookie(user_params, "webm", "webm")
|> set_cookie(user_params, "serve_webm", "serve_webm")
|> set_cookie(user_params, "chan_nsfw", "chan_nsfw")
|> set_cookie(user_params, "hide_staff_tools", "hide_staff_tools")
end

View file

@ -17,20 +17,24 @@
- true ->
.media-box__overlay.js-spoiler-info-overlay
= case render_intent(@conn, @image, @size) do
- {:hidpi, small_url, medium_url, hover_text} ->
.media-box__overlay.js-spoiler-info-overlay
a href=link title=hover_text
picture
img src=small_url srcset="#{small_url} 1x, #{medium_url} 2x" alt=hover_text
- {:image, small_url, hover_text} ->
.media-box__overlay.js-spoiler-info-overlay
= if @image.image_mime_type == "video/webm" do
| WebM
a href=link title=hover_text
picture
img src=small_url alt=hover_text
- {:video, webm, mp4, hover_text} ->
.media-box__overlay.js-spoiler-info-overlay
a href=link title=hover_text
video alt=hover_text autoplay="autoplay" muted="muted" loop="loop" playsinline="playsinline"
source src=webm type="video/webm"
@ -38,15 +42,18 @@
img alt=hover_text
- {:filtered_image, hover_text} ->
.media-box__overlay.js-spoiler-info-overlay
a href=link title=hover_text
picture
img alt=hover_text
- {:filtered_video, hover_text} ->
.media-box__overlay.js-spoiler-info-overlay
a href=link title=hover_text
video autoplay="autoplay" muted="muted" loop="loop" playsinline="playsinline"
img alt=hover_text
- :not_rendered ->
.media-box__overlay.js-spoiler-info-overlay
a href=link
' Thumbnails not yet generated
' Thumbnails not yet generated