Skip to content

Commit

Permalink
Cleanup (#1116)
Browse files Browse the repository at this point in the history
* Make video finalization async

* Add length check to clip downloader

* Remove resolved TODO

* Log clean up failures

* DRY
  • Loading branch information
ScrubN authored Jun 29, 2024
1 parent cdb247b commit 78f9752
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 82 deletions.
11 changes: 1 addition & 10 deletions TwitchDownloaderCore/ChatDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,7 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
{
await Task.Delay(100, CancellationToken.None);

outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
try
{
await outputFs.DisposeAsync();
outputFileInfo.Delete();
}
catch { }
}
TwitchHelper.CleanUpClaimedFile(outputFileInfo, outputFs, _progress);

throw;
}
Expand Down
26 changes: 2 additions & 24 deletions TwitchDownloaderCore/ChatRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,8 @@ public async Task RenderVideoAsync(CancellationToken cancellationToken)
{
await Task.Delay(100, CancellationToken.None);

outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
try
{
await outputFs.DisposeAsync();
outputFileInfo.Delete();
}
catch { }
}

if (maskFileInfo is not null)
{
maskFileInfo.Refresh();
if (maskFileInfo.Exists && maskFileInfo.Length == 0)
{
try
{
await maskFs.DisposeAsync();
maskFileInfo.Delete();
}
catch { }
}
}
TwitchHelper.CleanUpClaimedFile(outputFileInfo, outputFs, _progress);
TwitchHelper.CleanUpClaimedFile(maskFileInfo, maskFs, _progress);

throw;
}
Expand Down
11 changes: 1 addition & 10 deletions TwitchDownloaderCore/ChatUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,7 @@ public async Task UpdateAsync(CancellationToken cancellationToken)
{
await Task.Delay(100, CancellationToken.None);

outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
try
{
await outputFs.DisposeAsync();
outputFileInfo.Delete();
}
catch { }
}
TwitchHelper.CleanUpClaimedFile(outputFileInfo, outputFs, _progress);

throw;
}
Expand Down
13 changes: 2 additions & 11 deletions TwitchDownloaderCore/ClipDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
{
await Task.Delay(100, CancellationToken.None);

outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
try
{
await outputFs.DisposeAsync();
outputFileInfo.Delete();
}
catch { }
}
TwitchHelper.CleanUpClaimedFile(outputFileInfo, outputFs, _progress);

throw;
}
Expand Down Expand Up @@ -98,7 +89,7 @@ private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputF
await EncodeClipWithMetadata(tempFile, outputFileInfo.FullName, clipInfo.data.clip, clipChapter, cancellationToken);

outputFileInfo.Refresh();
if (!outputFileInfo.Exists)
if (!outputFileInfo.Exists || outputFileInfo.Length == 0)
{
File.Move(tempFile, outputFileInfo.FullName);
_progress.LogError("Unable to serialize metadata. The download has been completed without custom metadata.");
Expand Down
14 changes: 3 additions & 11 deletions TwitchDownloaderCore/TsMerger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -40,16 +41,7 @@ public async Task MergeAsync(CancellationToken cancellationToken)
{
await Task.Delay(100, CancellationToken.None);

outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
try
{
await outputFs.DisposeAsync();
outputFileInfo.Delete();
}
catch { }
}
TwitchHelper.CleanUpClaimedFile(outputFileInfo, outputFs, _progress);

throw;
}
Expand Down
31 changes: 31 additions & 0 deletions TwitchDownloaderCore/TwitchHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand Down Expand Up @@ -899,6 +900,36 @@ public static FileInfo ClaimFile(string path, Func<FileInfo, FileInfo> fileAlrea
return fileInfo;
}

public static void CleanUpClaimedFile([AllowNull] FileInfo fileInfo, [AllowNull] FileStream fileStream, ITaskLogger logger)
{
if (fileInfo is null)
{
return;
}

fileInfo.Refresh();
if (fileInfo.Exists && fileInfo.Length == 0)
{
try
{
fileStream?.Dispose();
}
catch
{
// Ignored
}

try
{
fileInfo.Delete();
}
catch (Exception e)
{
logger.LogWarning($"Failed to clean up {fileInfo.FullName}: {e.Message}");
}
}
}

public static DirectoryInfo CreateDirectory(string path)
{
DirectoryInfo directoryInfo = Directory.CreateDirectory(path);
Expand Down
22 changes: 6 additions & 16 deletions TwitchDownloaderCore/VideoDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,7 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
{
await Task.Delay(100, CancellationToken.None);

outputFileInfo.Refresh();
if (outputFileInfo.Exists && outputFileInfo.Length == 0)
{
try
{
await outputFs.DisposeAsync();
outputFileInfo.Delete();
}
catch { }
}
TwitchHelper.CleanUpClaimedFile(outputFileInfo, outputFs, _progress);

throw;
}
Expand Down Expand Up @@ -128,7 +119,7 @@ await FfmpegMetadata.SerializeAsync(metadataPath, videoInfo.owner.displayName, d
var ffmpegRetries = 0;
do
{
ffmpegExitCode = await Task.Run(() => RunFfmpegVideoCopy(downloadFolder, outputFileInfo, concatListPath, metadataPath, startOffset, endDuration, videoLength), cancellationToken);
ffmpegExitCode = await RunFfmpegVideoCopy(downloadFolder, outputFileInfo, concatListPath, metadataPath, startOffset, endDuration, videoLength, cancellationToken);
if (ffmpegExitCode != 0)
{
_progress.LogError($"Failed to finalize video (code {ffmpegExitCode}), retrying in 10 seconds...");
Expand Down Expand Up @@ -326,7 +317,7 @@ private async Task VerifyDownloadedParts(ICollection<M3U8.Stream> playlist, Rang
}
}

private int RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string concatListPath, string metadataPath, decimal startOffset, decimal endDuration, TimeSpan videoLength)
private async Task<int> RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string concatListPath, string metadataPath, decimal startOffset, decimal endDuration, TimeSpan videoLength, CancellationToken cancellationToken)
{
using var process = new Process
{
Expand Down Expand Up @@ -357,7 +348,6 @@ private int RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string co
outputFile.FullName
};

// TODO: Make this optional - "Safe" and "Exact" trimming methods
if (endDuration > 0)
{
args.Insert(0, "-t");
Expand Down Expand Up @@ -391,14 +381,14 @@ private int RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string co
process.Start();
process.BeginErrorReadLine();

using var logWriter = File.AppendText(Path.Combine(tempFolder, "ffmpegLog.txt"));
await using var logWriter = File.AppendText(Path.Combine(tempFolder, "ffmpegLog.txt"));
logWriter.AutoFlush = true;
do // We cannot handle logging inside the ErrorDataReceived lambda because more than 1 can come in at once and cause a race condition. lay295#598
{
Thread.Sleep(100);
await Task.Delay(200, cancellationToken);
while (!logQueue.IsEmpty && logQueue.TryDequeue(out var logMessage))
{
logWriter.WriteLine(logMessage);
await logWriter.WriteLineAsync(logMessage);
}
} while (!process.HasExited || !logQueue.IsEmpty);

Expand Down

0 comments on commit 78f9752

Please sign in to comment.