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 =>
{
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();
}
/// <summary>
@ -328,11 +326,8 @@ private static void Cleanup(IEnumerable<string> pathList)
foreach (var path in pathList)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
}
}
}

View file

@ -39,7 +39,7 @@ public FFMpegArgumentProcessor NotifyOnProgress(Action<TimeSpan>? 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<bool> 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;

View file

@ -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)
{

View file

@ -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;

View file

@ -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!");
}