Skip to content

Commit

Permalink
use central processing script
Browse files Browse the repository at this point in the history
also don't create temp folders next to videos, use os temp path
  • Loading branch information
f0e committed Oct 6, 2023
1 parent 42fc022 commit 3ee294d
Show file tree
Hide file tree
Showing 15 changed files with 24,872 additions and 255 deletions.
24,596 changes: 24,596 additions & 0 deletions lib/nlohmann/json.hpp

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions src/blur.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import vapoursynth as vs
from vapoursynth import core

# from vsrife import RIFE

import adjust
import blending
import deduplicate
import interpolate
import weighting

import json
from pathlib import Path

video_path = Path(vars().get("video_path"))
settings = json.loads(vars().get("settings"))

extension = video_path.suffix
if extension != ".avi":
video = core.ffms2.Source(source=video_path, cache=False)
else:
# FFmpegSource2 doesnt work with frameserver
video = core.avisource.AVISource(video_path)
video = core.fmtc.matrix(clip=video, mat="601", col_fam=vs.YUV, bits=16)
video = core.fmtc.resample(clip=video, css="420")
video = core.fmtc.bitdepth(clip=video, bits=8)

# replace duplicate frames with new frames which are interpolated based off of the surrounding frames
if settings["deduplicate"]:
video = deduplicate.fill_drops_old(video, threshold=0.001)

# if settings["deduplicate"]:
# video = deduplicate.fill_drops(video, threshold=0.001, svp_preset=settings["interpolation_preset"],
# svp_algorithm=settings["interpolation_algorithm"], svp_masking=settings["interpolation_mask_area"],
# svp_gpu=settings["gpu_interpolation"])

# input timescale
if settings["input_timescale"] != 1:
video = core.std.AssumeFPS(video, fpsnum=(video.fps * (1 / settings["input_timescale"])))

# interpolation
if settings["interpolate"]:
# fix interpolated fps
interpolated_fps = settings["interpolated_fps"]
split = settings["interpolated_fps"].split("x")
if len(split) > 1:
# contains x, is a multiplier (e.g. 5x)
interpolated_fps = int(video.fps * split[0])
else:
# no x, is an fps (e.g. 600)
interpolated_fps = int(interpolated_fps)

match settings["interpolation_program"].lower():
case "rife":
pass # TODO
# video = core.resize.Bicubic(video, format=vs.RGBS)

# while video.fps < interpolated_fps:
# video = RIFE(video)

# video = core.resize.Bicubic(video, format=vs.YUV420P8, matrix_s="709")

case "rife-ncnn":
pass # TODO
# video = core.resize.Bicubic(video, format=vs.RGBS)

# while video.fps < interpolated_fps:
# video = core.rife.RIFE(video)

# video = core.resize.Bicubic(video, format=vs.YUV420P8, matrix_s="709")

case _:
if settings["manual_svp"]:
pass # TODO
# todo
# super = core.svp1.Super(video, settings["super_string"])
# vectors = core.svp1.Analyse(super['clip'], super['data'], video, settings["vectors_string"])

# # insert interpolated fps
# fixed_smooth_string = settings["smooth_string"]
# if "rate:" not in fixed_smooth_string:
# fixed_smooth_string.erase(0, 1);
# fixed_smooth_string.pop_back();
# fixed_smooth_string = "'{rate:{num:%d,abs:true}," + fixed_smooth_string + "}' % interpolated_fps";
# }
# else {
# # you're handling it i guess
# fixed_smooth_string = "'" + fixed_smooth_string + "'";
# }

# video_script << fmt::format("video = core.svp2.SmoothFps(video, super['clip'], super['data'], vectors['clip'], vectors['data'], {})", fixed_smooth_string) << "\n";
else:
print(f"new_fps={interpolated_fps}, preset={settings['interpolation_preset']}, algorithm={settings['interpolation_algorithm']}, blocksize={settings['interpolation_blocksize']}, overlap={0}, speed={settings['interpolation_speed']}, masking={settings['interpolation_mask_area']}, gpu={settings['gpu_interpolation']}")

video = interpolate.interpolate_svp(video, new_fps=interpolated_fps, preset=settings["interpolation_preset"],
algorithm=settings["interpolation_algorithm"], blocksize=settings["interpolation_blocksize"],
overlap=0, speed=settings["interpolation_speed"], masking=settings["interpolation_mask_area"], gpu=settings["gpu_interpolation"])

# output timescale
if settings["output_timescale"] != 1:
video = core.std.AssumeFPS(video, fpsnum=(video.fps * settings["output_timescale"]))

# blurring
if settings["blur"]:
if settings["blur_amount"] > 0:
frame_gap = int(video.fps / settings["blur_output_fps"])
blended_frames = int(frame_gap * settings["blur_amount"])

if blended_frames > 0:
# number of weights must be odd
if blended_frames % 2 == 0:
blended_frames += 1

def do_weighting_fn(blur_weighting_fn):
match blur_weighting_fn:
case "gaussian":
return weighting.gaussian(blended_frames, settings["blur_weighting_gaussian_std_dev"], settings["blur_weighting_bound"])

