Skip to content

Commit

Permalink
Check that FFmpeg exists on launch (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored Aug 18, 2024
1 parent 40ba45f commit ff4f3ec
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
32 changes: 32 additions & 0 deletions YoutubeDownloader.Core/Downloading/FFmpeg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace YoutubeDownloader.Core.Downloading;

public static class FFmpeg
{
public static string? TryGetCliFilePath()
{
static IEnumerable<string> GetProbeDirectoryPaths()
{
yield return AppContext.BaseDirectory;
yield return Directory.GetCurrentDirectory();

foreach (
var path in Environment.GetEnvironmentVariable("PATH")?.Split(Path.PathSeparator)
?? Enumerable.Empty<string>()
)
{
yield return path;
}
}

return GetProbeDirectoryPaths()
.Select(dirPath =>
Path.Combine(dirPath, OperatingSystem.IsWindows() ? "ffmpeg.exe" : "ffmpeg")
)
.FirstOrDefault(File.Exists);
}
}
1 change: 1 addition & 0 deletions YoutubeDownloader.Core/Downloading/VideoDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ await _youtube.Videos.DownloadAsync(
downloadOption.StreamInfos,
trackInfos,
new ConversionRequestBuilder(filePath)
.SetFFmpegPath(FFmpeg.TryGetCliFilePath() ?? "ffmpeg")
.SetContainer(downloadOption.Container)
.SetPreset(ConversionPreset.Medium)
.Build(),
Expand Down
2 changes: 1 addition & 1 deletion YoutubeDownloader/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace YoutubeDownloader.Services;
[INotifyPropertyChanged]
public partial class SettingsService()
: SettingsBase(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings.dat"),
Path.Combine(AppContext.BaseDirectory, "Settings.dat"),
SerializerContext.Default
)
{
Expand Down
26 changes: 26 additions & 0 deletions YoutubeDownloader/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Threading.Tasks;
using Avalonia;
using CommunityToolkit.Mvvm.Input;
using YoutubeDownloader.Core;
using YoutubeDownloader.Core.Downloading;
using YoutubeDownloader.Framework;
using YoutubeDownloader.Services;
using YoutubeDownloader.Utils;
Expand Down Expand Up @@ -71,6 +73,29 @@ private async Task ShowDevelopmentBuildMessageAsync()
ProcessEx.StartShellExecute(Program.ProjectReleasesUrl);
}

private async Task ShowFFmpegMessageAsync()
{
if (!string.IsNullOrWhiteSpace(FFmpeg.TryGetCliFilePath()))
return;

var dialog = viewModelManager.CreateMessageBoxViewModel(
"FFmpeg is missing",
$"""
FFmpeg is required for {Program.Name} to work. Please download it and make it available in the application directory or on the system PATH.
Click DOWNLOAD to go to the FFmpeg download page. You can also install FFmpeg using a package manager instead.
""",
"DOWNLOAD",
"CLOSE"
);

if (await dialogManager.ShowDialogAsync(dialog) == true)
ProcessEx.StartShellExecute("https://ffmpeg.org/download.html");

if (Application.Current?.ApplicationLifetime?.TryShutdown(3) != true)
Environment.Exit(3);
}

private async Task CheckForUpdatesAsync()
{
try
Expand Down Expand Up @@ -106,6 +131,7 @@ private async Task InitializeAsync()
{
await ShowUkraineSupportMessageAsync();
await ShowDevelopmentBuildMessageAsync();
await ShowFFmpegMessageAsync();
await CheckForUpdatesAsync();
}

Expand Down

0 comments on commit ff4f3ec

Please sign in to comment.