From b2488303cf8451a679af5717b28dc5b6769d6b85 Mon Sep 17 00:00:00 2001 From: vfrz Date: Wed, 12 Apr 2023 21:47:36 +0200 Subject: [PATCH 1/5] feature: custom ffprobe arguments --- FFMpegCore/FFProbe/FFProbe.cs | 64 +++++++++++++++++------------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/FFMpegCore/FFProbe/FFProbe.cs b/FFMpegCore/FFProbe/FFProbe.cs index 8a7069e..ddff026 100644 --- a/FFMpegCore/FFProbe/FFProbe.cs +++ b/FFMpegCore/FFProbe/FFProbe.cs @@ -10,52 +10,52 @@ namespace FFMpegCore { public static class FFProbe { - public static IMediaAnalysis Analyse(string filePath, FFOptions? ffOptions = null) + public static IMediaAnalysis Analyse(string filePath, FFOptions? ffOptions = null, string? customArguments = null) { ThrowIfInputFileDoesNotExist(filePath); - var processArguments = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current); + var processArguments = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); var result = processArguments.StartAndWaitForExit(); ThrowIfExitCodeNotZero(result); return ParseOutput(result); } - public static FFProbeFrames GetFrames(string filePath, FFOptions? ffOptions = null) + public static FFProbeFrames GetFrames(string filePath, FFOptions? ffOptions = null, string? customArguments = null) { ThrowIfInputFileDoesNotExist(filePath); - var instance = PrepareFrameAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current); + 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) + public static FFProbePackets GetPackets(string filePath, FFOptions? ffOptions = null, string? customArguments = null) { ThrowIfInputFileDoesNotExist(filePath); - var instance = PreparePacketAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current); + var instance = PreparePacketAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); var result = instance.StartAndWaitForExit(); ThrowIfExitCodeNotZero(result); return ParsePacketsOutput(result); } - public static IMediaAnalysis Analyse(Uri uri, FFOptions? ffOptions = null) + public static IMediaAnalysis Analyse(Uri uri, FFOptions? ffOptions = null, string? customArguments = null) { - var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current); + var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments); var result = instance.StartAndWaitForExit(); ThrowIfExitCodeNotZero(result); return ParseOutput(result); } - public static IMediaAnalysis Analyse(Stream stream, FFOptions? ffOptions = null) + 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); + var instance = PrepareStreamAnalysisInstance(pipeArgument.PipePath, ffOptions ?? GlobalFFOptions.Current, customArguments); pipeArgument.Pre(); var task = instance.StartAndWaitForExitAsync(); @@ -75,57 +75,57 @@ public static IMediaAnalysis Analyse(Stream stream, FFOptions? ffOptions = null) return ParseOutput(result); } - public static async Task AnalyseAsync(string filePath, FFOptions? ffOptions = null, CancellationToken cancellationToken = default) + public static async Task AnalyseAsync(string filePath, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, string? customArguments = null) { ThrowIfInputFileDoesNotExist(filePath); - var instance = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current); + var instance = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments); var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); ThrowIfExitCodeNotZero(result); return ParseOutput(result); } - public static FFProbeFrames GetFrames(Uri uri, FFOptions? ffOptions = null) + public static FFProbeFrames GetFrames(Uri uri, FFOptions? ffOptions = null, string? customArguments = null) { - var instance = PrepareFrameAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current); + 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) + 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); + 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) + 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); + 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) + public static async Task AnalyseAsync(Uri uri, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, string? customArguments = null) { - var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current); + var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments); var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); ThrowIfExitCodeNotZero(result); return ParseOutput(result); } - public static async Task AnalyseAsync(Stream stream, FFOptions? ffOptions = null, CancellationToken cancellationToken = default) + public static async Task AnalyseAsync(Stream stream, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, string? customArguments = null) { var streamPipeSource = new StreamPipeSource(stream); var pipeArgument = new InputPipeArgument(streamPipeSource); - var instance = PrepareStreamAnalysisInstance(pipeArgument.PipePath, ffOptions ?? GlobalFFOptions.Current); + var instance = PrepareStreamAnalysisInstance(pipeArgument.PipePath, ffOptions ?? GlobalFFOptions.Current, customArguments); pipeArgument.Pre(); var task = instance.StartAndWaitForExitAsync(cancellationToken); @@ -148,9 +148,9 @@ public static async Task AnalyseAsync(Stream stream, FFOptions? return ParseOutput(result); } - public static async Task GetFramesAsync(Uri uri, FFOptions? ffOptions = null, CancellationToken cancellationToken = default) + public static async Task GetFramesAsync(Uri uri, FFOptions? ffOptions = null, CancellationToken cancellationToken = default, string? customArguments = null) { - var instance = PrepareFrameAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current); + var instance = PrepareFrameAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments); var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false); return ParseFramesOutput(result); } @@ -212,18 +212,18 @@ private static void ThrowIfExitCodeNotZero(IProcessResult result) } } - private static ProcessArguments PrepareStreamAnalysisInstance(string filePath, FFOptions ffOptions) - => PrepareInstance($"-loglevel error -print_format json -show_format -sexagesimal -show_streams \"{filePath}\"", ffOptions); - private static ProcessArguments PrepareFrameAnalysisInstance(string filePath, FFOptions ffOptions) - => PrepareInstance($"-loglevel error -print_format json -show_frames -v quiet -sexagesimal \"{filePath}\"", ffOptions); - private static ProcessArguments PreparePacketAnalysisInstance(string filePath, FFOptions ffOptions) - => PrepareInstance($"-loglevel error -print_format json -show_packets -v quiet -sexagesimal \"{filePath}\"", ffOptions); + private static ProcessArguments PrepareStreamAnalysisInstance(string filePath, FFOptions ffOptions, string? customArguments) + => PrepareInstance($"-loglevel error -print_format json -show_format -sexagesimal -show_streams \"{filePath}\"", ffOptions, customArguments); + private static ProcessArguments PrepareFrameAnalysisInstance(string filePath, FFOptions ffOptions, string? customArguments) + => PrepareInstance($"-loglevel error -print_format json -show_frames -v quiet -sexagesimal \"{filePath}\"", ffOptions, customArguments); + private static ProcessArguments PreparePacketAnalysisInstance(string filePath, FFOptions ffOptions, string? customArguments) + => PrepareInstance($"-loglevel error -print_format json -show_packets -v quiet -sexagesimal \"{filePath}\"", ffOptions, customArguments); - private static ProcessArguments PrepareInstance(string arguments, FFOptions ffOptions) + private static ProcessArguments PrepareInstance(string arguments, FFOptions ffOptions, string? customArguments) { FFProbeHelper.RootExceptionCheck(); FFProbeHelper.VerifyFFProbeExists(ffOptions); - var startInfo = new ProcessStartInfo(GlobalFFOptions.GetFFProbeBinaryPath(ffOptions), arguments) + var startInfo = new ProcessStartInfo(GlobalFFOptions.GetFFProbeBinaryPath(ffOptions), $"{arguments} {customArguments}") { StandardOutputEncoding = ffOptions.Encoding, StandardErrorEncoding = ffOptions.Encoding, From 4b0cd9239e563f630e0f54f5482f415f003e80ee Mon Sep 17 00:00:00 2001 From: vfrz Date: Wed, 12 Apr 2023 21:57:20 +0200 Subject: [PATCH 2/5] Add test --- FFMpegCore.Test/FFProbeTests.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/FFMpegCore.Test/FFProbeTests.cs b/FFMpegCore.Test/FFProbeTests.cs index 9da819a..0d58f43 100644 --- a/FFMpegCore.Test/FFProbeTests.cs +++ b/FFMpegCore.Test/FFProbeTests.cs @@ -1,4 +1,4 @@ -using FFMpegCore.Test.Resources; +using FFMpegCore.Test.Resources; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FFMpegCore.Test @@ -235,5 +235,12 @@ public async Task Probe_Success_32BitWavBitDepth_Async() Assert.IsNotNull(info.PrimaryAudioStream); Assert.AreEqual(32, info.PrimaryAudioStream.BitDepth); } + + [TestMethod] + public void Probe_Success_Custom_Arguments() + { + var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: world\""); + Assert.AreEqual(3, info.Duration.Seconds); + } } } From 66c166f2e8b2f3d486f2b2477ec546e625cf1bac Mon Sep 17 00:00:00 2001 From: vfrz Date: Wed, 12 Apr 2023 22:07:22 +0200 Subject: [PATCH 3/5] Try fix encoding --- FFMpegCore.Test/FFProbeTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FFMpegCore.Test/FFProbeTests.cs b/FFMpegCore.Test/FFProbeTests.cs index 0d58f43..ffdf9e9 100644 --- a/FFMpegCore.Test/FFProbeTests.cs +++ b/FFMpegCore.Test/FFProbeTests.cs @@ -239,7 +239,7 @@ public async Task Probe_Success_32BitWavBitDepth_Async() [TestMethod] public void Probe_Success_Custom_Arguments() { - var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: world\""); + var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: World\""); Assert.AreEqual(3, info.Duration.Seconds); } } From 338274b5009c11a4c69b5cdfef4886942b48a630 Mon Sep 17 00:00:00 2001 From: vfrz Date: Wed, 12 Apr 2023 22:10:37 +0200 Subject: [PATCH 4/5] Try fix encoding 2 --- FFMpegCore.Test/FFProbeTests.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/FFMpegCore.Test/FFProbeTests.cs b/FFMpegCore.Test/FFProbeTests.cs index ffdf9e9..9da819a 100644 --- a/FFMpegCore.Test/FFProbeTests.cs +++ b/FFMpegCore.Test/FFProbeTests.cs @@ -1,4 +1,4 @@ -using FFMpegCore.Test.Resources; +using FFMpegCore.Test.Resources; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FFMpegCore.Test @@ -235,12 +235,5 @@ public async Task Probe_Success_32BitWavBitDepth_Async() Assert.IsNotNull(info.PrimaryAudioStream); Assert.AreEqual(32, info.PrimaryAudioStream.BitDepth); } - - [TestMethod] - public void Probe_Success_Custom_Arguments() - { - var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: World\""); - Assert.AreEqual(3, info.Duration.Seconds); - } } } From bf06a8059be2730a462848f601cf5d5d2bf70873 Mon Sep 17 00:00:00 2001 From: vfrz Date: Wed, 12 Apr 2023 22:14:23 +0200 Subject: [PATCH 5/5] Add test again --- FFMpegCore.Test/FFProbeTests.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/FFMpegCore.Test/FFProbeTests.cs b/FFMpegCore.Test/FFProbeTests.cs index 9da819a..05ec92f 100644 --- a/FFMpegCore.Test/FFProbeTests.cs +++ b/FFMpegCore.Test/FFProbeTests.cs @@ -235,5 +235,12 @@ public async Task Probe_Success_32BitWavBitDepth_Async() Assert.IsNotNull(info.PrimaryAudioStream); Assert.AreEqual(32, info.PrimaryAudioStream.BitDepth); } + + [TestMethod] + public void Probe_Success_Custom_Arguments() + { + var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: World\""); + Assert.AreEqual(3, info.Duration.Seconds); + } } }