Skip to content

Commit

Permalink
Don't remove all unfinished Chunks from all Queue Items if one Queue …
Browse files Browse the repository at this point in the history
…Item finishes #105
  • Loading branch information
Alkl58 committed May 12, 2022
1 parent 87c9dba commit 6e9ed1c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
35 changes: 25 additions & 10 deletions NotEnoughAV1Encodes/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ private void ButtonCancelEncode_Click(object sender, RoutedEventArgs e)
ButtonAddToQueue.IsEnabled = true;
ButtonRemoveSelectedQueueItem.IsEnabled = true;
ButtonEditSelectedItem.IsEnabled = true;
SaveQueueStates();

// To Do: Save Queue States when Cancelling
// Problem: Needs VideoChunks List
// Possible Implementation:
// - Use VideoChunks Functions from MainStartAsync()
// - Save VideoChunks inside QueueElement
//SaveQueueElementState();
}
catch { }
}
Expand Down Expand Up @@ -478,22 +484,31 @@ private void PreAddToQueue()
uid = null;
}

private void SaveQueueStates()
private void SaveQueueElementState(Queue.QueueElement queueElement, List<string> VideoChunks)
{
// Save / Override Queuefile to save Progress of Chunks
System.Windows.Controls.ItemCollection tempList = ListBoxQueue.Items;
foreach (Queue.QueueElement queueElement in tempList)

foreach (string chunkT in VideoChunks)
{
// Remove all unfinished Chunks
queueElement.ChunkProgress.RemoveAll(chunk => chunk.Finished != true);
// Get Index
int index = VideoChunks.IndexOf(chunkT);

try
// Already Encoded Status
bool alreadyEncoded = File.Exists(Path.Combine(Global.Temp, "NEAV1E", queueElement.UniqueIdentifier, "Video", index.ToString("D6") + "_finished.log"));

// Remove Chunk if not finished
if (!alreadyEncoded)
{
File.WriteAllText(Path.Combine(Global.AppData, "NEAV1E", "Queue", queueElement.VideoDB.InputFileName + "_" + queueElement.UniqueIdentifier + ".json"), JsonConvert.SerializeObject(queueElement, Formatting.Indented));
queueElement.ChunkProgress.RemoveAll(chunk => chunk.ChunkName == chunkT);
}
catch { }
}

try
{
File.WriteAllText(Path.Combine(Global.AppData, "NEAV1E", "Queue", queueElement.VideoDB.InputFileName + "_" + queueElement.UniqueIdentifier + ".json"), JsonConvert.SerializeObject(queueElement, Formatting.Indented));
}
catch { }

}

private void ButtonSavePreset_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -2000,7 +2015,7 @@ private async Task MainStartAsync(CancellationToken _cancelToken)
await Task.Run(() => DeleteTempFiles(queueElement, startTime), _cancelToken);
// Save Queue States (e.g. Chunk Progress)
SaveQueueStates();
SaveQueueElementState(queueElement, VideoChunks);
}
}
catch (TaskCanceledException) { }
Expand Down
1 change: 0 additions & 1 deletion NotEnoughAV1Encodes/Queue/ChunkProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public class ChunkProgress
{
public string ChunkName { get; set; }
public bool Finished { get; set; }
public long Progress { get; set; } = 0;
public long ProgressSecondPass { get; set; } = 0;
}
Expand Down
34 changes: 12 additions & 22 deletions NotEnoughAV1Encodes/Video/VideoEncode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@ public void Encode(int _workerCount, List<string> VideoChunks, Queue.QueueElemen
try
{
int index = VideoChunks.IndexOf(chunk);
bool alreadyEncoded = false;
Directory.CreateDirectory(Path.Combine(Global.Temp, "NEAV1E", queueElement.UniqueIdentifier, "Video"));
// Already Encoded Status
bool alreadyEncoded = File.Exists(Path.Combine(Global.Temp, "NEAV1E", queueElement.UniqueIdentifier, "Video", index.ToString("D6") + "_finished.log"));
// Read Finished Status
List<Queue.ChunkProgress> tempList = queueElement.ChunkProgress.ToList();
if (tempList.Any(n => n.ChunkName == chunk && n.Finished == true))
{
alreadyEncoded = true;
}
Directory.CreateDirectory(Path.Combine(Global.Temp, "NEAV1E", queueElement.UniqueIdentifier, "Video"));
// Checks if Output really exists
if (alreadyEncoded)
Expand Down Expand Up @@ -177,11 +172,15 @@ public void Encode(int _workerCount, List<string> VideoChunks, Queue.QueueElemen
Global.LaunchedPIDs.Add(_pid);
Global.Logger("TRACE - VideoEncode.Encode() => Added PID: " + _pid + " Chunk: " + chunk, queueElement.Output + ".log");
// Create Progress Object if does not exist
// Create Progress Object
Queue.ChunkProgress chunkProgress = new();
chunkProgress.ChunkName = chunk;
chunkProgress.Progress = 0;
List<Queue.ChunkProgress> tempList = queueElement.ChunkProgress.ToList();
if (!tempList.Any(n => n.ChunkName == chunk))
{
Queue.ChunkProgress chunkProgress = new();
chunkProgress.ChunkName = chunk;
queueElement.ChunkProgress.Add(chunkProgress);
}
Expand Down Expand Up @@ -284,17 +283,8 @@ public void Encode(int _workerCount, List<string> VideoChunks, Queue.QueueElemen
if (processVideo.ExitCode == 0 && token.IsCancellationRequested == false)
{
// Save Finished Status
foreach (Queue.ChunkProgress progressElement in queueElement.ChunkProgress.Where(p => p.ChunkName == chunk))
{
if (File.Exists(ChunkOutput))
{
progressElement.Finished = true;
}
else
{
Global.Logger("FATAL ?! - VideoEncode.Encode() => Exit Code: 0 - This shouldn't have happened at all. Chunk: " + chunk, queueElement.Output + ".log");
}
}
FileStream finishedLog = File.Create(Path.Combine(Global.Temp, "NEAV1E", queueElement.UniqueIdentifier, "Video", index.ToString("D6") + "_finished.log"));
finishedLog.Close();
Global.Logger("INFO - VideoEncode.Encode() => Exit Code: 0 Chunk: " + chunk, queueElement.Output + ".log");
}
Expand Down

0 comments on commit 6e9ed1c

Please sign in to comment.