From 4361bf393b41ab775e4a4fde0e114b4c0e2e81ef Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Tue, 12 May 2020 17:30:35 +0200 Subject: [PATCH] Support custom temp dir and cleanup Former-commit-id: da399a69aab0aeab7a8ee90b38c8857aac4cbc15 --- FFMpegCore/FFMpeg/FFMpeg.cs | 25 ++++++++------------ FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs | 4 ++-- FFMpegCore/FFMpeg/FFMpegOptions.cs | 6 +++-- FFMpegCore/FFProbe/FFProbe.cs | 2 +- FFMpegCore/Helpers/FFMpegHelper.cs | 7 ++---- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/FFMpegCore/FFMpeg/FFMpeg.cs b/FFMpegCore/FFMpeg/FFMpeg.cs index 0d9786a..1bc591c 100644 --- a/FFMpegCore/FFMpeg/FFMpeg.cs +++ b/FFMpegCore/FFMpeg/FFMpeg.cs @@ -185,7 +185,7 @@ public static bool Join(string output, params MediaAnalysis[] videos) var temporaryVideoParts = videos.Select(video => { FFMpegHelper.ConversionSizeExceptionCheck(video); - var destinationPath = video.Path.Replace(video.Extension, FileExtension.Ts); + var destinationPath = Path.Combine(FFMpegOptions.Options.TempDirectory, $"{Path.GetFileNameWithoutExtension(video.Path)}{FileExtension.Ts}"); Convert(video, destinationPath, VideoType.Ts); return destinationPath; }).ToArray(); @@ -217,7 +217,7 @@ public static bool JoinImageSequence(string output, double frameRate = 30, param var temporaryImageFiles = images.Select((image, index) => { FFMpegHelper.ConversionSizeExceptionCheck(Image.FromFile(image.FullName)); - var destinationPath = image.FullName.Replace(image.Name, $"{index.ToString().PadLeft(9, '0')}{image.Extension}"); + var destinationPath = Path.Combine(FFMpegOptions.Options.TempDirectory, $"{index.ToString().PadLeft(9, '0')}{image.Extension}"); File.Copy(image.FullName, destinationPath); return destinationPath; }).ToArray(); @@ -226,7 +226,7 @@ public static bool JoinImageSequence(string output, double frameRate = 30, param try { return FFMpegArguments - .FromInputFiles(false, Path.Join(firstImage.Directory.FullName, "%09d.png")) + .FromInputFiles(false, Path.Combine(FFMpegOptions.Options.TempDirectory, "%09d.png")) .WithVideoCodec(VideoCodec.LibX264) .Resize(firstImage.Width, firstImage.Height) .WithFrameOutputCount(images.Length) @@ -251,15 +251,13 @@ public static bool SaveM3U8Stream(Uri uri, string output) { FFMpegHelper.ExtensionExceptionCheck(output, FileExtension.Mp4); - if (uri.Scheme == "http" || uri.Scheme == "https") - { - return FFMpegArguments - .FromInputFiles(false, uri) - .OutputToFile(output) - .ProcessSynchronously(); - } - - throw new ArgumentException($"Uri: {uri.AbsoluteUri}, does not point to a valid http(s) stream."); + if (uri.Scheme != "http" && uri.Scheme != "https") + throw new ArgumentException($"Uri: {uri.AbsoluteUri}, does not point to a valid http(s) stream."); + + return FFMpegArguments + .FromInputFiles(false, uri) + .OutputToFile(output) + .ProcessSynchronously(); } /// @@ -328,11 +326,8 @@ private static void Cleanup(IEnumerable pathList) foreach (var path in pathList) { if (File.Exists(path)) - { File.Delete(path); - } } } - } } \ No newline at end of file diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs index 9417c12..77d67a5 100644 --- a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs +++ b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs @@ -39,7 +39,7 @@ public FFMpegArgumentProcessor NotifyOnProgress(Action? onTimeProgress public bool ProcessSynchronously() { FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); - using var instance = new Instance(FFMpegOptions.Options.FFmpegBinary, _ffMpegArguments.Text); + using var instance = new Instance(FFMpegOptions.Options.FFmpegBinary(), _ffMpegArguments.Text); instance.DataReceived += OutputData; var errorCode = -1; @@ -60,7 +60,7 @@ public bool ProcessSynchronously() public async Task ProcessAsynchronously() { FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); - using var instance = new Instance(FFMpegOptions.Options.FFmpegBinary, _ffMpegArguments.Text); + using var instance = new Instance(FFMpegOptions.Options.FFmpegBinary(), _ffMpegArguments.Text); if (_onTimeProgress != null || (_onPercentageProgress != null && _totalTimespan != null)) instance.DataReceived += OutputData; var errorCode = -1; diff --git a/FFMpegCore/FFMpeg/FFMpegOptions.cs b/FFMpegCore/FFMpeg/FFMpegOptions.cs index 175c011..740ef13 100644 --- a/FFMpegCore/FFMpeg/FFMpegOptions.cs +++ b/FFMpegCore/FFMpeg/FFMpegOptions.cs @@ -9,6 +9,7 @@ public class FFMpegOptions { private static readonly string ConfigFile = Path.Combine(".", "ffmpeg.config.json"); private static readonly string DefaultRoot = Path.Combine(".", "FFMPEG", "bin"); + private static readonly string DefaultTemp = Path.Combine(Path.GetTempPath(), "FFMpegCore"); public static FFMpegOptions Options { get; private set; } = new FFMpegOptions(); @@ -29,10 +30,11 @@ static FFMpegOptions() } public string RootDirectory { get; set; } = DefaultRoot; + public string TempDirectory { get; set; } = DefaultTemp; - public string FFmpegBinary => FFBinary("FFMpeg"); + public string FFmpegBinary() => FFBinary("FFMpeg"); - public string FFProbeBinary => FFBinary("FFProbe"); + public string FFProbeBinary() => FFBinary("FFProbe"); private static string FFBinary(string name) { diff --git a/FFMpegCore/FFProbe/FFProbe.cs b/FFMpegCore/FFProbe/FFProbe.cs index bceb4ca..8dc0154 100644 --- a/FFMpegCore/FFProbe/FFProbe.cs +++ b/FFMpegCore/FFProbe/FFProbe.cs @@ -87,7 +87,7 @@ private static MediaAnalysis ParseOutput(string filePath, Instance instance) private static Instance PrepareInstance(string filePath, int outputCapacity) { FFProbeHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); - var ffprobe = FFMpegOptions.Options.FFProbeBinary; + var ffprobe = FFMpegOptions.Options.FFProbeBinary(); var arguments = $"-print_format json -show_streams \"{filePath}\""; var instance = new Instance(ffprobe, arguments) {DataBufferCapacity = outputCapacity}; return instance; diff --git a/FFMpegCore/Helpers/FFMpegHelper.cs b/FFMpegCore/Helpers/FFMpegHelper.cs index 063760c..f280b08 100644 --- a/FFMpegCore/Helpers/FFMpegHelper.cs +++ b/FFMpegCore/Helpers/FFMpegHelper.cs @@ -17,12 +17,9 @@ public static void ConversionSizeExceptionCheck(MediaAnalysis info) ConversionSizeExceptionCheck(new Size(info.PrimaryVideoStream.Width, info.PrimaryVideoStream.Height)); } - public static void ConversionSizeExceptionCheck(Size size) + private static void ConversionSizeExceptionCheck(Size size) { - if ( - size.Height % 2 != 0 || - size.Width % 2 != 0 - ) + if (size.Height % 2 != 0 || size.Width % 2 != 0 ) { throw new ArgumentException("FFMpeg yuv420p encoding requires the width and height to be a multiple of 2!"); }