Return null when no format detected during analysis

Former-commit-id: 66cdb302ef
This commit is contained in:
Malte Rosenbjerg 2021-02-03 23:20:12 +01:00
parent ffa53edb51
commit 08c898b56d

View file

@ -12,7 +12,7 @@ namespace FFMpegCore
{
public static class FFProbe
{
public static IMediaAnalysis Analyse(string filePath, int outputCapacity = int.MaxValue)
public static IMediaAnalysis? Analyse(string filePath, int outputCapacity = int.MaxValue)
{
if (!File.Exists(filePath))
throw new FFMpegException(FFMpegExceptionType.File, $"No file found at '{filePath}'");
@ -21,13 +21,13 @@ public static IMediaAnalysis Analyse(string filePath, int outputCapacity = int.M
instance.BlockUntilFinished();
return ParseOutput(filePath, instance);
}
public static IMediaAnalysis Analyse(Uri uri, int outputCapacity = int.MaxValue)
public static IMediaAnalysis? Analyse(Uri uri, int outputCapacity = int.MaxValue)
{
using var instance = PrepareInstance(uri.AbsoluteUri, outputCapacity);
instance.BlockUntilFinished();
return ParseOutput(uri.AbsoluteUri, instance);
}
public static IMediaAnalysis Analyse(Stream stream, int outputCapacity = int.MaxValue)
public static IMediaAnalysis? Analyse(Stream stream, int outputCapacity = int.MaxValue)
{
var streamPipeSource = new StreamPipeSource(stream);
var pipeArgument = new InputPipeArgument(streamPipeSource);
@ -50,7 +50,7 @@ public static IMediaAnalysis Analyse(Stream stream, int outputCapacity = int.Max
return ParseOutput(pipeArgument.PipePath, instance);
}
public static async Task<IMediaAnalysis> AnalyseAsync(string filePath, int outputCapacity = int.MaxValue)
public static async Task<IMediaAnalysis?> AnalyseAsync(string filePath, int outputCapacity = int.MaxValue)
{
if (!File.Exists(filePath))
throw new FFMpegException(FFMpegExceptionType.File, $"No file found at '{filePath}'");
@ -59,13 +59,13 @@ public static async Task<IMediaAnalysis> AnalyseAsync(string filePath, int outpu
await instance.FinishedRunning();
return ParseOutput(filePath, instance);
}
public static async Task<IMediaAnalysis> AnalyseAsync(Uri uri, int outputCapacity = int.MaxValue)
public static async Task<IMediaAnalysis?> AnalyseAsync(Uri uri, int outputCapacity = int.MaxValue)
{
using var instance = PrepareInstance(uri.AbsoluteUri, outputCapacity);
await instance.FinishedRunning();
return ParseOutput(uri.AbsoluteUri, instance);
}
public static async Task<IMediaAnalysis> AnalyseAsync(Stream stream, int outputCapacity = int.MaxValue)
public static async Task<IMediaAnalysis?> AnalyseAsync(Stream stream, int outputCapacity = int.MaxValue)
{
var streamPipeSource = new StreamPipeSource(stream);
var pipeArgument = new InputPipeArgument(streamPipeSource);
@ -92,13 +92,14 @@ public static async Task<IMediaAnalysis> AnalyseAsync(Stream stream, int outputC
return ParseOutput(pipeArgument.PipePath, instance);
}
private static IMediaAnalysis ParseOutput(string filePath, Instance instance)
private static IMediaAnalysis? ParseOutput(string filePath, Instance instance)
{
var json = string.Join(string.Empty, instance.OutputData);
var ffprobeAnalysis = JsonSerializer.Deserialize<FFProbeAnalysis>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
})!;
if (ffprobeAnalysis?.Format == null) return null;
return new MediaAnalysis(filePath, ffprobeAnalysis);
}