Skip to content

Commit

Permalink
sped up blending for high fps inputs, fixed audio desync, added frame…
Browse files Browse the repository at this point in the history
…server support and timescale audio pitch shifting

also fixed triangle weighting
  • Loading branch information
f0e committed Jul 20, 2021
1 parent 1d0453c commit 5843689
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
43 changes: 28 additions & 15 deletions blur/avisynth_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@ void c_script_handler::create(const std::string& temp_path, const std::string& v
std::ofstream video_script(script_filename);
{
video_script << "from vapoursynth import core" << "\n";
video_script << "import vapoursynth as vs" << "\n";
video_script << "import havsfunc as haf" << "\n";
video_script << "import adjust" << "\n";
video_script << "import weighting" << "\n";

// load video
std::string chungus = video_path;
std::replace(chungus.begin(), chungus.end(), '\\', '/');
video_script << fmt::format("video = core.ffms2.Source(source=\"{}\", cache=False)", chungus) << "\n";

auto extension = std::filesystem::path(video_path).extension();
if (extension != ".avi") {
video_script << fmt::format("video = core.ffms2.Source(source=\"{}\", cache=False)", chungus) << "\n";
}
else {
// FFmpegSource2 doesnt work with frameserver
video_script << fmt::format("video = core.avisource.AVISource(\"{}\")", chungus) << "\n";
video_script << "video = core.fmtc.matrix(clip=video, mat=\"601\", col_fam=vs.YUV, bits=16)" << "\n";
video_script << "video = core.fmtc.resample(clip=video, css=\"420\")" << "\n";
video_script << "video = core.fmtc.bitdepth(clip=video, bits=8)" << "\n";
}

// input timescale
video_script << fmt::format("video = core.std.AssumeFPS(video, fpsnum=(video.fps * (1 / {})))", settings.input_timescale) << "\n";
if (settings.input_timescale != 1.f)
video_script << fmt::format("video = core.std.AssumeFPS(video, fpsnum=(video.fps * (1 / {})))", settings.input_timescale) << "\n";

// interpolation
if (settings.interpolate) {
Expand All @@ -35,33 +48,30 @@ void c_script_handler::create(const std::string& temp_path, const std::string& v
}

// output timescale
video_script << fmt::format("video = core.std.AssumeFPS(video, fpsnum=(video.fps * {}))", settings.output_timescale) << "\n";

// brightness
video_script << fmt::format("video = adjust.Tweak(video, bright={}, cont={}, sat={})", settings.brightness - 1.f, settings.contrast, settings.saturation) << "\n";

if (settings.output_timescale != 1.f)
video_script << fmt::format("video = core.std.AssumeFPS(video, fpsnum=(video.fps * {}))", settings.output_timescale) << "\n";

// blurring
if (settings.blur) {
video_script << fmt::format("frame_gap = int(video.fps / {})", settings.blur_output_fps) << "\n";
video_script << fmt::format("blended_frames = int(frame_gap * {})", settings.blur_amount) << "\n";

video_script << "if blended_frames > 0:" << "\n";

// number of weights must be odd
video_script << " if blended_frames % 2 == 0:" << "\n";
video_script << " blended_frames += 1" << "\n";

const std::unordered_map<std::string, std::function<std::string()>> weighting_functions = {
{ "equal", [&]() { return fmt::format("weighting.equal(blended_frames)"); }},
{ "gaussian", [&]() { return fmt::format("weighting.gaussian(blended_frames, {}, {})", settings.blur_weighting_gaussian_std_dev, settings.blur_weighting_bound); }},
{ "gaussian_sym", [&]() { return fmt::format("weighting.gaussianSym(blended_frames, {}, {})", settings.blur_weighting_gaussian_std_dev, settings.blur_weighting_bound); }},
{ "pyramid", [&]() { return fmt::format("weighting.pyramid(blended_frames, {})", settings.blur_weighting_triangle_reverse); }},
{ "pyramid", [&]() { return fmt::format("weighting.pyramid(blended_frames, {})", settings.blur_weighting_triangle_reverse ? "True" : "False"); }},
{ "pyramid_sym", [&]() { return "weighting.pyramid(blended_frames)"; }},
{ "custom_weight", [&]() { return fmt::format("weighting.divide(blended_frames, {})", settings.blur_weighting); }},
{ "custom_function", [&]() { return fmt::format("weighting.custom(blended_frames, '{}', {})", settings.blur_weighting, settings.blur_weighting_bound); }},
};

std::string weighting = settings.blur_weighting;
if (!weighting_functions.contains(weighting)) {
// check if it's a custom weighting function
Expand All @@ -70,20 +80,23 @@ void c_script_handler::create(const std::string& temp_path, const std::string& v
else
weighting = "custom_function";
}

video_script << " weights = " << weighting_functions.at(weighting)() << "\n";

// frame blend
// video_script << " video = core.misc.AverageFrames(video, [1] * blended_frames)" << "\n";
video_script << fmt::format(" video = core.frameblender.FrameBlend(video, weights, True)") << "\n";

video_script << "if frame_gap > 0:" << "\n";
video_script << " video = core.std.SelectEvery(video, cycle=frame_gap, offsets=0)" << "\n";
// video_script << "if frame_gap > 0:" << "\n";
// video_script << " video = core.std.SelectEvery(video, cycle=frame_gap, offsets=0)" << "\n";

// set exact fps
video_script << fmt::format("video = haf.ChangeFPS(video, {})", settings.blur_output_fps) << "\n";
}

// filters
video_script << fmt::format("video = adjust.Tweak(video, bright={}, cont={}, sat={})", settings.brightness - 1.f, settings.contrast, settings.saturation) << "\n";

video_script << "video.set_output()" << "\n";
}
video_script.close();
Expand Down
4 changes: 3 additions & 1 deletion blur/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void c_config::create(const std::string& filepath, s_blur_settings current_setti
output << "- timescale" << "\n";
output << "input timescale: " << current_settings.input_timescale << "\n";
output << "output timescale: " << current_settings.output_timescale << "\n";
output << "adjust timescaled audio pitch: " << (current_settings.output_timescale_audio_pitch ? "true" : "false") << "\n";

output << "\n";
output << "- rendering filters" << "\n";
Expand Down Expand Up @@ -131,7 +132,8 @@ s_blur_settings c_config::parse(const std::string& config_filepath, bool& first_

config_get("input timescale", settings.input_timescale);
config_get("output timescale", settings.output_timescale);

config_get("adjust timescaled audio pitch", settings.output_timescale_audio_pitch);

config_get("multithreading", settings.multithreading);
config_get("gpu", settings.gpu);
config_get("gpu type (nvidia/amd/intel)", settings.gpu_type);
Expand Down
1 change: 1 addition & 0 deletions blur/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct s_blur_settings {

float input_timescale = 1.f;
float output_timescale = 1.f;
bool output_timescale_audio_pitch = false;

float brightness = 1.f;
float saturation = 1.f;
Expand Down
11 changes: 8 additions & 3 deletions blur/rendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,18 @@ std::string c_render::build_ffmpeg_command() {
// timescale (todo: this isn't ideal still, check for a better option)
if (settings.input_timescale != 1.f) {
// asetrate: speed up and change pitch
audio_filters += fmt::format("asetrate=44100*{}", (1 / settings.input_timescale));
audio_filters += fmt::format("asetrate=48000*{}", (1 / settings.input_timescale));
}

if (settings.output_timescale != 1.f) {
if (audio_filters != "") audio_filters += ",";
// atempo: speed up without changing pitch
audio_filters += fmt::format("atempo={}", settings.output_timescale);
if (settings.output_timescale_audio_pitch) {
audio_filters += fmt::format("asetrate=48000*{}", settings.output_timescale);
}
else {
// atempo: speed up without changing pitch
audio_filters += fmt::format("atempo={}", settings.output_timescale);
}
}
}

Expand Down
29 changes: 9 additions & 20 deletions installer.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,16 @@ title Blur dependencies installer

:: -------------------------- Administrator access request --------------------------

:check_permissions
net session >nul 2>&1
if %errorLevel% == 0 (
goto got_admin
) else (
goto request_administrator
pause
exit
:: https://privacy.sexy — v0.10.2 — Tue, 20 Jul 2021 11:05:44 GMT
:: Ensure admin privileges
fltmc >nul 2>&1 || (
echo Administrator privileges are required.
PowerShell Start -Verb RunAs '%0' 2> nul || (
echo Right-click on the script and select "Run as administrator".
pause & exit 1
)

:request_administrator
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
exit /B

:got_admin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
exit 0
)

:: ------------------------------- Python installation ------------------------------

Expand Down

0 comments on commit 5843689

Please sign in to comment.