further boosts to encoding speed

This commit is contained in:
byte[] 2020-05-28 22:14:27 -04:00
parent 843398cead
commit d71cefe877

View file

@ -6,11 +6,13 @@ defmodule Philomena.Processors.Webm do
dimensions = analysis.dimensions
duration = analysis.duration
preview = preview(duration, file)
palette = gif_palette(file)
palette = gif_palette(file, duration)
mp4 = scale_mp4_only(file, dimensions, dimensions)
{:ok, intensities} = Intensities.file(preview)
scaled = Enum.flat_map(versions, &scale_if_smaller(file, palette, duration, dimensions, &1))
scaled =
Enum.flat_map(versions, &scale_if_smaller(file, mp4, palette, duration, dimensions, &1))
%{
intensities: intensities,
@ -33,9 +35,7 @@ defmodule Philomena.Processors.Webm do
preview
end
defp scale_if_smaller(file, palette, _duration, dimensions, {:full, _target_dim}) do
{_webm, mp4} = scale_videos(file, palette, dimensions, dimensions)
defp scale_if_smaller(_file, mp4, _palette, _duration, _dimensions, {:full, _target_dim}) do
[
{:symlink_original, "full.webm"},
{:copy, mp4, "full.mp4"}
@ -44,12 +44,18 @@ defmodule Philomena.Processors.Webm do
defp scale_if_smaller(
file,
mp4,
palette,
duration,
dimensions,
{width, height},
{thumb_name, {target_width, target_height}}
) do
{webm, mp4} = scale_videos(file, palette, dimensions, {target_width, target_height})
{webm, mp4} =
if width > target_width or height > target_height do
scale_videos(file, {width, height}, {target_width, target_height})
else
{file, mp4}
end
cond do
thumb_name in [:thumb, :thumb_small, :thumb_tiny] ->
@ -69,7 +75,7 @@ defmodule Philomena.Processors.Webm do
end
end
defp scale_videos(file, _palette, dimensions, target_dimensions) do
defp scale_videos(file, dimensions, target_dimensions) do
{width, height} = box_dimensions(dimensions, target_dimensions)
webm = Briefly.create!(extname: ".webm")
mp4 = Briefly.create!(extname: ".mp4")
@ -102,6 +108,8 @@ defmodule Philomena.Processors.Webm do
"4",
"-max_muxing_queue_size",
"4096",
"-slices",
"8",
webm,
"-c:v",
"libx264",
@ -127,6 +135,42 @@ defmodule Philomena.Processors.Webm do
{webm, mp4}
end
defp scale_mp4_only(file, dimensions, target_dimensions) do
{width, height} = box_dimensions(dimensions, target_dimensions)
mp4 = Briefly.create!(extname: ".mp4")
scale_filter = "scale=w=#{width}:h=#{height}"
{_output, 0} =
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-c:v",
"libx264",
"-pix_fmt",
"yuv420p",
"-profile:v",
"main",
"-preset",
"medium",
"-crf",
"18",
"-b:v",
"5M",
"-vf",
scale_filter,
"-threads",
"4",
"-max_muxing_queue_size",
"4096",
mp4
])
mp4
end
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"
@ -153,8 +197,11 @@ defmodule Philomena.Processors.Webm do
gif
end
defp gif_palette(file) do
defp gif_palette(file, duration) do
palette = Briefly.create!(extname: ".png")
palette_filter = "palettegen=stats_mode=diff"
rate_filter = rate_filter(duration)
filter_graph = "#{rate_filter},#{palette_filter}"
{_output, 0} =
System.cmd("ffmpeg", [
@ -164,7 +211,7 @@ defmodule Philomena.Processors.Webm do
"-i",
file,
"-vf",
"palettegen=stats_mode=diff",
filter_graph,
palette
])