using System.IO.Compression; using System.Net; using System.Text.Json; using FFMpegCore.Extensions.Downloader.Enums; using FFMpegCore.Extensions.Downloader.Exceptions; using FFMpegCore.Extensions.Downloader.Extensions; using FFMpegCore.Extensions.Downloader.Models; namespace FFMpegCore.Extensions.Downloader.Services; /// /// Service to interact with ffbinaries.com API /// internal class FFbinariesService { /// /// Get version info from ffbinaries.com /// /// use to explicitly state the version of ffmpeg you want /// /// internal static async Task GetVersionInfo(FFMpegVersions version) { var versionUri = version.GetDescription(); HttpClient client = new(); var response = await client.GetAsync(versionUri); if (!response.IsSuccessStatusCode) { throw new FFMpegDownloaderException($"Failed to get version info from {versionUri}", "network error"); } var jsonString = await response.Content.ReadAsStringAsync(); var versionInfo = JsonSerializer.Deserialize(jsonString); return versionInfo ?? throw new FFMpegDownloaderException($"Failed to deserialize version info from {versionUri}", jsonString); } /// /// Download file from uri /// /// uri of the file /// internal static async Task DownloadFileAsSteam(Uri address) { var client = new HttpClient(); return await client.GetStreamAsync(address); } /// /// Extracts the binaries from the zip stream and saves them to the current binary folder /// /// steam of the zip file /// internal static IEnumerable ExtractZipAndSave(Stream zipStream) { using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read); List files = new(); foreach (var entry in archive.Entries) { if (entry.Name is "ffmpeg" or "ffmpeg.exe" or "ffprobe.exe" or "ffprobe" or "ffplay.exe" or "ffplay") { entry.ExtractToFile(Path.Combine(GlobalFFOptions.Current.BinaryFolder, entry.Name), true); files.Add(Path.Combine(GlobalFFOptions.Current.BinaryFolder, entry.Name)); } } return files; } }