Read-MediaInfo.ps1
|
#requires -Version 5.1 param( [Parameter(Mandatory)][string]$Library, [Parameter(Mandatory)][string]$MediaFile ) $ErrorActionPreference = 'Stop' Add-Type -TypeDefinition @' using System; using System.ComponentModel; using System.Runtime.InteropServices; namespace ConvertVideo { public static class MediaInfoReader { [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.StdCall)] private delegate IntPtr CreateHandle(); [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void DeleteHandle(IntPtr handle); [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)] private delegate UIntPtr OpenFile(IntPtr handle, string path); [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)] private delegate IntPtr SetOption(IntPtr handle, string name, string value); [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate IntPtr ReadReport(IntPtr handle, UIntPtr reserved); 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 static string Read(string library, string path) { const uint SearchDllDirectoryAndSystem32 = 0x00000100 | 0x00000800; IntPtr module = LoadLibraryEx(library, IntPtr.Zero, SearchDllDirectoryAndSystem32); if (module == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error()); try { var create = (CreateHandle)GetExport(module, "MediaInfo_New", typeof(CreateHandle)); var delete = (DeleteHandle)GetExport(module, "MediaInfo_Delete", typeof(DeleteHandle)); var open = (OpenFile)GetExport(module, "MediaInfo_Open", typeof(OpenFile)); var option = (SetOption)GetExport(module, "MediaInfo_Option", typeof(SetOption)); var inform = (ReadReport)GetExport(module, "MediaInfo_Inform", typeof(ReadReport)); IntPtr handle = create(); if (handle == IntPtr.Zero) throw new InvalidOperationException("MediaInfo could not create a reader."); try { option(handle, "Inform", "JSON"); if (open(handle, path) == UIntPtr.Zero) throw new InvalidOperationException("MediaInfo could not open the media file."); return Marshal.PtrToStringUni(inform(handle, UIntPtr.Zero)); } finally { delete(handle); } } finally { FreeLibrary(module); } } } } '@ $dll = (Get-Item -LiteralPath $Library).FullName $file = (Get-Item -LiteralPath $MediaFile).FullName $json = [ConvertVideo.MediaInfoReader]::Read($dll, $file) if ([string]::IsNullOrWhiteSpace($json)) { throw 'MediaInfo returned no metadata.' } [Console]::OutputEncoding = [Text.UTF8Encoding]::new($false) [Console]::WriteLine($json) |