refactoring and reformatting

This commit is contained in:
Kerry Cao 2023-05-05 01:47:08 -06:00
parent debb868b38
commit a647f44029

View file

@ -1,37 +1,81 @@
using System.Net; using System.Net;
using System.IO.Compression; using System.IO.Compression;
using System.Runtime.InteropServices;
namespace FFMpegCore.Helpers; namespace FFMpegCore.Helpers;
using System.Runtime.InteropServices;
/// <summary> /// <summary>
/// Downloads the latest FFMpeg suite binaries from GitHub. Only supported for windows at the moment. /// Downloads the latest FFMpeg suite binaries from GitHub. Only supported for windows at the moment.
/// </summary> /// </summary>
public class FFMpegDownloader public class FFMpegDownloader
{ {
private static Dictionary<FFMpegVersions, string> Windows64FFMpegDownloadUrls = new() private static readonly Dictionary<FFMpegVersions, string> Windows64FFMpegDownloadUrls = new()
{ {
{ FFMpegVersions.V4_4_1, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.4.1/ffmpeg-4.4.1-win-64.zip"}, {
{ FFMpegVersions.V4_2_1, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2.1/ffmpeg-4.2.1-win-64.zip"}, FFMpegVersions.V4_4_1,
{ FFMpegVersions.V4_2, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2/ffmpeg-4.2-win-64.zip"}, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.4.1/ffmpeg-4.4.1-win-64.zip"
{ FFMpegVersions.V4_1, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.1/ffmpeg-4.1-win-64.zip"}, },
{ FFMpegVersions.V4_0, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.0/ffmpeg-4.0.1-win-64.zip"}, {
{ FFMpegVersions.V3_4, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.4/ffmpeg-3.4-win-64.zip"}, FFMpegVersions.V4_2_1,
{ FFMpegVersions.V3_3, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.3/ffmpeg-3.3.4-win-64.zip"}, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2.1/ffmpeg-4.2.1-win-64.zip"
{ FFMpegVersions.V3_2, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.2/ffmpeg-3.2-win-64.zip"}, },
{
FFMpegVersions.V4_2,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2/ffmpeg-4.2-win-64.zip"
},
{
FFMpegVersions.V4_1,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.1/ffmpeg-4.1-win-64.zip"
},
{
FFMpegVersions.V4_0,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.0/ffmpeg-4.0.1-win-64.zip"
},
{
FFMpegVersions.V3_4,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.4/ffmpeg-3.4-win-64.zip"
},
{
FFMpegVersions.V3_3,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.3/ffmpeg-3.3.4-win-64.zip"
},
{
FFMpegVersions.V3_2,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.2/ffmpeg-3.2-win-64.zip"
},
}; };
private static Dictionary<FFMpegVersions, string> Windows32FFMpegDownloadUrls = new() private static readonly Dictionary<FFMpegVersions, string> Windows32FFMpegDownloadUrls = new()
{ {
{ FFMpegVersions.V4_4_1, "" }, { FFMpegVersions.V4_4_1, "" },
{ FFMpegVersions.V4_2_1, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2.1/ffmpeg-4.2.1-win-32.zip"}, {
{ FFMpegVersions.V4_2, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2/ffmpeg-4.2-win-32.zip"}, FFMpegVersions.V4_2_1,
{ FFMpegVersions.V4_1, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.1/ffmpeg-4.1-win-32.zip"}, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2.1/ffmpeg-4.2.1-win-32.zip"
{ FFMpegVersions.V4_0, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.0/ffmpeg-4.0.1-win-32.zip"}, },
{ FFMpegVersions.V3_4, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.4/ffmpeg-3.4-win-32.zip"}, {
{ FFMpegVersions.V3_3, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.3/ffmpeg-3.3.4-win-32.zip"}, FFMpegVersions.V4_2,
{ FFMpegVersions.V3_2, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.2/ffmpeg-3.2-win-32.zip"}, "https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.2/ffmpeg-4.2-win-32.zip"
},
{
FFMpegVersions.V4_1,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.1/ffmpeg-4.1-win-32.zip"
},
{
FFMpegVersions.V4_0,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v4.0/ffmpeg-4.0.1-win-32.zip"
},
{
FFMpegVersions.V3_4,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.4/ffmpeg-3.4-win-32.zip"
},
{
FFMpegVersions.V3_3,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.3/ffmpeg-3.3.4-win-32.zip"
},
{
FFMpegVersions.V3_2,
"https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v3.2/ffmpeg-3.2-win-32.zip"
},
}; };
/// <summary> /// <summary>
@ -74,11 +118,7 @@ public static List<string> DownloadFFMpeg(FFMpegVersions version = FFMpegVersion
? new Uri(Windows64FFMpegDownloadUrls[version]) ? new Uri(Windows64FFMpegDownloadUrls[version])
: new Uri(Windows32FFMpegDownloadUrls[version]); : new Uri(Windows32FFMpegDownloadUrls[version]);
HasValidUri(url); return DownloadAndSave(url);
Stream zipStream = DownloadZip(url);
return ExtractAndSave(zipStream);
} }
/// <summary> /// <summary>
@ -92,11 +132,7 @@ public static List<string> DownloadFFProbe(FFMpegVersions version = FFMpegVersio
? new Uri(Windows64FFMpegDownloadUrls[version].Replace("ffmpeg", "ffprobe")) ? new Uri(Windows64FFMpegDownloadUrls[version].Replace("ffmpeg", "ffprobe"))
: new Uri(Windows32FFMpegDownloadUrls[version].Replace("ffmpeg", "ffprobe")); : new Uri(Windows32FFMpegDownloadUrls[version].Replace("ffmpeg", "ffprobe"));
HasValidUri(url); return DownloadAndSave(url);
Stream zipStream = DownloadZip(url);
return ExtractAndSave(zipStream);
} }
/// <summary> /// <summary>
@ -110,11 +146,21 @@ public static List<string> DownloadFFPlay(FFMpegVersions version = FFMpegVersion
? new Uri(Windows64FFMpegDownloadUrls[version].Replace("ffmpeg", "ffplay")) ? new Uri(Windows64FFMpegDownloadUrls[version].Replace("ffmpeg", "ffplay"))
: new Uri(Windows32FFMpegDownloadUrls[version].Replace("ffmpeg", "ffplay")); : new Uri(Windows32FFMpegDownloadUrls[version].Replace("ffmpeg", "ffplay"));
return DownloadAndSave(url);
}
/// <summary>
/// Downloads the zip file from the given url and saves the binaries to bin directory.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static List<string> DownloadAndSave(Uri url)
{
HasValidUri(url); HasValidUri(url);
Stream zipStream = DownloadZip(url); Stream zipStream = DownloadZip(url);
return ExtractAndSave(zipStream); return ExtractZip(zipStream);
} }
/// <summary> /// <summary>
@ -136,13 +182,14 @@ private static MemoryStream DownloadZip(Uri address)
/// </summary> /// </summary>
/// <param name="zipStream"></param> /// <param name="zipStream"></param>
/// <returns></returns> /// <returns></returns>
private static List<string> ExtractAndSave(Stream zipStream) private static List<string> ExtractZip(Stream zipStream)
{ {
using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read); using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read);
List<string> files = new(); List<string> files = new();
foreach (var entry in archive.Entries) foreach (var entry in archive.Entries)
{ {
if (entry.Name is "ffmpeg.exe" or "ffmpeg" or "ffprobe.exe" or "ffplay.exe" or "ffplay") // only extract the binaries if (entry.Name is "ffmpeg.exe" or "ffmpeg" or "ffprobe.exe" or "ffplay.exe"
or "ffplay") // only extract the binaries
{ {
entry.ExtractToFile(Path.Combine(GlobalFFOptions.Current.BinaryFolder, entry.Name), true); entry.ExtractToFile(Path.Combine(GlobalFFOptions.Current.BinaryFolder, entry.Name), true);
files.Add(Path.Combine(GlobalFFOptions.Current.BinaryFolder, entry.Name)); files.Add(Path.Combine(GlobalFFOptions.Current.BinaryFolder, entry.Name));
@ -161,7 +208,8 @@ private static void HasValidUri(Uri uri)
{ {
if (uri.ToString() == "" || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (uri.ToString() == "" || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
throw new PlatformNotSupportedException("The requested version of FFMpeg component is not available for your OS/System."); throw new PlatformNotSupportedException(
"The requested version of FFMpeg component is not available for your OS/System.");
} }
} }
} }