case "gaussian_sym":
return weighting.gaussianSym(blended_frames, settings["blur_weighting_gaussian_std_dev"], settings["blur_weighting_bound"])

case "pyramid":
return weighting.pyramid(blended_frames, settings["blur_weighting_triangle_reverse"])

case "pyramid_sym":
return weighting.pyramidSym(blended_frames)

case "custom_weight":
return weighting.divide(blended_frames, settings["blur_weighting"])

case "custom_function":
return weighting.custom(blended_frames, settings["blur_weighting"], settings["blur_weighting_bound"])

case "equal":
return weighting.equal(blended_frames)

case _:
# check if it's a custom weighting function
if blur_weighting_fn[0] == '[' and blur_weighting_fn[-1] == ']':
return do_weighting_fn("custom_weight")
else:
return do_weighting_fn("custom_function")

weights = do_weighting_fn(settings["blur_weighting"])

# frame blend
# video = core.misc.AverageFrames(video, [1] * blended_frames)
video = blending.average(video, weights)

# if frame_gap > 0:
# video = core.std.SelectEvery(video, cycle=frame_gap, offsets=0)

# set exact fps
video = interpolate.change_fps(video, settings["blur_output_fps"])

# filters
if settings["brightness"] != 1 or settings["contrast"] != 1 or settings["saturation"] != 1:
video = adjust.Tweak(video, bright=settings["brightness"] - 1, cont=settings["contrast"], sat=settings["saturation"])

video.set_output()
3 changes: 1 addition & 2 deletions src/cli/blur-cli.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,14 @@
<ClInclude Include="..\..\lib\imgui\imgui_internal.h" />
<ClInclude Include="..\..\lib\imgui\imstb_rectpack.h" />
<ClInclude Include="..\..\lib\imgui\imstb_textedit.h" />
<ClInclude Include="..\..\lib\nlohmann\json.hpp" />
<ClInclude Include="..\..\resources\resource.h" />
<ClInclude Include="..\common\blur.h" />
<ClInclude Include="..\common\config.h" />
<ClInclude Include="..\common\helpers.h" />
<ClInclude Include="..\common\pch.h" />
<ClInclude Include="..\common\preview.h" />
<ClInclude Include="..\common\rendering.h" />
<ClInclude Include="..\common\script_handler.h" />
<ClInclude Include="cli.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
Expand All @@ -264,7 +264,6 @@
<ClCompile Include="..\common\helpers.cpp" />
<ClCompile Include="..\common\preview.cpp" />
<ClCompile Include="..\common\rendering.cpp" />
<ClCompile Include="..\common\script_handler.cpp" />
<ClCompile Include="cli.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="pch.cpp">
Expand Down
12 changes: 6 additions & 6 deletions src/cli/blur-cli.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<Filter Include="..\..\lib\imgui">
<UniqueIdentifier>{DC4EE3A0-48DA-5065-51EB-D359BDF5AC65}</UniqueIdentifier>
</Filter>
<Filter Include="..\..\lib\nlohmann">
<UniqueIdentifier>{3C0052A7-28E4-12FD-9144-0D507D47FD7E}</UniqueIdentifier>
</Filter>
<Filter Include="..\..\resources">
<UniqueIdentifier>{56A38A5D-C22E-F821-CB3F-7B16374A5422}</UniqueIdentifier>
</Filter>
Expand Down Expand Up @@ -288,6 +291,9 @@
<ClInclude Include="..\..\lib\imgui\imstb_textedit.h">
<Filter>..\..\lib\imgui</Filter>
</ClInclude>
<ClInclude Include="..\..\lib\nlohmann\json.hpp">
<Filter>..\..\lib\nlohmann</Filter>
</ClInclude>
<ClInclude Include="..\..\resources\resource.h">
<Filter>..\..\resources</Filter>
</ClInclude>
Expand All @@ -309,9 +315,6 @@
<ClInclude Include="..\common\rendering.h">
<Filter>..\common</Filter>
</ClInclude>
<ClInclude Include="..\common\script_handler.h">
<Filter>..\common</Filter>
</ClInclude>
<ClInclude Include="cli.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
Expand All @@ -331,9 +334,6 @@
<ClCompile Include="..\common\rendering.cpp">
<Filter>..\common</Filter>
</ClCompile>
<ClCompile Include="..\common\script_handler.cpp">
<Filter>..\common</Filter>
</ClCompile>
<ClCompile Include="cli.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="pch.cpp" />
Expand Down
33 changes: 20 additions & 13 deletions src/common/blur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,33 @@ bool c_blur::initialise(bool _verbose, bool _using_preview) {
return true;
}

