FFProbe: expose async methods

#30
This commit is contained in:
Malte Rosenbjerg 2020-01-29 11:53:36 +01:00 committed by GitHub
parent 8a8b8a968f
commit 7b9b8afac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 3 deletions

View file

@ -4,6 +4,8 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using RunProcessAsTask;
namespace FFMpegCore.FFMPEG namespace FFMpegCore.FFMPEG
{ {
@ -78,5 +80,10 @@ public void Kill()
IsKillFaulty = true; IsKillFaulty = true;
} }
} }
protected async Task<string> RunProcessAsync(string filePath, string arguments)
{
var result = await ProcessEx.RunAsync(filePath, arguments);
return string.Join("", result.StandardOutput);
}
} }
} }

View file

@ -5,6 +5,8 @@
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading.Tasks;
using RunProcessAsTask;
namespace FFMpegCore.FFMPEG namespace FFMpegCore.FFMPEG
{ {
@ -28,6 +30,15 @@ public VideoInfo ParseVideoInfo(string source)
{ {
return ParseVideoInfo(new VideoInfo(source)); return ParseVideoInfo(new VideoInfo(source));
} }
/// <summary>
/// Probes the targeted video file asynchronously and retrieves all available details.
/// </summary>
/// <param name="source">Source video file.</param>
/// <returns>A task for the video info object containing all details necessary.</returns>
public Task<VideoInfo> ParseVideoInfoAsync(string source)
{
return ParseVideoInfoAsync(new VideoInfo(source));
}
/// <summary> /// <summary>
/// Probes the targeted video file and retrieves all available details. /// Probes the targeted video file and retrieves all available details.
@ -36,10 +47,26 @@ public VideoInfo ParseVideoInfo(string source)
/// <returns>A video info object containing all details necessary.</returns> /// <returns>A video info object containing all details necessary.</returns>
public VideoInfo ParseVideoInfo(VideoInfo info) public VideoInfo ParseVideoInfo(VideoInfo info)
{ {
var jsonOutput = var output = RunProcess(BuildFFProbeArguments(info));
RunProcess($"-v quiet -print_format json -show_streams \"{info.FullName}\""); return ParseVideoInfoInternal(info, output);
}
/// <summary>
/// Probes the targeted video file asynchronously and retrieves all available details.
/// </summary>
/// <param name="info">Source video file.</param>
/// <returns>A video info object containing all details necessary.</returns>
public async Task<VideoInfo> ParseVideoInfoAsync(VideoInfo info)
{
var output = await RunProcessAsync(_ffprobePath, BuildFFProbeArguments(info));
return ParseVideoInfoInternal(info, output);
}
var metadata = JsonConvert.DeserializeObject<FFMpegStreamMetadata>(jsonOutput); private static string BuildFFProbeArguments(VideoInfo info) =>
$"-v quiet -print_format json -show_streams \"{info.FullName}\"";
private VideoInfo ParseVideoInfoInternal(VideoInfo info, string probeOutput)
{
var metadata = JsonConvert.DeserializeObject<FFMpegStreamMetadata>(probeOutput);
if (metadata.Streams == null || metadata.Streams.Count == 0) if (metadata.Streams == null || metadata.Streams.Count == 0)
{ {

View file

@ -130,6 +130,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="RunProcessAsTask" Version="1.2.4" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" /> <PackageReference Include="System.Drawing.Common" Version="4.5.1" />
</ItemGroup> </ItemGroup>