From a5aad904e7c0062fe9a68e0584a0d89a48d1594e Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Wed, 29 Jan 2020 11:53:36 +0100 Subject: [PATCH] FFProbe: expose async methods #30 Former-commit-id: 7b9b8afac86225a07a4805be7a03893b238c0fa9 --- FFMpegCore/FFMPEG/FFBase.cs | 7 +++++++ FFMpegCore/FFMPEG/FFProbe.cs | 33 ++++++++++++++++++++++++++++++--- FFMpegCore/FFMpegCore.csproj | 1 + 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/FFMpegCore/FFMPEG/FFBase.cs b/FFMpegCore/FFMPEG/FFBase.cs index fe90fd5..6ac3d80 100644 --- a/FFMpegCore/FFMPEG/FFBase.cs +++ b/FFMpegCore/FFMPEG/FFBase.cs @@ -4,6 +4,8 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Threading.Tasks; +using RunProcessAsTask; namespace FFMpegCore.FFMPEG { @@ -78,5 +80,10 @@ public void Kill() IsKillFaulty = true; } } + protected async Task RunProcessAsync(string filePath, string arguments) + { + var result = await ProcessEx.RunAsync(filePath, arguments); + return string.Join("", result.StandardOutput); + } } } \ No newline at end of file diff --git a/FFMpegCore/FFMPEG/FFProbe.cs b/FFMpegCore/FFMPEG/FFProbe.cs index df32c93..0095a95 100644 --- a/FFMpegCore/FFMPEG/FFProbe.cs +++ b/FFMpegCore/FFMPEG/FFProbe.cs @@ -5,6 +5,8 @@ using System.Globalization; using System.IO; using System.Runtime.InteropServices; +using System.Threading.Tasks; +using RunProcessAsTask; namespace FFMpegCore.FFMPEG { @@ -28,6 +30,15 @@ public VideoInfo ParseVideoInfo(string source) { return ParseVideoInfo(new VideoInfo(source)); } + /// + /// Probes the targeted video file asynchronously and retrieves all available details. + /// + /// Source video file. + /// A task for the video info object containing all details necessary. + public Task ParseVideoInfoAsync(string source) + { + return ParseVideoInfoAsync(new VideoInfo(source)); + } /// /// Probes the targeted video file and retrieves all available details. @@ -36,10 +47,26 @@ public VideoInfo ParseVideoInfo(string source) /// A video info object containing all details necessary. public VideoInfo ParseVideoInfo(VideoInfo info) { - var jsonOutput = - RunProcess($"-v quiet -print_format json -show_streams \"{info.FullName}\""); + var output = RunProcess(BuildFFProbeArguments(info)); + return ParseVideoInfoInternal(info, output); + } + /// + /// Probes the targeted video file asynchronously and retrieves all available details. + /// + /// Source video file. + /// A video info object containing all details necessary. + public async Task ParseVideoInfoAsync(VideoInfo info) + { + var output = await RunProcessAsync(_ffprobePath, BuildFFProbeArguments(info)); + return ParseVideoInfoInternal(info, output); + } - var metadata = JsonConvert.DeserializeObject(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(probeOutput); if (metadata.Streams == null || metadata.Streams.Count == 0) { diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index ab2456e..6ee0e95 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -130,6 +130,7 @@ +