Skip to content

Commit

Permalink
chore: Update ffmpeg filename version and refactor dependency handlin…
Browse files Browse the repository at this point in the history
…g and error handling

- Modify `ExternalProgram.cs`:
  - Added `Failed` status to `DependencyStatus` enum
  - Changed `FFmpegFileName` constant value to `ffmpeg-master-latest-win64-gpl-shared.zip`
  - Added a check for response nullability and failure in `DownloadYtdlp()` method
  - Added a check for response nullability and failure in `DownloadFFmpeg()` method
  - Added a try-catch block to handle exceptions in `UnpackFFmpeg()` method
- Modify `Form1.cs`:
  - Removed redundant check in `UpdateDependenciesAsync()` method
  - Added log and message box for finishing downloading all dependencies
  - Added status emoji for `DependencyStatus.Failed`

fix #6
  • Loading branch information
jim60105 committed Dec 24, 2023
1 parent 1132897 commit bab82bd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
32 changes: 28 additions & 4 deletions ExternalProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum DependencyStatus
Unknown,
NotExist,
Downloading,
Exist
Exist,
Failed
}

public static DependencyStatus Ytdlp_Status { get; private set; } = DependencyStatus.Unknown;
Expand All @@ -24,7 +25,7 @@ public enum DependencyStatus
public static string? FFmpegPath { get; private set; }

// https://github.com/yt-dlp/FFmpeg-Builds/releases/latest
private const string FFmpegFileName = "ffmpeg-n6.0-latest-win64-gpl-shared-6.0.zip";
private const string FFmpegFileName = "ffmpeg-master-latest-win64-gpl-shared.zip";

internal static async Task DownloadYtdlp()
{
Expand All @@ -37,6 +38,13 @@ internal static async Task DownloadYtdlp()
HttpResponseMessage response = await client.GetAsync(ytdlpUrl, HttpCompletionOption.ResponseHeadersRead);
Log.Debug("Get response from {YtdlpUrl}", ytdlpUrl);

if (response == null || !response.IsSuccessStatusCode)
{
Log.Fatal("Failed to get yt-dlp from {YtdlpUrl}!", ytdlpUrl);
Ytdlp_Status = DependencyStatus.Failed;
return;
}

string filePath = Path.Combine(YtdlpPath, "yt-dlp.exe");
File.Delete(filePath);

Expand All @@ -53,7 +61,8 @@ internal static async Task DownloadYtdlp()
else
{
Log.Fatal("Finished downloading yt-dlp but file not found in {filePath}!", filePath);
Ytdlp_Status = DependencyStatus.NotExist;
Ytdlp_Status = DependencyStatus.Failed;
return;
}
}

Expand All @@ -68,6 +77,13 @@ internal static async Task DownloadFFmpeg()
var response = await client.GetAsync(ffmpegUrl, HttpCompletionOption.ResponseHeadersRead);
Log.Debug("Get response from {FFmpegUrl}", ffmpegUrl);

if (response == null || !response.IsSuccessStatusCode)
{
Log.Fatal("Failed to get ffmpeg from {FFmpegUrl}!", ffmpegUrl);
Ytdlp_Status = DependencyStatus.Failed;
return;
}

string archivePath = Path.Combine(FFmpegPath, FFmpegFileName);
File.Delete(archivePath);

Expand Down Expand Up @@ -100,13 +116,21 @@ internal static async Task DownloadFFmpeg()
{
Log.Information("Complete FFmpeg unpacking to {filePath}", FFmpegPath);
FFmpeg_Status = DependencyStatus.Exist;
return;
}
else
{
Log.Fatal("Finished unpacking FFmpeg under {filePath} but couldn't find ffmpeg.exe!", FFmpegPath);
FFmpeg_Status = DependencyStatus.NotExist;
FFmpeg_Status = DependencyStatus.Failed;
return;
}
}
catch (Exception e)
{
Log.Fatal(e, "Failed to unpack FFmpeg!");
FFmpeg_Status = DependencyStatus.Failed;
return;
}
finally
{
File.Delete(archivePath);
Expand Down
22 changes: 19 additions & 3 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,24 @@ private async Task PrepareYtdlpAndFFmpegAsync(bool forceUpdate = false)
_ = UpdateDependenciesAsync(ytdlpPath, ffmpegPath, forceUpdate).ConfigureAwait(false);

// Update UI
while (FFmpeg_Status != DependencyStatus.Exist
|| Ytdlp_Status != DependencyStatus.Exist)
while (true)
{
if (FFmpeg_Status == DependencyStatus.Exist
&& Ytdlp_Status == DependencyStatus.Exist)
{
Log.Information("Finish downloading all dependencies.");
if (forceUpdate) MessageBox.Show("Finish downloading all dependencies.", "Finish");
break;
}

if (FFmpeg_Status == DependencyStatus.Failed
|| Ytdlp_Status == DependencyStatus.Failed)
{
Log.Fatal("!!!! Failed to download dependencies !!!!");
MessageBox.Show("Failed to download dependencies. Please check the log for detailed exceptions.", "Error!");
break;
}

panel_download.Visible = true;

await Task.Delay(TimeSpan.FromSeconds(1));
Expand All @@ -48,6 +63,7 @@ Ytdlp_Status switch
DependencyStatus.NotExist => "",
DependencyStatus.Downloading => "",
DependencyStatus.Exist => "✔️",
DependencyStatus.Failed => "😕",
_ => throw new NotImplementedException()
};
label_checking_ffmpeg.Text =
Expand All @@ -57,13 +73,13 @@ FFmpeg_Status switch
DependencyStatus.NotExist => "",
DependencyStatus.Downloading => "",
DependencyStatus.Exist => "✔️",
DependencyStatus.Failed => "😕",
_ => throw new NotImplementedException()
};

Application.DoEvents();
}

Log.Information("Finish downloading all dependencies.");
panel_download.Visible = false;
}

Expand Down
2 changes: 1 addition & 1 deletion Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
Expand Down

0 comments on commit bab82bd

Please sign in to comment.