Support custom temp dir and cleanup

Former-commit-id: da399a69aa
This commit is contained in:
Malte Rosenbjerg 2020-05-12 17:30:35 +02:00
parent 684f2f6c08
commit 4361bf393b
5 changed files with 19 additions and 25 deletions

View file

@ -185,7 +185,7 @@ public static bool Join(string output, params MediaAnalysis[] videos)
var temporaryVideoParts = videos.Select(video => var temporaryVideoParts = videos.Select(video =>
{ {
FFMpegHelper.ConversionSizeExceptionCheck(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); Convert(video, destinationPath, VideoType.Ts);
return destinationPath; return destinationPath;
}).ToArray(); }).ToArray();
@ -217,7 +217,7 @@ public static bool JoinImageSequence(string output, double frameRate = 30, param
var temporaryImageFiles = images.Select((image, index) => var temporaryImageFiles = images.Select((image, index) =>
{ {
FFMpegHelper.ConversionSizeExceptionCheck(Image.FromFile(image.FullName)); 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); File.Copy(image.FullName, destinationPath);
return destinationPath; return destinationPath;
}).ToArray(); }).ToArray();
@ -226,7 +226,7 @@ public static bool JoinImageSequence(string output, double frameRate = 30, param
try try
{ {
return FFMpegArguments return FFMpegArguments
.FromInputFiles(false, Path.Join(firstImage.Directory.FullName, "%09d.png")) .FromInputFiles(false, Path.Combine(FFMpegOptions.Options.TempDirectory, "%09d.png"))
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.Resize(firstImage.Width, firstImage.Height) .Resize(firstImage.Width, firstImage.Height)
.WithFrameOutputCount(images.Length) .WithFrameOutputCount(images.Length)
@ -251,15 +251,13 @@ public static bool SaveM3U8Stream(Uri uri, string output)
{ {
FFMpegHelper.ExtensionExceptionCheck(output, FileExtension.Mp4); FFMpegHelper.ExtensionExceptionCheck(output, FileExtension.Mp4);
if (uri.Scheme == "http" || uri.Scheme == "https") 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) return FFMpegArguments
.OutputToFile(output) .FromInputFiles(false, uri)
.ProcessSynchronously(); .OutputToFile(output)
} .ProcessSynchronously();
throw new ArgumentException($"Uri: {uri.AbsoluteUri}, does not point to a valid http(s) stream.");
} }
/// <summary> /// <summary>
@ -328,11 +326,8 @@ private static void Cleanup(IEnumerable<string> pathList)
foreach (var path in pathList) foreach (var path in pathList)
{ {
if (File.Exists(path)) if (File.Exists(path))
{
File.Delete(path); File.Delete(path);
}
} }
} }
} }
} }

View file

@ -39,7 +39,7 @@ public FFMpegArgumentProcessor NotifyOnProgress(Action<TimeSpan>? onTimeProgress
public bool ProcessSynchronously() public bool ProcessSynchronously()
{ {
FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); 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; instance.DataReceived += OutputData;
var errorCode = -1; var errorCode = -1;
@ -60,7 +60,7 @@ public bool ProcessSynchronously()
public async Task<bool> ProcessAsynchronously() public async Task<bool> ProcessAsynchronously()
{ {
FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); 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)) if (_onTimeProgress != null || (_onPercentageProgress != null && _totalTimespan != null))
instance.DataReceived += OutputData; instance.DataReceived += OutputData;
var errorCode = -1; var errorCode = -1;

View file

@ -9,6 +9,7 @@ public class FFMpegOptions
{ {
private static readonly string ConfigFile = Path.Combine(".", "ffmpeg.config.json"); private static readonly string ConfigFile = Path.Combine(".", "ffmpeg.config.json");
private static readonly string DefaultRoot = Path.Combine(".", "FFMPEG", "bin"); 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(); public static FFMpegOptions Options { get; private set; } = new FFMpegOptions();
@ -29,10 +30,11 @@ static FFMpegOptions()
} }
public string RootDirectory { get; set; } = DefaultRoot; 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) private static string FFBinary(string name)
{ {

View file

@ -87,7 +87,7 @@ private static MediaAnalysis ParseOutput(string filePath, Instance instance)
private static Instance PrepareInstance(string filePath, int outputCapacity) private static Instance PrepareInstance(string filePath, int outputCapacity)
{ {
FFProbeHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); FFProbeHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory);
var ffprobe = FFMpegOptions.Options.FFProbeBinary; var ffprobe = FFMpegOptions.Options.FFProbeBinary();
var arguments = $"-print_format json -show_streams \"{filePath}\""; var arguments = $"-print_format json -show_streams \"{filePath}\"";
var instance = new Instance(ffprobe, arguments) {DataBufferCapacity = outputCapacity}; var instance = new Instance(ffprobe, arguments) {DataBufferCapacity = outputCapacity};
return instance; return instance;

View file

@ -17,12 +17,9 @@ public static void ConversionSizeExceptionCheck(MediaAnalysis info)
ConversionSizeExceptionCheck(new Size(info.PrimaryVideoStream.Width, info.PrimaryVideoStream.Height)); ConversionSizeExceptionCheck(new Size(info.PrimaryVideoStream.Width, info.PrimaryVideoStream.Height));
} }
public static void ConversionSizeExceptionCheck(Size size) private static void ConversionSizeExceptionCheck(Size size)
{ {
if ( if (size.Height % 2 != 0 || size.Width % 2 != 0 )
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!"); throw new ArgumentException("FFMpeg yuv420p encoding requires the width and height to be a multiple of 2!");
} }