mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2025-01-19 04:56:43 +00:00
features to auto download ffmpeg binaries added
This commit is contained in:
parent
e32dd3a7f7
commit
9db4ba75b4
2 changed files with 91 additions and 0 deletions
27
FFMpegCore.Test/DownloaderTests.cs
Normal file
27
FFMpegCore.Test/DownloaderTests.cs
Normal file
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
64
FFMpegCore/Helpers/FFMpegDownloader.cs
Normal file
64
FFMpegCore/Helpers/FFMpegDownloader.cs
Normal file
|
@ -0,0 +1,64 @@
|
|||
using System.ComponentModel;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
|
||||
|
||||
namespace FFMpegCore.Helpers;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
/// <summary>
|
||||
/// Downloads the latest FFMpeg binaries from GitHub. Only supported for windows at the moment.
|
||||
/// </summary>
|
||||
public class FFMpegDownloader // this class is built to be easily modified to support other platforms
|
||||
{
|
||||
/// <summary>
|
||||
/// List of URLs to download FFMpeg from.
|
||||
/// </summary>
|
||||
private static Dictionary<string, string> FFMpegDownloadUrls = new()
|
||||
{
|
||||
{ "windows", "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip" }
|
||||
};
|
||||
|
||||
public static List<string> 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<string> ExtractAndSave(Stream zipStream)
|
||||
{
|
||||
using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read);
|
||||
List<string> 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.");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue