From e1ef75f0413232ac06a92f194fd65a1836351c16 Mon Sep 17 00:00:00 2001 From: Stefan Breunig Date: Tue, 26 Mar 2024 20:05:54 +0100 Subject: [PATCH] speed up previews by scaling input(s) instead of output ``` // scaling inputs frame= 5395 fps= 34 q=17.0 Lsize= 345710kB time=00:02:59.97 bitrate=15735.4kbits/s speed=1.15x 976,68s user 8,95s system 626% cpu 2:37,30 total // scaling outputs frame= 5395 fps= 30 q=17.0 Lsize= 346170kB time=00:02:59.97 bitrate=15756.3kbits/s speed= 1x 1177,91s user 10,40s system 658% cpu 3:00,51 total ``` --- lib/video/renderer.ex | 19 +++++++++---------- test/video/renderer_test.exs | 4 +--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/video/renderer.ex b/lib/video/renderer.ex index 632aea1b..a0bcc027 100644 --- a/lib/video/renderer.ex +++ b/lib/video/renderer.ex @@ -14,7 +14,8 @@ defmodule Video.Renderer do ensure_min_version(rendered) sources = Video.Track.normalize_video_tuples(rendered.sources()) - blurred = if blur, do: blurs(sources), else: settb(sources) + prefix = "scale=640:-1," + blurred = if blur, do: blurs(sources, prefix), else: settb(sources, prefix) filter = Enum.join(blurred ++ time_lapse_corrects(sources) ++ xfades(sources, rendered), ";") filter = @@ -33,8 +34,6 @@ defmodule Video.Renderer do filter, "-pix_fmt", "yuv420p", - "-s", - "640x480", "-c:v", "libx264", "-preset", @@ -53,10 +52,9 @@ defmodule Video.Renderer do @spec adhoc_cmd(Video.Track.plain()) :: [binary()] def adhoc_cmd(sources) when is_list(sources) do sources = Video.Track.normalize_video_tuples(sources) - blurs = blurs(sources) + blurs = blurs(sources, "scale=1080:-1,") xfades = xfades(sources, Video.Track.default_fade(), "ad-hoc") filter = Enum.join(blurs ++ xfades, ";") - filter = filter <> "[scale];[scale]scale=1080:608" Util.low_priority_cmd_prefix() ++ ["ffmpeg", "-hide_banner", "-loglevel", "error"] ++ @@ -127,7 +125,7 @@ defmodule Video.Renderer do def render_cmd(rendered, tmp_dir) do sources = Video.Track.normalize_video_tuples(rendered.sources()) - filter = Enum.join(blurs(sources) ++ xfades(sources, rendered), ";") + filter = Enum.join(blurs(sources, nil) ++ xfades(sources, rendered), ";") outputs = Enum.map(variants(), fn %{index: idx} -> "[out#{idx}]" end) filter = filter <> ",split=#{Enum.count(outputs)}#{Enum.join(outputs)}" @@ -223,14 +221,15 @@ defmodule Video.Renderer do # uses the jsonblur frei0r plugin for the input videos (e.g. [0]) and outputs # them as blurs (e.g. [blur0]). Additionally it sets the timebase, see settb # for details. - defp blurs(sources) when is_list(sources) do + defp blurs(sources, prefix) when is_list(sources) do sources |> Enum.with_index() |> Parallel.map(2, fn {{path, from, _to, opts}, idx} -> detections = Video.Path.detections_rel_to_cwd(path) from = if from in [:start, :seamless], do: 0, else: Video.Timestamp.in_milliseconds(from) blur_frame_skip = blur_frame_skip(path, from) - "[#{idx}]frei0r=jsonblur:#{detections}|#{blur_frame_skip},#{vf(opts)}settb=AVTB[blur#{idx}]" + + "[#{idx}]frei0r=jsonblur:#{detections}|#{blur_frame_skip},#{prefix}#{vf(opts)}settb=AVTB[blur#{idx}]" end) end @@ -259,11 +258,11 @@ defmodule Video.Renderer do # sets the timebase for all input videos (e.g. [0]) and outputs them as blurs # (e.g. [blur0]). This is sometimes required or ffmpeg will fail with # "different timebase". - defp settb(sources) when is_list(sources) do + defp settb(sources, prefix) when is_list(sources) do sources |> Enum.with_index() |> Enum.map(fn {{_path, _from, _to, opts}, idx} -> - "[#{idx}]#{vf(opts)}settb=AVTB[blur#{idx}]" + "[#{idx}]#{prefix}#{vf(opts)}settb=AVTB[blur#{idx}]" end) end diff --git a/test/video/renderer_test.exs b/test/video/renderer_test.exs index 93574f58..e8a3bc0a 100644 --- a/test/video/renderer_test.exs +++ b/test/video/renderer_test.exs @@ -216,11 +216,9 @@ defmodule Video.RendererTest do "-i", "videos/source/2.mp4", "-filter_complex", - "[0]frei0r=jsonblur:videos/source/1.mp4.json.gz|1760,settb=AVTB[blur0];[1]frei0r=jsonblur:videos/source/2.mp4.json.gz|1106,settb=AVTB[blur1];[blur0][blur1]xfade=transition=fade:duration=0.26693333333333336:offset=0.7010666666666666", + "[0]frei0r=jsonblur:videos/source/1.mp4.json.gz|1760,scale=640:-1,settb=AVTB[blur0];[1]frei0r=jsonblur:videos/source/2.mp4.json.gz|1106,scale=640:-1,settb=AVTB[blur1];[blur0][blur1]xfade=transition=fade:duration=0.26693333333333336:offset=0.7010666666666666", "-pix_fmt", "yuv420p", - "-s", - "640x480", "-c:v", "libx264", "-preset",