Skip to content

Commit e393e60

Browse files
committed
fix: config preview edge cases
1 parent cece18e commit e393e60

3 files changed

Lines changed: 35 additions & 23 deletions

File tree

src/common/rendering/render.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
#include "render_pipeline.h"
44

55
namespace {
6-
size_t get_seek_start_frame(const std::filesystem::path& input_path, float seek) {
7-
auto video_info = u::get_video_info(input_path);
6+
constexpr int FRAMES_NEEDED_FOR_VSPIPE_TO_NOT_POO_ITSELF =
7+
3; // need some buffer of frames so something actually renders out. idk how to get a proper value for this.
8+
// @todo: make sure this doesnt ever fail (set config preview seek = 1 and try lots of diff vids and configs,
9+
// make sure preview never fails to render)
10+
11+
size_t get_seek_start_frame(const BlurSettings& settings, const u::VideoInfo& video_info, float seek) {
812
if (!video_info.has_video_stream || video_info.fps_num <= 0 || video_info.fps_den <= 0 ||
913
video_info.duration <= 0.f)
1014
return 0;
1115

16+
double ffmpeg_required_extra_time = 1.f / settings.blur_output_fps; // see -ss below
1217
double fps = static_cast<double>(video_info.fps_num) / video_info.fps_den;
13-
auto total_frames = static_cast<size_t>(video_info.duration * fps);
1418

15-
return std::min(
16-
static_cast<size_t>(std::clamp(seek, 0.f, 1.f) * total_frames), total_frames - 1
17-
); // cap at total_frames - 1 cause otherwise start will be end and vspipe will error (needs at least 1 frame)
19+
size_t total_frames = static_cast<size_t>(video_info.duration * fps);
20+
21+
size_t total_usable_frames = static_cast<size_t>(std::max(
22+
0.0, ((video_info.duration - ffmpeg_required_extra_time) * fps) - FRAMES_NEEDED_FOR_VSPIPE_TO_NOT_POO_ITSELF
23+
));
24+
25+
size_t start_frame = static_cast<size_t>(std::clamp(seek, 0.f, 1.f) * total_frames);
26+
size_t offset = total_frames > total_usable_frames ? total_frames - total_usable_frames : 0;
27+
28+
return offset < start_frame ? start_frame - offset : 0;
1829
}
1930
}
2031

@@ -35,14 +46,11 @@ tl::expected<rendering::FrameRenderResult, std::variant<std::string, rendering::
3546
if (!merged_settings)
3647
return tl::unexpected(merged_settings.error());
3748

38-
auto vspipe_args = detail::build_vspipe_base_args(input_path, *merged_settings);
49+
auto video_info = u::get_video_info(input_path);
3950

40-
if (seek > 0.f) {
41-
size_t start_frame = get_seek_start_frame(input_path, seek);
42-
if (start_frame > 0) {
43-
vspipe_args.insert(vspipe_args.end() - 2, { "-a", std::format("start={}", start_frame) });
44-
}
45-
}
51+
auto vspipe_args = detail::build_vspipe_video_args(
52+
input_path, *merged_settings, video_info, seek > 0.f ? get_seek_start_frame(settings, video_info, seek) : 0
53+
);
4654

4755
RenderCommands commands = {
4856
.vspipe_video = std::move(vspipe_args),
@@ -51,11 +59,12 @@ tl::expected<rendering::FrameRenderResult, std::variant<std::string, rendering::
5159
"-hide_banner",
5260
"-stats",
5361
"-i", "-",
62+
"-ss", std::to_string(1.f / settings.blur_output_fps), // skip ahead a frame, likely need a frame for frames to start being blended @todo: revisit if i ever change that behaviour (first frame of video not being blurred properly thing)
5463
"-map", "0:v",
55-
"-vframes", "1",
64+
"-frames:v", "1",
65+
"-c:v", "mjpeg",
5666
"-q:v", "2",
5767
"-f", "image2pipe",
58-
"-vcodec", "mjpeg",
5968
"-",
6069
},
6170
};

src/common/rendering/render_commands.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ std::vector<std::string> rendering::detail::build_vspipe_video_args(
199199
const std::filesystem::path& input_path,
200200
const nlohmann::json& merged_settings,
201201
const u::VideoInfo& video_info,
202-
size_t start_frame,
203-
size_t end_frame
202+
std::optional<size_t> start_frame,
203+
std::optional<size_t> end_frame
204204
) {
205205
auto args = build_vspipe_base_args(input_path, merged_settings);
206206
args.insert(
@@ -214,12 +214,15 @@ std::vector<std::string> rendering::detail::build_vspipe_video_args(
214214
"color_range=" + (video_info.color_range ? *video_info.color_range : "undefined"),
215215
"-a",
216216
std::format("preroll_frames={}", video_info.preroll_frames),
217-
"-a",
218-
std::format("start={}", start_frame),
219-
"-a",
220-
std::format("end={}", end_frame),
221217
}
222218
);
219+
220+
if (start_frame)
221+
args.insert(args.end(), { "-a", std::format("start={}", *start_frame) });
222+
223+
if (end_frame)
224+
args.insert(args.end(), { "-a", std::format("end={}", *end_frame) });
225+
223226
return args;
224227
}
225228

src/common/rendering/render_commands.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace rendering::detail {
2121
const std::filesystem::path& input_path,
2222
const nlohmann::json& merged_settings,
2323
const u::VideoInfo& video_info,
24-
size_t start_frame,
25-
size_t end_frame
24+
std::optional<size_t> start_frame = {},
25+
std::optional<size_t> end_frame = {}
2626
);
2727

2828
bool copies_audio(const BlurSettings& settings, const GlobalAppSettings& app_settings);

0 commit comments

Comments
 (0)