Skip to content

Commit

Permalink
#240 sample ETA from fps
Browse files Browse the repository at this point in the history
  • Loading branch information
Anime4000 committed Jul 7, 2024
1 parent 0086297 commit 3e5cfac
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions IFME/ProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ internal class ProcessManager
internal static bool IsPause = false;

private TimeSpan eta = new TimeSpan(0, 0, 0);
private List<Tuple<int, DateTime>> recentFrames;
private List<double> recentFps;
private const int sampleSize = 5;

public ProcessManager()
{
recentFrames = new List<Tuple<int, DateTime>>();
recentFps = new List<double>();
}

internal static int Start(string Command)
Expand Down Expand Up @@ -132,40 +132,51 @@ private void Proc_DataReceived(object sender, DataReceivedEventArgs e)

double percentage = (double)frame / MediaEncoding.RealFrameCount * 100;

// Update recent frames list
DateTime currentTime = DateTime.Now;
recentFrames.Add(new Tuple<int, DateTime>(frame, currentTime));
if (recentFrames.Count > sampleSize)
// Update recent fps list
recentFps.Add(speed);
if (recentFps.Count > sampleSize)
{
recentFrames.RemoveAt(0);
recentFps.RemoveAt(0);
}

// Calculate ETA
if (recentFrames.Count == sampleSize)
if (recentFps.Count == sampleSize)
{
double totalFrameTime = 0;
for (int i = 1; i < recentFrames.Count; i++)
double averageFps = 0;
foreach (var fps in recentFps)
{
int frameDiff = recentFrames[i].Item1 - recentFrames[i - 1].Item1;
TimeSpan timeDiff = recentFrames[i].Item2 - recentFrames[i - 1].Item2;
totalFrameTime += timeDiff.TotalSeconds / frameDiff;
averageFps += fps;
}
double averageFrameTime = totalFrameTime / (sampleSize - 1);
int remainingFrames = MediaEncoding.RealFrameCount - frame;
double remainingTime = remainingFrames * averageFrameTime;

try
{
eta = TimeSpan.FromSeconds(remainingTime);
}
catch (Exception ex)
{
frmMain.PrintLog($"[WARN] ETA Logic is crashed: {ex.Message}");
averageFps /= sampleSize;
averageFps = Math.Round(averageFps); // Round to the nearest whole number

if (averageFps > 0)
{
int remainingFrames = MediaEncoding.RealFrameCount - frame;
double remainingTime = remainingFrames / averageFps;

if (remainingTime > TimeSpan.MaxValue.TotalSeconds)
{
remainingTime = TimeSpan.MaxValue.TotalSeconds;
}
else if (remainingTime < TimeSpan.MinValue.TotalSeconds)
{
remainingTime = TimeSpan.MinValue.TotalSeconds;
}

try
{
eta = TimeSpan.FromSeconds(remainingTime);
}
catch (Exception ex)
{
frmMain.PrintLog($"[WARN] ETA Logic is crashed: {ex.Message}");

}
}
}

frmMain.PrintProgress($"[{percentage:0.0} %] Frame: {frame}, Bitrate: {bitrate} kb/s, Speed: {speed} fps, ETA: {eta:hh\\:mm\\:ss}");
frmMain.PrintProgress($"[{percentage:0.00} %] Frame: {frame}, Bitrate: {bitrate:0} kb/s, Speed: {speed:0.00} fps, ETA: {eta:hh\\:mm\\:ss}");

return;
}
Expand Down

0 comments on commit 3e5cfac

Please sign in to comment.