From d1a1864aa1f141cacd504c4c74b0d3a2a787f991 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Wed, 15 Apr 2020 00:40:51 +0200 Subject: [PATCH] Make output file existence check optional --- FFMpegCore/FFMPEG/FFMpeg.cs | 28 ++++++++++++++-------------- FFMpegCore/FFMpegCore.csproj | 5 ++--- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/FFMpegCore/FFMPEG/FFMpeg.cs b/FFMpegCore/FFMPEG/FFMpeg.cs index d98f24b..8f2c1a3 100644 --- a/FFMpegCore/FFMPEG/FFMpeg.cs +++ b/FFMpegCore/FFMPEG/FFMpeg.cs @@ -88,7 +88,7 @@ public Bitmap Snapshot(VideoInfo source, FileInfo output, Size? size = null, Tim new OutputArgument(output) ); - if (!RunProcess(container, output)) + if (!RunProcess(container, output, false)) { throw new OperationCanceledException("Could not take snapshot!"); } @@ -198,7 +198,7 @@ public VideoInfo PosterWithAudio(FileInfo image, FileInfo audio, FileInfo output new ShortestArgument(true), new OutputArgument(output) ); - if (!RunProcess(container, output)) + if (!RunProcess(container, output, false)) { throw new FFMpegException(FFMpegExceptionType.Operation, "An error occured while adding the audio file to the image."); @@ -274,7 +274,7 @@ public VideoInfo JoinImageSequence(FileInfo output, double frameRate = 30, param try { - if (!RunProcess(container, output)) + if (!RunProcess(container, output, false)) { throw new FFMpegException(FFMpegExceptionType.Operation, "Could not join the provided image sequence."); @@ -346,7 +346,7 @@ public FileInfo ExtractAudio(VideoInfo source, FileInfo output) new OutputArgument(output) ); - if (!RunProcess(container, output)) + if (!RunProcess(container, output, false)) { throw new FFMpegException(FFMpegExceptionType.Operation, "Could not extract the audio from the requested video."); @@ -381,24 +381,24 @@ public VideoInfo ReplaceAudio(VideoInfo source, FileInfo audio, FileInfo output, )); } - public VideoInfo Convert(ArgumentContainer arguments) + public VideoInfo Convert(ArgumentContainer arguments, bool skipExistsCheck = false) { var (sources, output) = GetInputOutput(arguments); _totalTime = TimeSpan.FromSeconds(sources.Sum(source => source.Duration.TotalSeconds)); - if (!RunProcess(arguments, output)) - throw new FFMpegException(FFMpegExceptionType.Operation, "Could not replace the video audio."); + if (!RunProcess(arguments, output, skipExistsCheck)) + throw new FFMpegException(FFMpegExceptionType.Conversion, "Could not process file without error"); _totalTime = TimeSpan.MinValue; return new VideoInfo(output); } - public async Task ConvertAsync(ArgumentContainer arguments) + public async Task ConvertAsync(ArgumentContainer arguments, bool skipExistsCheck = false) { var (sources, output) = GetInputOutput(arguments); _totalTime = TimeSpan.FromSeconds(sources.Sum(source => source.Duration.TotalSeconds)); - if (!await RunProcessAsync(arguments, output)) - throw new FFMpegException(FFMpegExceptionType.Operation, "Could not replace the video audio."); + if (!await RunProcessAsync(arguments, output, skipExistsCheck)) + throw new FFMpegException(FFMpegExceptionType.Conversion, "Could not process file without error"); _totalTime = TimeSpan.MinValue; return new VideoInfo(output); @@ -438,7 +438,7 @@ public void Stop() private readonly string _ffmpegPath; private TimeSpan _totalTime; - private bool RunProcess(ArgumentContainer container, FileInfo output) + private bool RunProcess(ArgumentContainer container, FileInfo output, bool skipExistsCheck) { _instance?.Dispose(); var arguments = ArgumentBuilder.BuildArguments(container); @@ -447,12 +447,12 @@ private bool RunProcess(ArgumentContainer container, FileInfo output) _instance.DataReceived += OutputData; var exitCode = _instance.BlockUntilFinished(); - if (!File.Exists(output.FullName) || new FileInfo(output.FullName).Length == 0) + if (!skipExistsCheck && (!File.Exists(output.FullName) || new FileInfo(output.FullName).Length == 0)) throw new FFMpegException(FFMpegExceptionType.Process, string.Join("\n", _instance.ErrorData)); return exitCode == 0; } - private async Task RunProcessAsync(ArgumentContainer container, FileInfo output) + private async Task RunProcessAsync(ArgumentContainer container, FileInfo output, bool skipExistsCheck) { _instance?.Dispose(); var arguments = ArgumentBuilder.BuildArguments(container); @@ -461,7 +461,7 @@ private async Task RunProcessAsync(ArgumentContainer container, FileInfo o _instance.DataReceived += OutputData; var exitCode = await _instance.FinishedRunning(); - if (!File.Exists(output.FullName) || new FileInfo(output.FullName).Length == 0) + if (!skipExistsCheck && (!File.Exists(output.FullName) || new FileInfo(output.FullName).Length == 0)) throw new FFMpegException(FFMpegExceptionType.Process, string.Join("\n", _instance.ErrorData)); return exitCode == 0; diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index 56b870d..f79117b 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -10,10 +10,9 @@ 1.0.12 1.1.0.0 1.1.0.0 - Add more argument types and make ffprobe output capacity configurable -Update dependency + Make output file existence check optional 8 - 1.3.1 + 1.3.2 Vlad Jerca, Malte Rosenbjerg ffmpeg ffprobe convert video audio mediafile resize analyze muxing GitHub