2020-11-27 14:35:32 +01:00
using Flowframes.IO ;
using Flowframes.Magick ;
using Flowframes.Main ;
using Flowframes.OS ;
using System ;
using System.Collections.Generic ;
using System.Drawing ;
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
namespace Flowframes.UI
{
class MainUiFunctions
{
public static async Task InitInput ( TextBox outputTbox , TextBox inputTbox , TextBox fpsInTbox )
{
2020-12-02 01:09:41 +01:00
Program . mainForm . SetTab ( "interpolate" ) ;
2020-11-27 14:35:32 +01:00
outputTbox . Text = inputTbox . Text . Trim ( ) . GetParentDir ( ) ;
string path = inputTbox . Text . Trim ( ) ;
Program . lastInputPath = path ;
string fpsStr = "Not Found" ;
float fps = IOUtils . GetFpsFolderOrVideo ( path ) ;
if ( fps > 0 )
{
fpsStr = fps . ToString ( ) ;
fpsInTbox . Text = fpsStr ;
}
Interpolate . SetFps ( fps ) ;
Program . lastInputPathIsSsd = OSUtils . DriveIsSSD ( path ) ;
if ( ! Program . lastInputPathIsSsd )
Logger . Log ( "Your file seems to be on an HDD or USB device. It is recommended to interpolate videos on an SSD drive for best performance." ) ;
if ( IOUtils . IsPathDirectory ( path ) )
2020-12-07 00:41:07 +01:00
Logger . Log ( $"Video FPS (Loaded from fps.ini): {fpsStr} - Total Number Of Frames: {InterpolateUtils.GetInputFrameCount(path)}" ) ;
2020-11-27 14:35:32 +01:00
else
2020-12-07 00:41:07 +01:00
Logger . Log ( $"Video FPS: {fpsStr} - Total Number Of Frames: {InterpolateUtils.GetInputFrameCount(path)}" ) ;
2020-12-08 16:49:47 +01:00
CheckExistingFolder ( path , outputTbox . Text . Trim ( ) ) ;
2020-11-27 14:35:32 +01:00
await Task . Delay ( 10 ) ;
2020-12-01 16:54:12 +01:00
await PrintResolution ( path ) ;
2020-11-27 14:35:32 +01:00
MagickDedupe . ClearCache ( ) ;
await Task . Delay ( 10 ) ;
InterpolateUtils . SetPreviewImg ( await GetThumbnail ( path ) ) ;
}
2020-12-08 16:49:47 +01:00
static void CheckExistingFolder ( string inpath , string outpath )
{
if ( Config . GetInt ( "processingMode" ) = = 0 ) return ;
string tmpFolder = InterpolateUtils . GetTempFolderLoc ( inpath , outpath ) ;
if ( Directory . Exists ( tmpFolder ) )
{
int scnFrmAmount = IOUtils . GetAmountOfFiles ( Path . Combine ( tmpFolder , Paths . scenesDir ) , false , "*.png" ) ;
string scnFrames = scnFrmAmount > 0 ? $"{scnFrmAmount} scene frames" : "no scene frames" ;
int srcFrmAmount = IOUtils . GetAmountOfFiles ( Path . Combine ( tmpFolder , Paths . framesDir ) , false , "*.png" ) ;
string srcFrames = srcFrmAmount > 1 ? $"{srcFrmAmount} source frames" : "no source frames" ;
int interpFrmAmount = IOUtils . GetAmountOfFiles ( Path . Combine ( tmpFolder , Paths . interpDir ) , false ) ;
string interpFrames = interpFrmAmount > 2 ? $"{interpFrmAmount} interpolated frames" : "no interpolated frames" ;
string msg = $"A temporary folder for this video already exists. It contains {scnFrames}, {srcFrames}, {interpFrames}." ;
DialogResult dialogResult = MessageBox . Show ( $"{msg}\n\nClick \" Yes \ " to use the existing files or \"No\" to delete them." , "Use files from existing temp folder?" , MessageBoxButtons . YesNo ) ;
if ( dialogResult = = DialogResult . Yes )
{
return ;
}
else if ( dialogResult = = DialogResult . No )
{
IOUtils . TryDeleteIfExists ( tmpFolder ) ;
Logger . Log ( "Deleted old temp folder." ) ;
}
}
}
2020-12-01 16:54:12 +01:00
static async Task PrintResolution ( string path )
{
Size res = new Size ( ) ;
if ( ! IOUtils . IsPathDirectory ( path ) ) // If path is video
{
res = FFmpegCommands . GetSize ( path ) ;
}
else // Path is frame folder
{
Image thumb = await GetThumbnail ( path ) ;
res = new Size ( thumb . Width , thumb . Height ) ;
}
if ( res . Width > 1 & & res . Height > 1 )
Logger . Log ( $"Input Resolution: {res.Width}x{res.Height}" ) ;
}
static async Task < Image > GetThumbnail ( string path )
2020-11-27 14:35:32 +01:00
{
string imgOnDisk = Path . Combine ( Paths . GetDataPath ( ) , "thumb-temp.png" ) ;
try
{
2020-12-01 16:54:12 +01:00
if ( ! IOUtils . IsPathDirectory ( path ) ) // If path is video - Extract first frame
{
await FFmpegCommands . ExtractSingleFrame ( path , imgOnDisk , 1 , false , false ) ;
return IOUtils . GetImage ( imgOnDisk ) ;
}
else // Path is frame folder - Get first frame
{
return IOUtils . GetImage ( Directory . GetFiles ( path ) [ 0 ] ) ;
}
2020-11-27 14:35:32 +01:00
}
catch ( Exception e )
{
Logger . Log ( "GetThumbnail Error: " + e . Message , true ) ;
return null ;
}
}
}
}