From ee1c1c3ff884cd7903ce5760bb56db2664c9b5ca Mon Sep 17 00:00:00 2001 From: Emem Adegbola <43295492+ememadegbola@users.noreply.github.com> Date: Fri, 19 Dec 2025 14:14:40 +0000 Subject: [PATCH] Reorder FFProbe methods and use overloads where possible - Reordered public methods so Analyse, GetFrames and GetPackets (and their overloads) are grouped together for readability - Updated synchronous overloads to call the corresponding async implementations and wait with ConfigureAwait(false).GetAwaiter().GetResult() to avoid duplicated logic --- FFMpegCore/FFProbe/FFProbe.cs | 131 +++++++++++----------------------- 1 file changed, 43 insertions(+), 88 deletions(-) diff --git a/FFMpegCore/FFProbe/FFProbe.cs b/FFMpegCore/FFProbe/FFProbe.cs index a263b45..24300b8 100644 --- a/FFMpegCore/FFProbe/FFProbe.cs +++ b/FFMpegCore/FFProbe/FFProbe.cs @@ -13,73 +13,17 @@ public static class FFProbe { public static IMediaAnalysis Analyse(string filePath, FFOptions? ffOptions = null, string? customArguments = null) { - ThrowIfInputFileDoesNotExist(filePath); - - var processArguments = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); - var result = processArguments.StartAndWaitForExit(); - ThrowIfExitCodeNotZero(result); - - return ParseOutput(result); - } - - public static FFProbeFrames GetFrames(Stream stream, FFOptions? ffOptions = null, string? customArguments = null) - { - return GetFramesAsync(stream, ffOptions, CancellationToken.None, customArguments).ConfigureAwait(false).GetAwaiter().GetResult(); - } - - public static FFProbeFrames GetFrames(string filePath, FFOptions? ffOptions = null, string? customArguments = null) - { - ThrowIfInputFileDoesNotExist(filePath); - - var instance = PrepareFrameAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); - var result = instance.StartAndWaitForExit(); - ThrowIfExitCodeNotZero(result); - - return ParseFramesOutput(result); - } - - public static FFProbePackets GetPackets(string filePath, FFOptions? ffOptions = null, string? customArguments = null) - { - ThrowIfInputFileDoesNotExist(filePath); - - var instance = PreparePacketAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); - var result = instance.StartAndWaitForExit(); - ThrowIfExitCodeNotZero(result); - - return ParsePacketsOutput(result); + return AnalyseAsync(filePath, ffOptions, CancellationToken.None, customArguments).ConfigureAwait(false).GetAwaiter().GetResult(); } public static IMediaAnalysis Analyse(Uri uri, FFOptions? ffOptions = null, string? customArguments = null) { - var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments); - var result = instance.StartAndWaitForExit(); - ThrowIfExitCodeNotZero(result); - - return ParseOutput(result); + return AnalyseAsync(uri, ffOptions, CancellationToken.None, customArguments).ConfigureAwait(false).GetAwaiter().GetResult(); } public static IMediaAnalysis Analyse(Stream stream, FFOptions? ffOptions = null, string? customArguments = null) { - var streamPipeSource = new StreamPipeSource(stream); - var pipeArgument = new InputPipeArgument(streamPipeSource); - var instance = PrepareStreamAnalysisInstance(pipeArgument.PipePath, ffOptions ?? GlobalFFOptions.Current, customArguments); - pipeArgument.Pre(); - - var task = instance.StartAndWaitForExitAsync(); - try - { - pipeArgument.During().ConfigureAwait(false).GetAwaiter().GetResult(); - } - catch (IOException) { } - finally - { - pipeArgument.Post(); - } - - var result = task.ConfigureAwait(false).GetAwaiter().GetResult(); - ThrowIfExitCodeNotZero(result); - - return ParseOutput(result); + return AnalyseAsync(stream, ffOptions, CancellationToken.None, customArguments).ConfigureAwait(false).GetAwaiter().GetResult(); } public static async Task AnalyseAsync(string filePath, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, @@ -95,35 +39,6 @@ public static class FFProbe return ParseOutput(result); } - public static FFProbeFrames GetFrames(Uri uri, FFOptions? ffOptions = null, string? customArguments = null) - { - var instance = PrepareFrameAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments); - var result = instance.StartAndWaitForExit(); - ThrowIfExitCodeNotZero(result); - - return ParseFramesOutput(result); - } - - public static async Task GetFramesAsync(string filePath, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, - string? customArguments = null) - { - ThrowIfInputFileDoesNotExist(filePath); - - var instance = PrepareFrameAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); - var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); - return ParseFramesOutput(result); - } - - public static async Task GetPacketsAsync(string filePath, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, - string? customArguments = null) - { - ThrowIfInputFileDoesNotExist(filePath); - - var instance = PreparePacketAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); - var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); - return ParsePacketsOutput(result); - } - public static async Task AnalyseAsync(Uri uri, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, string? customArguments = null) { @@ -164,6 +79,31 @@ public static class FFProbe return ParseOutput(result); } + public static FFProbeFrames GetFrames(string filePath, FFOptions? ffOptions = null, string? customArguments = null) + { + return GetFramesAsync(filePath, ffOptions, CancellationToken.None, customArguments).ConfigureAwait(false).GetAwaiter().GetResult(); + } + + public static FFProbeFrames GetFrames(Uri uri, FFOptions? ffOptions = null, string? customArguments = null) + { + return GetFramesAsync(uri, ffOptions, CancellationToken.None, customArguments).ConfigureAwait(false).GetAwaiter().GetResult(); + } + + public static FFProbeFrames GetFrames(Stream stream, FFOptions? ffOptions = null, string? customArguments = null) + { + return GetFramesAsync(stream, ffOptions, CancellationToken.None, customArguments).ConfigureAwait(false).GetAwaiter().GetResult(); + } + + public static async Task GetFramesAsync(string filePath, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, + string? customArguments = null) + { + ThrowIfInputFileDoesNotExist(filePath); + + var instance = PrepareFrameAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); + var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); + return ParseFramesOutput(result); + } + public static async Task GetFramesAsync(Uri uri, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, string? customArguments = null) { @@ -197,6 +137,21 @@ public static class FFProbe return ParseFramesOutput(result); } + public static FFProbePackets GetPackets(string filePath, FFOptions? ffOptions = null, string? customArguments = null) + { + return GetPacketsAsync(filePath, ffOptions, CancellationToken.None, customArguments).ConfigureAwait(false).GetAwaiter().GetResult(); + } + + public static async Task GetPacketsAsync(string filePath, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, + string? customArguments = null) + { + ThrowIfInputFileDoesNotExist(filePath); + + var instance = PreparePacketAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); + var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); + return ParsePacketsOutput(result); + } + private static IMediaAnalysis ParseOutput(IProcessResult instance) { var json = string.Join(string.Empty, instance.OutputData);