2020-12-02 17:23:24 +01:00
using Flowframes.Data ;
using Flowframes.IO ;
2020-11-25 12:40:17 +01:00
using Flowframes.UI ;
using System ;
2020-11-24 02:23:19 +01:00
using System.Collections.Generic ;
2020-11-25 12:40:17 +01:00
using System.Diagnostics ;
2020-11-30 20:32:33 +01:00
using System.Globalization ;
2020-11-24 02:23:19 +01:00
using System.IO ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace Flowframes.Main
{
class VfrDedupe
{
2020-12-04 16:53:39 +01:00
public static async Task CreateTimecodeFiles ( string framesPath , bool loopEnabled , int times , bool noTimestamps )
2020-11-29 16:10:31 +01:00
{
Logger . Log ( "Generating timecodes..." ) ;
2020-12-07 10:48:50 +01:00
await CreateTimecodeFile ( framesPath , loopEnabled , times , false , noTimestamps ) ;
2020-11-29 16:10:31 +01:00
Logger . Log ( $"Generating timecodes... Done." , false , true ) ;
}
2020-12-04 16:53:39 +01:00
public static async Task CreateTimecodeFile ( string framesPath , bool loopEnabled , int interpFactor , bool notFirstRun , bool noTimestamps )
2020-11-24 02:23:19 +01:00
{
2020-11-30 20:32:33 +01:00
if ( Interpolate . canceled ) return ;
Logger . Log ( $"Generating timecodes for {interpFactor}x..." , false , true ) ;
2020-12-03 01:20:33 +01:00
if ( noTimestamps )
Logger . Log ( "Timestamps are disabled, using static frame rate." ) ;
2020-11-26 20:17:18 +01:00
bool sceneDetection = true ;
2020-11-25 12:40:17 +01:00
2020-12-07 10:48:50 +01:00
FileInfo [ ] frameFiles = new DirectoryInfo ( framesPath ) . GetFiles ( "*.png" ) ;
string vfrFile = Path . Combine ( framesPath . GetParentDir ( ) , $"vfr-{interpFactor}x.ini" ) ;
2020-11-25 12:40:17 +01:00
string fileContent = "" ;
2020-11-30 20:32:33 +01:00
string scnFramesPath = Path . Combine ( framesPath . GetParentDir ( ) , Paths . scenesDir ) ;
2020-12-02 14:27:41 +01:00
string interpPath = Paths . interpDir ;
2020-11-30 20:32:33 +01:00
2020-12-01 16:54:12 +01:00
List < string > sceneFrames = new List < string > ( ) ;
if ( Directory . Exists ( scnFramesPath ) )
sceneFrames = Directory . GetFiles ( scnFramesPath ) . Select ( file = > Path . GetFileName ( file ) ) . ToList ( ) ;
2020-11-24 02:23:19 +01:00
int lastFrameDuration = 1 ;
// Calculate time duration between frames
int totalFileCount = 1 ;
for ( int i = 0 ; i < ( frameFiles . Length - 1 ) ; i + + )
{
2020-11-25 14:04:31 +01:00
if ( Interpolate . canceled ) return ;
2020-11-25 12:40:17 +01:00
2020-12-02 14:27:41 +01:00
int durationTotal = 100 ; // Default for no timestamps in input filenames (divided by output fps later)
2020-11-25 12:40:17 +01:00
2020-12-02 14:27:41 +01:00
if ( ! noTimestamps ) // Get timings from frame filenames
{
string filename1 = frameFiles [ i ] . Name ;
string filename2 = frameFiles [ i + 1 ] . Name ;
durationTotal = Path . GetFileNameWithoutExtension ( filename2 ) . GetInt ( ) - Path . GetFileNameWithoutExtension ( filename1 ) . GetInt ( ) ;
}
2020-11-26 20:17:18 +01:00
2020-11-24 02:23:19 +01:00
lastFrameDuration = durationTotal ;
float durationPerInterpFrame = ( float ) durationTotal / interpFactor ;
2020-11-25 12:40:17 +01:00
2020-11-26 20:17:18 +01:00
int interpFramesAmount = interpFactor ;
2020-11-30 20:32:33 +01:00
bool discardThisFrame = ( sceneDetection & & ( i + 2 ) < frameFiles . Length & & sceneFrames . Contains ( frameFiles [ i + 1 ] . Name ) ) ; // i+2 is in scene detection folder, means i+1 is ugly interp frame
2020-11-26 20:17:18 +01:00
2020-11-24 02:23:19 +01:00
// If loop is enabled, account for the extra frame added to the end for loop continuity
if ( loopEnabled & & i = = ( frameFiles . Length - 2 ) )
2020-11-26 20:17:18 +01:00
interpFramesAmount = interpFramesAmount * 2 ;
2020-11-24 02:23:19 +01:00
2020-12-06 18:49:53 +01:00
// Logger.Log("Writing out frames for in frame " + i);
2020-11-24 02:23:19 +01:00
// Generate frames file lines
2020-11-26 20:17:18 +01:00
for ( int frm = 0 ; frm < interpFramesAmount ; frm + + )
2020-11-24 02:23:19 +01:00
{
2020-12-06 18:49:53 +01:00
// Logger.Log($"Writing out frame {frm+1}/{interpFramesAmount}", true);
2020-11-30 20:32:33 +01:00
string durationStr = ( ( durationPerInterpFrame / 1000f ) * 1 ) . ToString ( "0.00000" , CultureInfo . InvariantCulture ) ;
2020-11-26 20:17:18 +01:00
2020-12-04 16:53:39 +01:00
if ( discardThisFrame & & totalFileCount > 1 ) // Never discard 1st frame
2020-11-26 20:17:18 +01:00
{
2020-12-06 18:49:53 +01:00
int lastNum = totalFileCount ;
// Logger.Log($"Writing frame {totalFileCount} [Discarding Next]", true);
fileContent + = $"file '{interpPath}/{totalFileCount.ToString().PadLeft(Padding.interpFrames, '0')}.png'\nduration {durationStr}\n" ;
totalFileCount + + ;
// Logger.Log("Discarding interp frames with out num " + totalFileCount);
2020-11-29 16:10:31 +01:00
for ( int dupeCount = 1 ; dupeCount < interpFramesAmount ; dupeCount + + )
2020-11-26 20:17:18 +01:00
{
2020-12-06 18:49:53 +01:00
// Logger.Log($"Writing frame {totalFileCount} which is actually repeated frame {lastNum}");
2020-12-02 23:11:27 +01:00
fileContent + = $"file '{interpPath}/{lastNum.ToString().PadLeft(Padding.interpFrames, '0')}.png'\nduration {durationStr}\n" ;
2020-11-26 20:17:18 +01:00
totalFileCount + + ;
}
2020-12-06 18:49:53 +01:00
frm = interpFramesAmount ;
}
else
{
// Logger.Log($"Writing frame {totalFileCount}", true, false);
fileContent + = $"file '{interpPath}/{totalFileCount.ToString().PadLeft(Padding.interpFrames, '0')}.png'\nduration {durationStr}\n" ;
totalFileCount + + ;
}
2020-11-24 02:23:19 +01:00
}
2020-11-25 12:40:17 +01:00
2020-11-29 16:10:31 +01:00
if ( ( i + 1 ) % 100 = = 0 )
2020-11-25 12:40:17 +01:00
await Task . Delay ( 1 ) ;
}
File . WriteAllText ( vfrFile , fileContent ) ;
2020-11-29 16:10:31 +01:00
2020-12-02 00:44:43 +01:00
if ( notFirstRun ) return ; // Skip all steps that only need to be done once
2020-11-25 12:40:17 +01:00
if ( Config . GetBool ( "enableLoop" ) )
{
int lastFileNumber = frameFiles . Last ( ) . Name . GetInt ( ) ;
lastFileNumber + = lastFrameDuration ;
2020-12-02 17:23:24 +01:00
string loopFrameTargetPath = Path . Combine ( frameFiles . First ( ) . FullName . GetParentDir ( ) , lastFileNumber . ToString ( ) . PadLeft ( Padding . inputFrames , '0' ) + ".png" ) ;
2020-11-29 16:10:31 +01:00
if ( File . Exists ( loopFrameTargetPath ) )
return ;
File . Copy ( frameFiles . First ( ) . FullName , loopFrameTargetPath ) ;
2020-11-24 02:23:19 +01:00
}
}
}
}