Files
MercuryConverter/src/Utility/Utils.cs
T

92 lines
2.8 KiB
C#
Raw Normal View History

2025-08-27 12:44:57 -07:00
using System;
using System.Collections.Generic;
2025-08-27 16:11:06 -07:00
using System.IO;
2025-08-27 12:44:57 -07:00
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
2025-08-27 16:11:06 -07:00
using Avalonia.Platform;
2025-08-27 12:44:57 -07:00
using Avalonia.Platform.Storage;
using Avalonia.Threading;
2025-08-27 23:03:30 -07:00
using FFMpegCore;
using FFMpegCore.Arguments;
2025-08-31 23:36:35 -07:00
using Instances.Exceptions;
2025-08-27 12:44:57 -07:00
using MercuryConverter.UI;
namespace MercuryConverter.Utility;
public static class Utils
{
public static string CoreCount => Environment.ProcessorCount.ToString();
public static async Task<string> BeginDirSelection(string title, string? startDir = null)
{
IReadOnlyList<IStorageFolder>? dirSelection = null;
await Dispatcher.UIThread.Invoke(async () =>
{
await Task.Delay(250);
var tl = TopLevel.GetTopLevel(MainWindow.Instance)!;
dirSelection = await tl.StorageProvider.OpenFolderPickerAsync
(
new FolderPickerOpenOptions
{
Title = title,
AllowMultiple = false,
SuggestedStartLocation = startDir == null ? null : await tl.StorageProvider.TryGetFolderFromPathAsync(startDir),
}
);
});
if (dirSelection!.Count <= 0)
{
return "";
}
return dirSelection!.First().TryGetLocalPath()!;
}
2025-08-27 16:11:06 -07:00
/// <summary>
/// Get an AvaloniaResource asset.
/// </summary>
/// <param name="path">Forward-slash (/)-separated path to asset.</param>
/// <returns></returns>
public static Stream AssetPath(string path) => AssetLoader.Open(new Uri("avares://MercuryConverter/Assets/" + path));
2025-08-27 23:03:30 -07:00
public static string IIDToMusicFilePath(uint id)
{
return $"MER_BGM_S{id / 1000:D2}_{id % 1000:D3}";
}
2025-08-31 23:36:35 -07:00
private static bool? _ffmpegAvailable = null;
public static bool FFMpegAvailable
2025-08-27 23:03:30 -07:00
{
2025-08-31 23:36:35 -07:00
get
{
if (_ffmpegAvailable == null)
{
var testVidPath = Path.Combine(
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!,
"ic.mp4"
);
try
{
FFMpegArguments
.FromFileInput(testVidPath)
.OutputToFile("-", true, options => options.WithArgument(new ForceFormatArgument("null")))
.ProcessSynchronously();
2025-08-27 23:03:30 -07:00
2025-08-31 23:36:35 -07:00
_ffmpegAvailable = true;
}
catch (InstanceFileNotFoundException)
{
Console.WriteLine($"Could not find FFmpeg on PATH!");
_ffmpegAvailable = false;
}
Console.WriteLine($"FFmpeg available: {_ffmpegAvailable}");
}
return (bool)_ffmpegAvailable!;
}
2025-08-27 23:03:30 -07:00
}
2025-08-27 12:44:57 -07:00
}