2021-08-23 16:50:18 +02:00
using Flowframes.Media ;
using Flowframes.IO ;
using System ;
using System.IO ;
using System.Threading.Tasks ;
using Flowframes.MiscUtils ;
using System.Windows.Forms ;
2022-04-07 04:59:34 +02:00
using Flowframes.Ui ;
2021-08-23 16:50:18 +02:00
namespace Flowframes.Main
{
using static Interpolate ;
class InterpolateSteps
{
public static async Task Run ( string step )
{
Logger . Log ( $"[SBS] Running step '{step}'" , true ) ;
canceled = false ;
Program . mainForm . SetWorking ( true ) ;
if ( current = = null )
{
Logger . Log ( $"[SBS] Getting new current settings" , true ) ;
current = Program . mainForm . GetCurrentSettings ( ) ;
}
else
{
Logger . Log ( $"[SBS] Updating current settings" , true ) ;
current = Program . mainForm . UpdateCurrentSettings ( current ) ;
}
current . RefreshAlpha ( ) ;
current . stepByStep = true ;
2022-05-03 16:00:55 +02:00
if ( ! InterpolateUtils . InputIsValid ( current . inPath , current . outPath , current . inFps , current . interpFactor , current . outMode , current . tempFolder ) ) return ; // General input checks
2021-08-23 16:50:18 +02:00
if ( ! InterpolateUtils . CheckPathValid ( current . inPath ) ) return ; // Check if input path/file is valid
if ( step . Contains ( "Extract Frames" ) )
await ExtractFramesStep ( ) ;
if ( step . Contains ( "Run Interpolation" ) )
await InterpolateStep ( ) ;
if ( step . Contains ( "Export" ) )
await CreateOutputVid ( ) ;
if ( step . Contains ( "Cleanup" ) )
await Reset ( ) ;
Program . mainForm . SetWorking ( false ) ;
Program . mainForm . SetStatus ( "Done running step." ) ;
Logger . Log ( "Done running this step." ) ;
}
public static async Task ExtractFramesStep ( )
{
if ( ! ( await IoUtils . TryDeleteIfExistsAsync ( current . framesFolder ) ) )
{
2022-04-07 04:59:34 +02:00
UiUtils . ShowMessageBox ( "Failed to delete existing frames folder - Make sure no file is opened in another program!" , UiUtils . MessageType . Error ) ;
2021-08-23 16:50:18 +02:00
return ;
}
currentInputFrameCount = await GetFrameCountCached . GetFrameCountAsync ( current . inPath ) ;
await GetFrames ( ) ;
await PostProcessFrames ( true ) ;
}
public static async Task InterpolateStep ( )
{
2021-08-31 15:57:48 +02:00
if ( ! InterpolateUtils . CheckAiAvailable ( current . ai , current . model ) ) return ;
2021-08-23 16:50:18 +02:00
current . framesFolder = Path . Combine ( current . tempFolder , Paths . framesDir ) ;
if ( IoUtils . GetAmountOfFiles ( current . framesFolder , false , "*" ) < 2 )
{
if ( Config . GetBool ( Config . Key . sbsRunPreviousStepIfNeeded ) )
{
Logger . Log ( $"There are no extracted frames to interpolate - Running extract step first..." ) ;
await ExtractFramesStep ( ) ;
}
if ( IoUtils . GetAmountOfFiles ( current . framesFolder , false , "*" ) < 2 )
{
2022-04-07 04:59:34 +02:00
UiUtils . ShowMessageBox ( "There are no extracted frames that can be interpolated!\nDid you run the extraction step?" , UiUtils . MessageType . Error ) ;
2021-08-23 16:50:18 +02:00
return ;
}
}
if ( ! ( await IoUtils . TryDeleteIfExistsAsync ( current . interpFolder ) ) )
{
2022-04-07 04:59:34 +02:00
UiUtils . ShowMessageBox ( "Failed to delete existing frames folder - Make sure no file is opened in another program!" , UiUtils . MessageType . Error ) ;
2021-08-23 16:50:18 +02:00
return ;
}
currentInputFrameCount = await GetFrameCountCached . GetFrameCountAsync ( current . inPath ) ;
2022-07-19 21:54:25 +02:00
if ( Config . GetBool ( Config . Key . sbsAllowAutoEnc ) & & ! ( await InterpolateUtils . CheckEncoderValid ( current . outFps . GetFloat ( ) ) ) ) return ;
2021-08-23 16:50:18 +02:00
if ( canceled ) return ;
Program . mainForm . SetStatus ( "Running AI..." ) ;
await RunAi ( current . interpFolder , current . ai , true ) ;
await Task . Run ( async ( ) = > { await FrameRename . Unrename ( ) ; } ) ; // Get timestamps back
Program . mainForm . SetProgress ( 0 ) ;
}
public static async Task CreateOutputVid ( )
{
if ( IoUtils . GetAmountOfFiles ( current . interpFolder , false ) < 2 )
{
if ( Config . GetBool ( Config . Key . sbsRunPreviousStepIfNeeded ) )
{
Logger . Log ( $"There are no interpolated frames to export - Running interpolation step first..." ) ;
await InterpolateStep ( ) ;
}
if ( IoUtils . GetAmountOfFiles ( current . interpFolder , false ) < 2 )
{
Cancel ( $"There are no interpolated frames to encode!\n\nDid you delete the folder?" ) ;
return ;
}
}
2022-07-19 21:54:25 +02:00
if ( ! ( await InterpolateUtils . CheckEncoderValid ( current . outFps . GetFloat ( ) ) ) ) return ;
2021-08-23 16:50:18 +02:00
string [ ] outFrames = IoUtils . GetFilesSorted ( current . interpFolder , current . interpExt ) ;
if ( outFrames . Length > 0 & & ! IoUtils . CheckImageValid ( outFrames [ 0 ] ) )
{
2022-04-07 04:59:34 +02:00
UiUtils . ShowMessageBox ( "Invalid frame files detected!\n\nIf you used Auto-Encode, this is normal, and you don't need to run " +
"this step as the video was already created in the \"Interpolate\" step." , UiUtils . MessageType . Error ) ;
2021-08-23 16:50:18 +02:00
return ;
}
2021-09-12 14:23:35 +02:00
await Export . ExportFrames ( current . interpFolder , current . outPath , current . outMode , true ) ;
2021-08-23 16:50:18 +02:00
}
public static async Task Reset ( )
{
DialogResult dialog = MessageBox . Show ( $"Are you sure you want to remove all temporary files?" , "Are you sure?" , MessageBoxButtons . YesNo ) ;
if ( dialog = = DialogResult . Yes )
await Cleanup ( true ) ;
}
}
}