Read-FFmpegLibraries.ps1
|
#requires -Version 5.1 param([Parameter(Mandatory)][string]$Directory) $ErrorActionPreference = 'Stop' if (![Environment]::Is64BitProcess) { throw 'The shared FFmpeg libraries require 64-bit Windows PowerShell.' } $principal = [Security.Principal.WindowsPrincipal]::new([Security.Principal.WindowsIdentity]::GetCurrent()) if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Inspect per-user FFmpeg libraries as a standard user.' } $components = 'avutil', 'swresample', 'swscale', 'avcodec', 'avformat', 'avfilter', 'avdevice' $root = Get-Item -LiteralPath $Directory if ($root -isnot [IO.DirectoryInfo]) { throw 'A directory containing the FFmpeg DLLs is required.' } Add-Type -TypeDefinition @' using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.InteropServices; namespace VideoTools { public sealed class FFmpegLibraries : IDisposable { private readonly List<IntPtr> handles = new List<IntPtr>(); [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr LoadLibraryEx(string path, IntPtr file, uint flags); [DllImport("kernel32.dll", ExactSpelling = true)] private static extern IntPtr GetProcAddress(IntPtr module, string name); [DllImport("kernel32.dll")] private static extern bool FreeLibrary(IntPtr module); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate uint ReadVersion(); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr ReadText(); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr Iterate(ref IntPtr state); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int TestCodec(IntPtr codec); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int NextDevice(int previous); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr DeviceName(int device); public IntPtr Load(string path) { const uint SearchLibraryDirectoryAndSystem32 = 0x00000100 | 0x00000800; IntPtr handle = LoadLibraryEx(path, IntPtr.Zero, SearchLibraryDirectoryAndSystem32); if (handle == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not load " + path); handles.Add(handle); return handle; } private static Delegate GetExport(IntPtr module, string name, Type signature) { IntPtr address = GetProcAddress(module, name); if (address == IntPtr.Zero) throw new EntryPointNotFoundException(name); return Marshal.GetDelegateForFunctionPointer(address, signature); } public uint Version(IntPtr module, string name) { return ((ReadVersion)GetExport(module, name, typeof(ReadVersion)))(); } public string Text(IntPtr module, string name) { return Marshal.PtrToStringAnsi(((ReadText)GetExport(module, name, typeof(ReadText)))()); } public string[] Names(IntPtr module, string iterator, string predicate) { var next = (Iterate)GetExport(module, iterator, typeof(Iterate)); var include = String.IsNullOrEmpty(predicate) ? null : (TestCodec)GetExport(module, predicate, typeof(TestCodec)); var names = new List<string>(); IntPtr state = IntPtr.Zero; IntPtr item; while ((item = next(ref state)) != IntPtr.Zero) { // AVCodec, AVInputFormat, AVOutputFormat and AVFilter start with a name pointer. if (include == null || include(item) != 0) names.Add(Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(item))); } return names.ToArray(); } public string[] HardwareDevices(IntPtr module) { var next = (NextDevice)GetExport(module, "av_hwdevice_iterate_types", typeof(NextDevice)); var name = (DeviceName)GetExport(module, "av_hwdevice_get_type_name", typeof(DeviceName)); var names = new List<string>(); int device = 0; while ((device = next(device)) != 0) names.Add(Marshal.PtrToStringAnsi(name(device))); return names.ToArray(); } public void Dispose() { for (int i = handles.Count - 1; i >= 0; i--) FreeLibrary(handles[i]); handles.Clear(); } } } '@ $native = [VideoTools.FFmpegLibraries]::new() try { $handles = @{} $versions = [ordered]@{} $supportedMajors = @{ avutil = 61; swresample = 7; swscale = 10; avcodec = 63; avformat = 63; avfilter = 12; avdevice = 63 } foreach ($component in $components) { $files = @(Get-ChildItem -LiteralPath $root.FullName -File -Filter "$component-*.dll") if ($files.Count -ne 1 -or $files[0].Name -notmatch ('^' + $component + '-([0-9]+)\.dll$')) { throw "Expected one versioned $component DLL." } $expectedMajor = [int]$Matches[1] if ($expectedMajor -ne $supportedMajors[$component]) { throw "This version of VideoTools does not support $component ABI $expectedMajor." } $handle = $native.Load($files[0].FullName) $number = $native.Version($handle, ($component + '_version')) $major = $number -shr 16 if ($major -ne $expectedMajor) { throw "The $component ABI does not match its filename." } $handles[$component] = $handle $versions[$component] = '{0}.{1}.{2}' -f $major, (($number -shr 8) -band 255), ($number -band 255) } [pscustomobject]@{ Version = $native.Text($handles.avutil, 'av_version_info') License = $native.Text($handles.avutil, 'avutil_license') Configuration = $native.Text($handles.avutil, 'avutil_configuration') Libraries = $versions Encoders = @($native.Names($handles.avcodec, 'av_codec_iterate', 'av_codec_is_encoder') | Sort-Object -Unique) Decoders = @($native.Names($handles.avcodec, 'av_codec_iterate', 'av_codec_is_decoder') | Sort-Object -Unique) Muxers = @($native.Names($handles.avformat, 'av_muxer_iterate', $null) | Sort-Object -Unique) Demuxers = @($native.Names($handles.avformat, 'av_demuxer_iterate', $null) | Sort-Object -Unique) Filters = @($native.Names($handles.avfilter, 'av_filter_iterate', $null) | Sort-Object -Unique) HardwareDeviceTypes = @($native.HardwareDevices($handles.avutil) | Sort-Object -Unique) } | ConvertTo-Json -Depth 6 -Compress } finally { $native.Dispose() } |