Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that FFmpeg exists on launch #485

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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