From 9db4ba75b45a2a55da2feabf973a459865d1acb7 Mon Sep 17 00:00:00 2001 From: Kerry Cao Date: Tue, 11 Apr 2023 21:55:30 -0600 Subject: [PATCH] features to auto download ffmpeg binaries added --- FFMpegCore.Test/DownloaderTests.cs | 27 +++++++++++ FFMpegCore/Helpers/FFMpegDownloader.cs | 64 ++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 FFMpegCore.Test/DownloaderTests.cs create mode 100644 FFMpegCore/Helpers/FFMpegDownloader.cs diff --git a/FFMpegCore.Test/DownloaderTests.cs b/FFMpegCore.Test/DownloaderTests.cs new file mode 100644 index 0000000..301078a --- /dev/null +++ b/FFMpegCore.Test/DownloaderTests.cs @@ -0,0 +1,27 @@ +using System.Runtime.InteropServices; +using FFMpegCore.Helpers; + +namespace FFMpegCore.Test; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +public class DownloaderTests +{ + [TestClass] + public class FFMpegDownloaderTest + { + [TestMethod] + public void GetLatestVersionTest() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + var files = FFMpegDownloader.GetLatestVersion(); + Assert.IsTrue(files.Count == 3); + } + else + { + Assert.Inconclusive("This test is only for Windows"); + } + + } + } +} diff --git a/FFMpegCore/Helpers/FFMpegDownloader.cs b/FFMpegCore/Helpers/FFMpegDownloader.cs new file mode 100644 index 0000000..7767683 --- /dev/null +++ b/FFMpegCore/Helpers/FFMpegDownloader.cs @@ -0,0 +1,64 @@ +using System.ComponentModel; +using System.Net; +using System.IO; +using System.IO.Compression; + + +namespace FFMpegCore.Helpers; +using System.Runtime.InteropServices; + +/// +/// Downloads the latest FFMpeg binaries from GitHub. Only supported for windows at the moment. +/// +public class FFMpegDownloader // this class is built to be easily modified to support other platforms +{ + /// + /// List of URLs to download FFMpeg from. + /// + private static Dictionary FFMpegDownloadUrls = new() + { + { "windows", "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip" } + }; + + public static List GetLatestVersion() + { + var os = GetOSPlatform(); + var zipStream = DownloadFFMpeg(new Uri(FFMpegDownloadUrls[os])); + return ExtractAndSave(zipStream); + } + + private static MemoryStream DownloadFFMpeg(Uri address) + { + var client = new WebClient(); + var zipStream = new MemoryStream(client.DownloadData(address)); + zipStream.Position = 0; + + return zipStream; + } + + private static List ExtractAndSave(Stream zipStream) + { + using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read); + List files = new(); + foreach (var entry in archive.Entries) + { + if (entry.Name is "ffmpeg.exe" or "ffmpeg" or "ffprobe.exe") + { + entry.ExtractToFile(Path.Combine(GlobalFFOptions.Current.BinaryFolder, entry.Name), true); + files.Add(Path.Combine(GlobalFFOptions.Current.BinaryFolder, entry.Name)); + } + } + + return files; + } + + private static string GetOSPlatform() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return "windows"; + } + + throw new PlatformNotSupportedException("Auto download is only supported on Windows."); + } +}