Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
feat(engines): enforce video size limit from percentage in FFmpeg
Gian Abi Firdausf0e9539dc71d
diff --git a/src/Filee.Engines/Media/FfmpegConverter.cs b/src/Filee.Engines/Media/FfmpegConverter.csindex 9e78ce6e..8cb51a67 100644--- a/src/Filee.Engines/Media/FfmpegConverter.cs+++ b/src/Filee.Engines/Media/FfmpegConverter.cs@@ -168,15 +168,21 @@ public sealed partial class FfmpegConverter(ILogger? logger = null) : IConverterreturn [output];}- // The size limit (#553): refused before anything is encoded when not even a low bitrate fits, else one encode with+ // The size limit (#553, #680): refused before anything is encoded when not even a low bitrate fits, else one encode with// the bitrate capped at what fits (MediaEncoding.ForSizeLimit), and another at that bitrate only if it missed.- var limit = !still && media.MaxSizeMb > 0 && MediaEncoding.VideoTargets.ContainsKey(step.To) ? SizeLimit.Bytes(media.MaxSizeMb) : 0;+ var source = step.OriginalSourcePath ?? step.InputPath;+ var limit = !still && MediaEncoding.VideoTargets.ContainsKey(step.To) ? SizeLimit.ForVideo(media, source) : 0;var kbps = 0;if (limit > 0){(media, kbps) = MediaEncoding.ForSizeLimit(media, info, limit);if (kbps == 0 && info.Duration is { TotalSeconds: > 0 })- throw new ConversionRefusedException("error.over_size_limit", $"{info.Duration.Value.TotalSeconds:0} s in {media.MaxSizeMb:0.##} MB");+ {+ var detail = media.MaxSizePercent > 0+ ? $"{info.Duration.Value.TotalSeconds:0} s in {media.MaxSizePercent}%"+ : $"{info.Duration.Value.TotalSeconds:0} s in {media.MaxSizeMb:0.##} MB";+ throw new ConversionRefusedException("error.over_size_limit", detail);+ }}var encoders = still ? null : await EncodersOfAsync(ffmpeg);diff --git a/src/Filee.Engines/Media/MediaEncoding.cs b/src/Filee.Engines/Media/MediaEncoding.csindex a0f89fd3..0db9f37c 100644--- a/src/Filee.Engines/Media/MediaEncoding.cs+++ b/src/Filee.Engines/Media/MediaEncoding.cs@@ -383,7 +383,7 @@ internal static partial class MediaEncodinginternal static bool CanCopyVideo(string target, VideoStreamInfo video, MediaOptions options){// A size limit encodes the video, so its bitrate can be capped (ForSizeLimit).- if (options.Quality == MediaQuality.Small || options.MaxSizeMb > 0 || options.MaxHeight > 0 && video.DisplayHeight > Math.Max(16, options.MaxHeight)+ if (options.Quality == MediaQuality.Small || options.MaxSizeMb > 0 || options.MaxSizePercent > 0 || options.MaxHeight > 0 && video.DisplayHeight > Math.Max(16, options.MaxHeight)|| video.Codec is not { } codec || !Remuxable.TryGetValue(target, out var codecs))return false;var takes = MediaCodecs.Effective(target, options.VideoCodec) switchdiff --git a/tests/Filee.Engines.Tests/SizeLimitTests.cs b/tests/Filee.Engines.Tests/SizeLimitTests.csindex 7d0ec5a6..62048dee 100644--- a/tests/Filee.Engines.Tests/SizeLimitTests.cs+++ b/tests/Filee.Engines.Tests/SizeLimitTests.cs@@ -429,4 +429,34 @@ public class SizeLimitTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Equal("8 s in 0.05 MB", file.ErrorDetail);Assert.Empty(job.Outputs);}++ [Fact]+ public async Task A_video_over_percentage_limit_is_encoded_until_it_fits()+ {+ Assert.SkipUnless(Tools is not null, "FFmpeg is not installed (pwsh build/fetch-engines.ps1 -Only ffmpeg)");+ var source = await MakeVideoAsync();+ var sourceLength = new FileInfo(source).Length;+ Assert.True(sourceLength > 3 * 1024 * 1024, $"the test video is only {sourceLength} bytes");++ var job = await fx.ConvertAsync([source], new Preset { TargetFormat = "mp4", Media = { MaxSizePercent = 25 }, Output = { FileNamePattern = "{name}_pct_fit" } });++ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));+ var outputLength = new FileInfo(Assert.Single(job.Outputs)).Length;+ var limit = SizeLimit.Bytes(sourceLength, 25);+ Assert.InRange(outputLength, 1, limit);+ }++ [Fact]+ public async Task A_video_too_long_for_its_percentage_limit_is_refused()+ {+ Assert.SkipUnless(Tools is not null, "FFmpeg is not installed (pwsh build/fetch-engines.ps1 -Only ffmpeg)");+ var source = await MakeVideoAsync();++ var job = await fx.ConvertAsync([source], new Preset { TargetFormat = "mp4", Media = { MaxSizePercent = 1 }, Output = { FileNamePattern = "{name}_pct_fit" } });++ var file = Assert.Single(job.Files);+ Assert.Equal("error.over_size_limit", file.ErrorKey);+ Assert.Equal("8 s in 1%", file.ErrorDetail);+ Assert.Empty(job.Outputs);+ }}