feat: finish implementation of ffmpeg installation on linux
This requires testing on Windows before we can call it a full release.
This commit is contained in:
1 parent
4c9e22faf8
commit
3c7cfd23ac
10 files changed
+205
-57
No files matched your search
@@ -0,0 +1,10 @@
|
||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
||||
using PantherByte.ViewModels;
|
||||
|
||||
namespace PantherByte.Messages;
|
||||
|
||||
public class OpenNotificationDialogMessage(string title, string message)
|
||||
: AsyncRequestMessage<NotificationDialogViewModel?> {
|
||||
public readonly string Title = title;
|
||||
public readonly string Message = message;
|
||||
}
|
||||
@@ -4,6 +4,6 @@ using PantherByte.ViewModels;
|
||||
|
||||
namespace PantherByte.Messages;
|
||||
|
||||
public class OpenProgressDialogMessage(ProcessStartInfo info) : AsyncRequestMessage<ProgressWindowViewModel?> {
|
||||
public readonly ProcessStartInfo Info = info;
|
||||
public class OpenProgressDialogMessage(ProgressWindowViewModel vm) : AsyncRequestMessage<ProgressWindowViewModel?> {
|
||||
public readonly ProgressWindowViewModel ViewModel = vm;
|
||||
};
|
||||
@@ -4,6 +4,7 @@ using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
@@ -27,7 +28,7 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
/// <summary>
|
||||
/// The link to the video to download. For now, only use YouTube links.
|
||||
/// </summary>
|
||||
public string Link {
|
||||
public string Link {
|
||||
get => _link;
|
||||
set => this.RaiseAndSetIfChanged(ref _link, value);
|
||||
}
|
||||
@@ -45,7 +46,7 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
get => _availableFormats;
|
||||
set => this.RaiseAndSetIfChanged(ref _availableFormats, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The folder to save the files in. This will become the argument passed to the "-P" argument in
|
||||
/// <see cref="GenerateCmdArgsAsync"/>.
|
||||
@@ -54,7 +55,7 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
get => _saveLocation;
|
||||
set => this.RaiseAndSetIfChanged(ref _saveLocation, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Indicates if the file can be downloaded. If not, the download button is disabled.
|
||||
/// </summary>
|
||||
@@ -62,7 +63,7 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
get => _canDownload;
|
||||
set => this.RaiseAndSetIfChanged(ref _canDownload, value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructor. Mainly used for subscribing properties to the changes from other properties.
|
||||
/// </summary>
|
||||
@@ -78,7 +79,7 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
)
|
||||
.Subscribe(value => CanDownload = value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the save location for the generated file. We are only allowing users to download one file at a time for now.
|
||||
/// </summary>
|
||||
@@ -101,16 +102,20 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
if (Directory.Exists(tempPath)) {
|
||||
SaveLocation = tempPath;
|
||||
Console.WriteLine("Successfully set the file path.");
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new DirectoryNotFoundException($"Directory does not exist: {tempPath}");
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new NullReferenceException("The resulting storage folder was null.");
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new NullReferenceException("Missing a FileSaveService instance");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Exceptions are caught locally for now, but will eventually require a modal error window.
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
@@ -119,7 +124,6 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
/// <summary>
|
||||
/// Generates the arguments used for the main command string.
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private async Task<List<string>> GenerateCmdArgsAsync() {
|
||||
// The command string format to use: yt-dlp <link> -t <mp3|mp4|wav> -P <path> --no-playlist
|
||||
// yt-dlp requires usage of ffmpeg as well.
|
||||
@@ -130,7 +134,8 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
||||
Console.WriteLine("Using Windows program.");
|
||||
fileName = "Programs/yt-dlp-win64.exe";
|
||||
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
|
||||
Console.WriteLine("Using Linux program. May require additional run permissions.");
|
||||
fileName = "Programs/yt-dlp-linux";
|
||||
}
|
||||
@@ -140,14 +145,19 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
// WAV files require special command arguments
|
||||
if (Format.Equals("wav")) {
|
||||
args.AddRange(["--extract-audio", "--audio-format", Format]);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
args.AddRange(["-t", Format]);
|
||||
}
|
||||
|
||||
|
||||
return args;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if ffmpeg is installed.
|
||||
/// </summary>
|
||||
/// <returns>True if ffmpeg is installed, or false if not.</returns>
|
||||
private async Task<bool> CheckFfmpegInstalled() {
|
||||
return await Task.Run(async () => {
|
||||
ProcessStartInfo ffmpegInfo = new("ffmpeg") {
|
||||
@@ -160,13 +170,13 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
StartInfo = ffmpegInfo
|
||||
};
|
||||
|
||||
bool result = true;
|
||||
var result = true;
|
||||
|
||||
try {
|
||||
process.Start();
|
||||
await process.WaitForExitAsync();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
catch (Exception) {
|
||||
result = false;
|
||||
}
|
||||
|
||||
@@ -175,7 +185,9 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the download process of the file. Generates the command and runs it.
|
||||
/// Handles the download process of the file.
|
||||
///
|
||||
/// This command is also where the dependency check/installation is run from.
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private async Task DownloadFileAsync() {
|
||||
@@ -184,40 +196,88 @@ public partial class MainWindowViewModel : ViewModelBase {
|
||||
var args = await GenerateCmdArgsAsync();
|
||||
// Run the command.
|
||||
try {
|
||||
// ffmpeg installation check
|
||||
var installed = await CheckFfmpegInstalled();
|
||||
Console.WriteLine($"Is ffmpeg installed: {installed}");
|
||||
if (!installed) {
|
||||
SimpleDialogViewModel vm = new("Dependency ffmpeg was not found. Would you like to install it?",
|
||||
"Dependency Install");
|
||||
var result = await WeakReferenceMessenger.Default.Send(new OpenSimpleDialogMessage(vm));
|
||||
if (result is SimpleDialogViewModel.SimpleDialogResult.Yes) {
|
||||
// Install the program
|
||||
installed = await InstallFfmpegAsync();
|
||||
}
|
||||
}
|
||||
|
||||
// This means installation has failed or the user canceled it.
|
||||
if (!installed) return;
|
||||
|
||||
// The command that's run must work on at least Linux and Windows
|
||||
ProcessStartInfo info = new() {
|
||||
FileName = args[0],
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
CreateNoWindow = true,
|
||||
};
|
||||
info.ArgumentList.AddRange(args[1..]);
|
||||
|
||||
// Check that ffmpeg is installed.
|
||||
var temp = await CheckFfmpegInstalled();
|
||||
// if (!await CheckFfmpegInstalled()) {
|
||||
SimpleDialogViewModel vm = new("Dependency ffmpeg was not found. Would you like to install it?",
|
||||
"Dependency Install");
|
||||
var result = await WeakReferenceMessenger.Default.Send(new OpenSimpleDialogMessage(vm));
|
||||
if (result is not null) {
|
||||
Console.WriteLine($"Received result: {result}");
|
||||
}
|
||||
// }
|
||||
|
||||
|
||||
// This window isn't linked to the download process yet, but that is the plan.
|
||||
await OpenProgressWindowAsync(info);
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Begins the installation process for ffmpeg and displays a window.
|
||||
/// </summary>
|
||||
/// <returns>True if the installation was successful, or false if not.</returns>
|
||||
private async Task<bool> InstallFfmpegAsync() {
|
||||
ProcessStartInfo info = new() {
|
||||
CreateNoWindow = true,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
|
||||
var result = false;
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
||||
// Run: winget install "FFmpeg (Essentials Build)"
|
||||
info.Verb = "runas";
|
||||
info.FileName = "winget";
|
||||
info.Arguments = "install \"FFmpeg (Essentials Build)\"";
|
||||
|
||||
ProgressWindowViewModel vm = new(info) {
|
||||
StatusMessage = "Installing ffmpeg..."
|
||||
};
|
||||
await WeakReferenceMessenger.Default.Send(new OpenProgressDialogMessage(vm));
|
||||
|
||||
// If there was an error, return false. Otherwise, true.
|
||||
result = !vm.Error;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
|
||||
await WeakReferenceMessenger.Default.Send(new OpenNotificationDialogMessage(
|
||||
"Linux Install Error",
|
||||
"Linux users must install ffmpeg manually.\nThis may be developed further in the future."
|
||||
));
|
||||
|
||||
// Since this requires a manual install (we can't run sudo nicely) we'll just return false,
|
||||
// as we didn't actually install anything.
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a ProgressWindow as a modal dialog. I haven't decided if this window should run the command or if it
|
||||
/// should simply display the progress of a command that's running here.
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private async Task OpenProgressWindowAsync(ProcessStartInfo info) {
|
||||
await WeakReferenceMessenger.Default.Send(new OpenProgressDialogMessage(info));
|
||||
ProgressWindowViewModel vm = new(info);
|
||||
await WeakReferenceMessenger.Default.Send(new OpenProgressDialogMessage(vm));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace PantherByte.ViewModels;
|
||||
|
||||
public class NotificationDialogViewModel : ViewModelBase {
|
||||
public string Title { get; set; } = "Notification";
|
||||
public string Message { get; set; } = "Testing message";
|
||||
|
||||
public NotificationDialogViewModel() { }
|
||||
|
||||
public NotificationDialogViewModel(string title, string message) {
|
||||
(Title, Message) = (title, message);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public partial class ProgressWindowViewModel : ViewModelBase, IProgressWindowVie
|
||||
private bool _inProgress = false;
|
||||
private string _statusMessage = "Downloading video...";
|
||||
private string _barColor = "CornflowerBlue";
|
||||
|
||||
|
||||
public string ProcessStdOut {
|
||||
get => _processStdOut;
|
||||
set => this.RaiseAndSetIfChanged(ref _processStdOut, value);
|
||||
@@ -48,11 +48,11 @@ public partial class ProgressWindowViewModel : ViewModelBase, IProgressWindowVie
|
||||
public async Task RunProcessAsync() {
|
||||
Process downloadProcess = new();
|
||||
downloadProcess.StartInfo = _processStartInfo;
|
||||
|
||||
|
||||
downloadProcess.EnableRaisingEvents = true;
|
||||
downloadProcess.OutputDataReceived += (_, e) => {
|
||||
if (string.IsNullOrWhiteSpace(e.Data)) return;
|
||||
|
||||
|
||||
Console.WriteLine(e.Data);
|
||||
ProcessStdOut += e.Data + "\n";
|
||||
};
|
||||
@@ -77,7 +77,7 @@ public partial class ProgressWindowViewModel : ViewModelBase, IProgressWindowVie
|
||||
catch (Exception e) {
|
||||
Console.WriteLine(e.StackTrace);
|
||||
ProcessStdOut += "Error: " + e.StackTrace + "\n";
|
||||
StatusMessage = "Error while downloading video.";
|
||||
StatusMessage = "Error while performing download.";
|
||||
BarColor = "Red";
|
||||
Error = true;
|
||||
}
|
||||
@@ -90,4 +90,4 @@ public partial class ProgressWindowViewModel : ViewModelBase, IProgressWindowVie
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using PantherByte.Messages;
|
||||
using PantherByte.ViewModels;
|
||||
@@ -12,17 +13,26 @@ public partial class MainWindow : Window {
|
||||
WeakReferenceMessenger.Default.Register<MainWindow, OpenProgressDialogMessage>(this,
|
||||
static (w, m) => {
|
||||
var dialog = new ProgressWindow {
|
||||
DataContext = new ProgressWindowViewModel(m.Info)
|
||||
DataContext = m.ViewModel
|
||||
};
|
||||
m.Reply(dialog.ShowDialog<ProgressWindowViewModel?>(w));
|
||||
});
|
||||
|
||||
WeakReferenceMessenger.Default.Register<MainWindow, OpenSimpleDialogMessage>(this,
|
||||
static (w, m) => {
|
||||
static async (w, m) => {
|
||||
var dialog = new SimpleDialog {
|
||||
DataContext = m.ViewModel
|
||||
};
|
||||
m.Reply(dialog.ShowDialog<SimpleDialogViewModel.SimpleDialogResult?>(w));
|
||||
var response = dialog.ShowDialog<SimpleDialogViewModel.SimpleDialogResult?>(w);
|
||||
m.Reply(response);
|
||||
});
|
||||
|
||||
WeakReferenceMessenger.Default.Register<MainWindow, OpenNotificationDialogMessage>(this,
|
||||
static (w, m) => {
|
||||
var dialog = new NotificationDialog {
|
||||
DataContext = new NotificationDialogViewModel(m.Title, m.Message)
|
||||
};
|
||||
m.Reply(dialog.ShowDialog<NotificationDialogViewModel?>(w));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<Window xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="using:PantherByte.ViewModels"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="PantherByte.Views.NotificationDialog"
|
||||
x:DataType="vm:NotificationDialogViewModel"
|
||||
Title="{Binding Title}"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
SizeToContent="WidthAndHeight"
|
||||
Background="Gray"
|
||||
CanMaximize="False">
|
||||
<Design.DataContext>
|
||||
<vm:NotificationDialogViewModel />
|
||||
</Design.DataContext>
|
||||
|
||||
<StackPanel Spacing="20" Margin="10" HorizontalAlignment="Stretch">
|
||||
<TextBlock
|
||||
HorizontalAlignment="Center"
|
||||
Text="{Binding Message}" />
|
||||
<Button HorizontalAlignment="Center"
|
||||
HorizontalContentAlignment="Center"
|
||||
Content="OK"
|
||||
Padding="30 10"
|
||||
Click="OkButton_Click" />
|
||||
</StackPanel>
|
||||
</Window>
|
||||
@@ -0,0 +1,15 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Markup.Xaml;
|
||||
|
||||
namespace PantherByte.Views;
|
||||
|
||||
public partial class NotificationDialog : Window {
|
||||
public NotificationDialog() {
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void OkButton_Click(object? sender, RoutedEventArgs e) =>
|
||||
Close(DataContext);
|
||||
}
|
||||
@@ -11,39 +11,40 @@
|
||||
SizeToContent="WidthAndHeight"
|
||||
CanMaximize="False"
|
||||
WindowStartupLocation="CenterOwner">
|
||||
|
||||
|
||||
<StackPanel
|
||||
Width="700"
|
||||
Spacing="10">
|
||||
|
||||
|
||||
<StackPanel Spacing="10" Margin="10">
|
||||
<TextBlock
|
||||
HorizontalAlignment="Center"
|
||||
Text="{Binding StatusMessage}"/>
|
||||
Text="{Binding StatusMessage}" />
|
||||
<ProgressBar
|
||||
IsIndeterminate="{Binding InProgress}"
|
||||
Foreground="{Binding BarColor}"
|
||||
Maximum="100"
|
||||
Value="100"/>
|
||||
Value="100" />
|
||||
</StackPanel>
|
||||
|
||||
|
||||
<Expander VerticalAlignment="Top" HorizontalAlignment="Stretch"
|
||||
Margin="5"
|
||||
BorderThickness="0"
|
||||
Background="Transparent">
|
||||
Background="Transparent"
|
||||
x:Name="InfoExpander">
|
||||
<Expander.Header>More information...</Expander.Header>
|
||||
<Grid RowDefinitions="*" ColumnDefinitions="*">
|
||||
<ScrollViewer Grid.Row="0"
|
||||
x:Name="ScrollView"
|
||||
Height="100"
|
||||
HorizontalScrollBarVisibility="Auto"
|
||||
HorizontalScrollBarVisibility="Auto"
|
||||
VerticalScrollBarVisibility="Auto">
|
||||
<TextBlock x:Name="StdOutText"
|
||||
Text="{Binding ProcessStdOut}"
|
||||
TextWrapping="Wrap"/>
|
||||
TextWrapping="Wrap" />
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
</Expander>
|
||||
|
||||
|
||||
</StackPanel>
|
||||
</Window>
|
||||
</Window>
|
||||
@@ -1,3 +1,4 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using PantherByte.ViewModels;
|
||||
|
||||
@@ -6,13 +7,24 @@ namespace PantherByte.Views;
|
||||
public partial class ProgressWindow : Window {
|
||||
public ProgressWindow() {
|
||||
InitializeComponent();
|
||||
|
||||
// This feels a little fragile, but we'll leave it for now.
|
||||
|
||||
// Runs the specified process in the view model when the window is opened.
|
||||
Opened += async (_, _) => {
|
||||
if (DataContext is IProgressWindowViewModel vm)
|
||||
await vm.RunProcessAsync();
|
||||
};
|
||||
|
||||
// Automatically scrolls to the end of the logs when more information is added.
|
||||
StdOutText.SizeChanged += (_, _) => ScrollView.ScrollToEnd();
|
||||
|
||||
// Automatically resizes the window when the user collapses the expander.
|
||||
InfoExpander.Collapsing += (_, _) => {
|
||||
if (Content is not Control content) return;
|
||||
|
||||
content.Measure(Size.Infinity);
|
||||
var desired = content.DesiredSize;
|
||||
Width = desired.Width;
|
||||
Height = desired.Height;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user