From ef8edf2a6ed9b33ebc7f373bff5f767c3e45c728 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Mon, 13 Jul 2020 23:54:46 +0200 Subject: [PATCH] Base file extension on video container info from ffmpeg Former-commit-id: 442e69ce1dc37f643c641ec81ebdb491bac02a32 --- FFMpegCore.Test/VideoTest.cs | 14 +++++++++ FFMpegCore/FFMpeg/Enums/FileExtension.cs | 10 +++---- FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs | 30 +++++++++++--------- FFMpegCore/FFMpegCore.csproj | 4 +-- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 5371d9a..e677370 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -332,6 +332,20 @@ public void Video_ToMP4_Args_StreamOutputPipe_Async() .WaitForResult(); } + [TestMethod] + public async Task TestDuplicateRun() + { + FFMpegArguments.FromInputFiles(VideoLibrary.LocalVideo) + .OutputToFile("temporary.mp4", true) + .ProcessSynchronously(); + + await FFMpegArguments.FromInputFiles(VideoLibrary.LocalVideo) + .OutputToFile("temporary.mp4", true) + .ProcessAsynchronously(); + + File.Delete("temporary.mp4"); + } + [TestMethod] public void Video_ToMP4_Args_StreamOutputPipe() { diff --git a/FFMpegCore/FFMpeg/Enums/FileExtension.cs b/FFMpegCore/FFMpeg/Enums/FileExtension.cs index d2e4a63..d45faf6 100644 --- a/FFMpegCore/FFMpeg/Enums/FileExtension.cs +++ b/FFMpegCore/FFMpeg/Enums/FileExtension.cs @@ -16,11 +16,11 @@ public static string Extension(this Codec type) _ => throw new Exception("The extension for this video type is not defined.") }; } - public static readonly string Mp4 = ".mp4"; - public static readonly string Mp3 = ".mp3"; - public static readonly string Ts = ".ts"; - public static readonly string Ogv = ".ogv"; + public static readonly string Mp4 = VideoType.Mp4.Extension; + public static readonly string Ts = VideoType.MpegTs.Extension; + public static readonly string Ogv = VideoType.Ogv.Extension; + public static readonly string WebM = VideoType.WebM.Extension; public static readonly string Png = ".png"; - public static readonly string WebM = ".webm"; + public static readonly string Mp3 = ".mp3"; } } diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs index 096c800..cfb1544 100644 --- a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs +++ b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; +using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; @@ -50,7 +52,8 @@ public FFMpegArgumentProcessor CancellableThrough(out Action cancel) } public bool ProcessSynchronously(bool throwOnError = true) { - var instance = PrepareInstance(out var cancellationTokenSource, out var errorCode); + var instance = PrepareInstance(out var cancellationTokenSource); + var errorCode = -1; void OnCancelEvent(object sender, EventArgs args) { @@ -70,7 +73,7 @@ void OnCancelEvent(object sender, EventArgs args) } catch (Exception e) { - if (!HandleException(throwOnError, e, instance)) return false; + if (!HandleException(throwOnError, e, instance.ErrorData)) return false; } finally { @@ -78,13 +81,13 @@ void OnCancelEvent(object sender, EventArgs args) _ffMpegArguments.Post(); } - return HandleCompletion(throwOnError, errorCode, instance); + return HandleCompletion(throwOnError, errorCode, instance.ErrorData); } - private bool HandleCompletion(bool throwOnError, int errorCode, Instance instance) + private bool HandleCompletion(bool throwOnError, int errorCode, IReadOnlyList errorData) { if (throwOnError && errorCode != 0) - throw new FFMpegException(FFMpegExceptionType.Conversion, string.Join("\n", instance.ErrorData)); + throw new FFMpegException(FFMpegExceptionType.Conversion, string.Join("\n", errorData)); _onPercentageProgress?.Invoke(100.0); if (_totalTimespan.HasValue) _onTimeProgress?.Invoke(_totalTimespan.Value); @@ -94,7 +97,8 @@ private bool HandleCompletion(bool throwOnError, int errorCode, Instance instanc public async Task ProcessAsynchronously(bool throwOnError = true) { - using var instance = PrepareInstance(out var cancellationTokenSource, out var errorCode); + using var instance = PrepareInstance(out var cancellationTokenSource); + var errorCode = -1; void OnCancelEvent(object sender, EventArgs args) { @@ -114,18 +118,18 @@ await Task.WhenAll(instance.FinishedRunning().ContinueWith(t => } catch (Exception e) { - if (!HandleException(throwOnError, e, instance)) return false; + if (!HandleException(throwOnError, e, instance.ErrorData)) return false; } finally { CancelEvent -= OnCancelEvent; _ffMpegArguments.Post(); } - - return HandleCompletion(throwOnError, errorCode, instance); + + return HandleCompletion(throwOnError, errorCode, instance.ErrorData); } - private Instance PrepareInstance(out CancellationTokenSource cancellationTokenSource, out int errorCode) + private Instance PrepareInstance(out CancellationTokenSource cancellationTokenSource) { FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); var instance = new Instance(FFMpegOptions.Options.FFmpegBinary(), _ffMpegArguments.Text); @@ -135,18 +139,16 @@ private Instance PrepareInstance(out CancellationTokenSource cancellationTokenSo if (_onTimeProgress != null || (_onPercentageProgress != null && _totalTimespan != null)) instance.DataReceived += OutputData; - errorCode = -1; - return instance; } - private static bool HandleException(bool throwOnError, Exception e, Instance instance) + private static bool HandleException(bool throwOnError, Exception e, IReadOnlyList errorData) { if (!throwOnError) return false; throw new FFMpegException(FFMpegExceptionType.Process, "Exception thrown during processing", e, - string.Join("\n", instance.ErrorData)); + string.Join("\n", errorData)); } private void OutputData(object sender, (DataType Type, string Data) msg) diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index 0c8f097..ac93458 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -9,9 +9,9 @@ 1.0.12 1.1.0.0 1.1.0.0 - - Fix NullReferenceException from parsing Format property on MediaAnalysis with information from show_format + - Fix for TS file extension difference between ffmpeg builds 8 - 2.2.1 + 2.2.2 Vlad Jerca, Malte Rosenbjerg ffmpeg ffprobe convert video audio mediafile resize analyze muxing GitHub