Skip to content

Commit c66ad47

Browse files
committed
Refactor Analyse and GetFrames overloads to share backing logic
1 parent ee1c1c3 commit c66ad47

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

FFMpegCore/FFProbe/FFProbe.cs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,24 @@ public static async Task<IMediaAnalysis> AnalyseAsync(string filePath, FFOptions
3030
string? customArguments = null)
3131
{
3232
ThrowIfInputFileDoesNotExist(filePath);
33-
34-
var instance = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments);
35-
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
36-
cancellationToken.ThrowIfCancellationRequested();
37-
ThrowIfExitCodeNotZero(result);
38-
39-
return ParseOutput(result);
33+
return await AnalyseCoreAsync(filePath, ffOptions, cancellationToken, customArguments).ConfigureAwait(false);
4034
}
4135

4236
public static async Task<IMediaAnalysis> AnalyseAsync(Uri uri, FFOptions? ffOptions = null, CancellationToken cancellationToken = default,
4337
string? customArguments = null)
4438
{
45-
var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments);
46-
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
47-
cancellationToken.ThrowIfCancellationRequested();
48-
ThrowIfExitCodeNotZero(result);
49-
50-
return ParseOutput(result);
39+
return await AnalyseCoreAsync(uri.AbsoluteUri, ffOptions, cancellationToken, customArguments).ConfigureAwait(false);
5140
}
5241

5342
public static async Task<IMediaAnalysis> AnalyseAsync(Stream stream, FFOptions? ffOptions = null, CancellationToken cancellationToken = default,
5443
string? customArguments = null)
5544
{
5645
var streamPipeSource = new StreamPipeSource(stream);
5746
var pipeArgument = new InputPipeArgument(streamPipeSource);
58-
var instance = PrepareStreamAnalysisInstance(pipeArgument.PipePath, ffOptions ?? GlobalFFOptions.Current, customArguments);
59-
pipeArgument.Pre();
6047

61-
var task = instance.StartAndWaitForExitAsync(cancellationToken);
48+
var task = AnalyseCoreAsync(pipeArgument.PipePath, ffOptions ?? GlobalFFOptions.Current, cancellationToken, customArguments);
49+
50+
pipeArgument.Pre();
6251
try
6352
{
6453
await pipeArgument.During(cancellationToken).ConfigureAwait(false);
@@ -71,12 +60,7 @@ public static async Task<IMediaAnalysis> AnalyseAsync(Stream stream, FFOptions?
7160
pipeArgument.Post();
7261
}
7362

74-
var result = await task.ConfigureAwait(false);
75-
cancellationToken.ThrowIfCancellationRequested();
76-
ThrowIfExitCodeNotZero(result);
77-
78-
pipeArgument.Post();
79-
return ParseOutput(result);
63+
return await task.ConfigureAwait(false);
8064
}
8165

8266
public static FFProbeFrames GetFrames(string filePath, FFOptions? ffOptions = null, string? customArguments = null)
@@ -98,29 +82,24 @@ public static async Task<FFProbeFrames> GetFramesAsync(string filePath, FFOption
9882
string? customArguments = null)
9983
{
10084
ThrowIfInputFileDoesNotExist(filePath);
101-
102-
var instance = PrepareFrameAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments);
103-
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
104-
return ParseFramesOutput(result);
85+
return await GetFramesCoreAsync(filePath, ffOptions, cancellationToken, customArguments).ConfigureAwait(false);
10586
}
10687

10788
public static async Task<FFProbeFrames> GetFramesAsync(Uri uri, FFOptions? ffOptions = null, CancellationToken cancellationToken = default,
10889
string? customArguments = null)
10990
{
110-
var instance = PrepareFrameAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments);
111-
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
112-
return ParseFramesOutput(result);
91+
return await GetFramesCoreAsync(uri.AbsoluteUri, ffOptions, cancellationToken, customArguments).ConfigureAwait(false);
11392
}
11493

11594
public static async Task<FFProbeFrames> GetFramesAsync(Stream stream, FFOptions? ffOptions = null, CancellationToken cancellationToken = default,
11695
string? customArguments = null)
11796
{
11897
var streamPipeSource = new StreamPipeSource(stream);
11998
var pipeArgument = new InputPipeArgument(streamPipeSource);
120-
var instance = PrepareFrameAnalysisInstance(pipeArgument.PipePath, ffOptions ?? GlobalFFOptions.Current, customArguments);
121-
pipeArgument.Pre();
12299

123-
var task = instance.Start().WaitForExitAsync(cancellationToken);
100+
var task = GetFramesCoreAsync(pipeArgument.PipePath, ffOptions ?? GlobalFFOptions.Current, cancellationToken, customArguments);
101+
102+
pipeArgument.Pre();
124103
try
125104
{
126105
await pipeArgument.During(cancellationToken).ConfigureAwait(false);
@@ -131,7 +110,24 @@ public static async Task<FFProbeFrames> GetFramesAsync(Stream stream, FFOptions?
131110
pipeArgument.Post();
132111
}
133112

134-
var result = task.ConfigureAwait(false).GetAwaiter().GetResult();
113+
return await task.ConfigureAwait(false);
114+
}
115+
116+
private static async Task<IMediaAnalysis> AnalyseCoreAsync(string inputPath, FFOptions? ffOptions, CancellationToken cancellationToken, string? customArguments)
117+
{
118+
var instance = PrepareStreamAnalysisInstance(inputPath, ffOptions ?? GlobalFFOptions.Current, customArguments);
119+
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
120+
cancellationToken.ThrowIfCancellationRequested();
121+
ThrowIfExitCodeNotZero(result);
122+
123+
return ParseOutput(result);
124+
}
125+
126+
private static async Task<FFProbeFrames> GetFramesCoreAsync(string inputPath, FFOptions? ffOptions, CancellationToken cancellationToken, string? customArguments)
127+
{
128+
var instance = PrepareFrameAnalysisInstance(inputPath, ffOptions ?? GlobalFFOptions.Current, customArguments);
129+
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
130+
cancellationToken.ThrowIfCancellationRequested();
135131
ThrowIfExitCodeNotZero(result);
136132

137133
return ParseFramesOutput(result);

0 commit comments

Comments
 (0)