Ensure all images have same file extension and handle that

This commit is contained in:
Malte Rosenbjerg 2023-02-16 23:55:10 +01:00
parent e32dd3a7f7
commit ad5dca3cd6
2 changed files with 24 additions and 16 deletions

View file

@ -66,25 +66,34 @@ public static async Task<bool> SnapshotAsync(string input, string output, Size?
/// <returns>Output video information.</returns> /// <returns>Output video information.</returns>
public static bool JoinImageSequence(string output, double frameRate = 30, params string[] images) public static bool JoinImageSequence(string output, double frameRate = 30, params string[] images)
{ {
int? width = null, height = null; var fileExtensions = images.Select(Path.GetExtension).Distinct().ToArray();
var tempFolderName = Path.Combine(GlobalFFOptions.Current.TemporaryFilesFolder, Guid.NewGuid().ToString()); if (fileExtensions.Length != 1)
var temporaryImageFiles = images.Select((imagePath, index) =>
{ {
var analysis = FFProbe.Analyse(imagePath); throw new ArgumentException("All images must have the same extension", nameof(images));
FFMpegHelper.ConversionSizeExceptionCheck(analysis.PrimaryVideoStream!.Width, analysis.PrimaryVideoStream!.Height); }
width ??= analysis.PrimaryVideoStream.Width;
height ??= analysis.PrimaryVideoStream.Height;
var destinationPath = Path.Combine(tempFolderName, $"{index.ToString().PadLeft(9, '0')}{Path.GetExtension(imagePath)}"); var fileExtension = fileExtensions[0].ToLowerInvariant();
Directory.CreateDirectory(tempFolderName); int? width = null, height = null;
File.Copy(imagePath, destinationPath);
return destinationPath; var tempFolderName = Path.Combine(GlobalFFOptions.Current.TemporaryFilesFolder, Guid.NewGuid().ToString());
}).ToArray(); Directory.CreateDirectory(tempFolderName);
try try
{ {
var index = 0;
foreach (var imagePath in images)
{
var analysis = FFProbe.Analyse(imagePath);
FFMpegHelper.ConversionSizeExceptionCheck(analysis.PrimaryVideoStream!.Width, analysis.PrimaryVideoStream!.Height);
width ??= analysis.PrimaryVideoStream.Width;
height ??= analysis.PrimaryVideoStream.Height;
var destinationPath = Path.Combine(tempFolderName, $"{index++.ToString().PadLeft(9, '0')}{fileExtension}");
File.Copy(imagePath, destinationPath);
}
return FFMpegArguments return FFMpegArguments
.FromFileInput(Path.Combine(tempFolderName, "%09d.png"), false) .FromFileInput(Path.Combine(tempFolderName, $"%09d{fileExtension}"), false)
.OutputToFile(output, true, options => options .OutputToFile(output, true, options => options
.ForcePixelFormat("yuv420p") .ForcePixelFormat("yuv420p")
.Resize(width!.Value, height!.Value) .Resize(width!.Value, height!.Value)
@ -93,8 +102,7 @@ public static bool JoinImageSequence(string output, double frameRate = 30, param
} }
finally finally
{ {
Cleanup(temporaryImageFiles); Directory.Delete(tempFolderName, true);
Directory.Delete(tempFolderName);
} }
} }

View file

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<IsPackable>true</IsPackable> <IsPackable>true</IsPackable>
<Description>A .NET Standard FFMpeg/FFProbe wrapper for easily integrating media analysis and conversion into your .NET applications</Description> <Description>A .NET Standard FFMpeg/FFProbe wrapper for easily integrating media analysis and conversion into your .NET applications</Description>
<PackageVersion>5.0.0</PackageVersion> <PackageVersion>5.0.1</PackageVersion>
<PackageReleaseNotes> <PackageReleaseNotes>
</PackageReleaseNotes> </PackageReleaseNotes>
<PackageTags>ffmpeg ffprobe convert video audio mediafile resize analyze muxing</PackageTags> <PackageTags>ffmpeg ffprobe convert video audio mediafile resize analyze muxing</PackageTags>