Skip to content

Commit

Permalink
speed up previews by scaling input(s) instead of output
Browse files Browse the repository at this point in the history
```
// 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
```
  • Loading branch information
breunigs committed Mar 26, 2024
1 parent 6f21a41 commit 7702037
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
19 changes: 9 additions & 10 deletions lib/video/renderer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -33,8 +34,6 @@ defmodule Video.Renderer do
filter,
"-pix_fmt",
"yuv420p",
"-s",
"640x480",
"-c:v",
"libx264",
"-preset",
Expand All @@ -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"] ++
Expand Down Expand Up @@ -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)}"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions test/video/renderer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7702037

Please sign in to comment.