std::filesystem::path c_blur::create_temp_path(const std::filesystem::path& video_path) {
std::filesystem::path temp_path = video_path / "blur_temp";
bool c_blur::create_temp_path() {
if (!blur.temp_path.empty())
return true;

temp_path = std::filesystem::temp_directory_path() / "blur";

// check if the path already exists
if (!std::filesystem::exists(temp_path)) {
// try create the path
if (!std::filesystem::create_directory(temp_path))
throw std::exception("failed to create temporary path");
return false;
}

helpers::set_hidden(temp_path);
// also remove temp path on program exit
std::atexit([] {
blur.remove_temp_path();
});

return temp_path;
return true;
}

void c_blur::remove_temp_path(const std::filesystem::path& path) {
// check if the path doesn't already exist
if (!std::filesystem::exists(path))
return;
bool c_blur::remove_temp_path() {
if (blur.temp_path.empty())
return true;

// remove the path and all the files inside
std::filesystem::remove_all(path);
if (!std::filesystem::exists(blur.temp_path))
return true;

std::filesystem::remove_all(blur.temp_path);

return true;
}
12 changes: 5 additions & 7 deletions src/common/blur.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@

class c_blur {
public:
const std::string BLUR_VERSION = "1.9";
const std::string BLUR_VERSION = "1.93";

bool verbose = true;
bool using_preview = false;

std::filesystem::path path;
bool used_installer = false;

private:
std::vector<std::string> get_files(int& argc, char* argv[]);

public:
std::filesystem::path create_temp_path(const std::filesystem::path& video_path);
void remove_temp_path(const std::filesystem::path& path);
std::filesystem::path temp_path;

public:
bool initialise(bool _verbose, bool _using_preview);

bool create_temp_path();
bool remove_temp_path();
};

inline c_blur blur;
66 changes: 55 additions & 11 deletions src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ s_blur_settings c_config::parse(const std::filesystem::path& config_filepath) {
ss >> std::boolalpha >> out; // boolalpha: enable true/false bool parsing
}
catch (const std::exception&) {
throw std::exception(fmt::format("failed to parse config variable '{}' (value: {})", var, config[var]).c_str());
helpers::debug_log("failed to parse config variable '{}' (value: {})", var, config[var]);
return;
}
};

Expand Down Expand Up @@ -171,17 +172,10 @@ s_blur_settings c_config::parse(const std::filesystem::path& config_filepath) {

// config_get_str("interpolation program (svp/rife/rife-ncnn)", settings.interpolation_program);
config_get_str("interpolation preset", settings.interpolation_preset);
config_get_str("interpolation algorithm", settings.interpolation_algorithm);
config_get("interpolation algorithm", settings.interpolation_algorithm);
config_get("interpolation block size", settings.interpolation_blocksize);
config_get_str("interpolation speed", settings.interpolation_speed);
config_get_str("interpolation mask area", settings.interpolation_mask_area);

// fix old configs (Im So Lazy)
#define FIX_OLD(setting) \
if (settings.setting == "default") \
settings.setting = default_settings.setting

FIX_OLD(interpolation_algorithm);
config_get("interpolation mask area", settings.interpolation_mask_area);

config_get("manual svp", settings.manual_svp);
config_get_str("super string", settings.super_string);
Expand Down Expand Up @@ -219,4 +213,54 @@ s_blur_settings c_config::get_config(const std::filesystem::path& config_filepat
}

return parse(cfg_path);
}
}

nlohmann::json s_blur_settings::to_json() {
nlohmann::json j;

j["blur"] = this->blur;
j["blur_amount"] = this->blur_amount;
j["blur_output_fps"] = this->blur_output_fps;
j["blur_weighting"] = this->blur_weighting;

j["interpolate"] = this->interpolate;
j["interpolated_fps"] = this->interpolated_fps;

j["input_timescale"] = this->input_timescale;
j["output_timescale"] = this->output_timescale;
j["output_timescale_audio_pitch"] = this->output_timescale_audio_pitch;

j["brightness"] = this->brightness;
j["saturation"] = this->saturation;
j["contrast"] = this->contrast;

j["quality"] = this->quality;
j["deduplicate"] = this->deduplicate;
j["preview"] = this->preview;
j["detailed_filenames"] = this->detailed_filenames;

j["gpu_interpolation"] = this->gpu_interpolation;
j["gpu_rendering"] = this->gpu_rendering;
j["gpu_type"] = this->gpu_type;
j["video_container"] = this->video_container;
j["ffmpeg_override"] = this->ffmpeg_override;
j["debug"] = this->debug;

j["blur_weighting_gaussian_std_dev"] = this->blur_weighting_gaussian_std_dev;
j["blur_weighting_triangle_reverse"] = this->blur_weighting_triangle_reverse;
j["blur_weighting_bound"] = this->blur_weighting_bound;

j["interpolation_program"] = this->interpolation_program;
j["interpolation_preset"] = this->interpolation_preset;
j["interpolation_algorithm"] = this->interpolation_algorithm;
j["interpolation_blocksize"] = this->interpolation_blocksize;
j["interpolation_speed"] = this->interpolation_speed;
j["interpolation_mask_area"] = this->interpolation_mask_area;

j["manual_svp"] = this->manual_svp;
j["super_string"] = this->super_string;
j["vectors_string"] = this->vectors_string;
j["smooth_string"] = this->smooth_string;

return j;
}
Loading

0 comments on commit 3ee294d

Please sign in to comment.