Skip to content

armgit / Filee

Public Imported from GitHub
0

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.

Every frame of a variable frame rate video is saved once

KnifeLemon82500965e786
diff --git a/src/Filee.Engines/Media/MediaEncoding.cs b/src/Filee.Engines/Media/MediaEncoding.cs
index eb40b8a1..fcbbd8a4 100644
--- a/src/Filee.Engines/Media/MediaEncoding.cs
+++ b/src/Filee.Engines/Media/MediaEncoding.cs
@@ -170,9 +170,15 @@ internal static class MediaEncoding
}
/// <summary>
- /// Slices frames of a video into an image sequence in <paramref name="outputPattern"/>
- /// for focus stacking and photography analysis.
+ /// Every frame (or every <paramref name="frameInterval"/>th) of the first video stream as numbered pictures in
+ /// <paramref name="outputPattern"/> (an image2 pattern such as <c>frame_%06d.png</c>), for focus stacking and
+ /// picking a shot.
/// </summary>
+ /// <remarks>
+ /// Each decoded frame is written once. The image2 muxer would otherwise pace frames to a constant rate and
+ /// duplicate (or drop) frames of a variable frame rate video (phones), so frames pass through with their own
+ /// timestamps; with an interval the select filter leaves gaps, which <c>vfr</c> keeps as they are.
+ /// </remarks>
public static List<string> FrameSequence(
string input,
string outputPattern,
@@ -182,27 +188,24 @@ internal static class MediaEncoding
int frameInterval = 1,
double fps = 0)
{
- List<string> args = [.. Common, "-i", input];
-
- var filters = new List<string>();
- if (video is not null && Math.Abs(video.SampleAspect - 1) > 0.01)
- filters.Add("scale=w='trunc(iw*sar/2)*2':h=ih,setsar=1");
-
- if (frameInterval > 1)
- filters.Add($"select='not(mod(n\\,{frameInterval}))'");
- else if (fps > 0)
- filters.Add($"fps={fps.ToString(CultureInfo.InvariantCulture)}");
-
- if (filters.Count > 0)
- args.AddRange(["-vf", string.Join(",", filters)]);
-
+ var select = frameInterval > 1 ? $"select='not(mod(n\\,{frameInterval}))'"
+ : fps > 0 ? $"fps={fps.ToString(CultureInfo.InvariantCulture)}"
+ : "null";
+ List<string> args =
+ [
+ .. Common, "-i", input, "-map", "0:V:0", "-vf", (video is null ? "" : SquarePixels(video)) + select,
+ ];
if (frameInterval > 1)
args.AddRange(["-fps_mode", "vfr"]);
+ else if (fps <= 0)
+ args.AddRange(["-fps_mode", "passthrough"]);
if (imageExt is "jpg" or "jpeg")
- args.AddRange(["-q:v", QualityToQv(quality).ToString(CultureInfo.InvariantCulture)]);
+ args.AddRange(["-c:v", "mjpeg", "-q:v", QualityToQv(quality).ToString(CultureInfo.InvariantCulture)]);
+ else
+ args.AddRange(["-c:v", "png"]);
- args.AddRange(["-progress", "pipe:1", outputPattern]);
+ args.AddRange(["-f", "image2", "-progress", "pipe:1", outputPattern]);
return args;
}
diff --git a/tests/Filee.Engines.Tests/MediaTests.cs b/tests/Filee.Engines.Tests/MediaTests.cs
index b5984635..8a88db27 100644
--- a/tests/Filee.Engines.Tests/MediaTests.cs
+++ b/tests/Filee.Engines.Tests/MediaTests.cs
@@ -910,12 +910,41 @@ public class MediaTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal("vfr", After(jpg, "-fps_mode"));
Assert.Equal("out_frames/frame_%06d.jpg", jpg[^1]);
+ Assert.Equal("0:V:0", After(jpg, "-map"));
+
+ // Every frame: each decoded frame once, never paced to a constant rate.
+ var every = MediaEncoding.FrameSequence("in.mp4", "out_frames/frame_%06d.png", "png", video);
+ Assert.Equal("passthrough", After(every, "-fps_mode"));
+ Assert.Equal("0:V:0", After(every, "-map"));
+ Assert.Equal("png", After(every, "-c:v"));
+
var png = MediaEncoding.FrameSequence("in.mp4", "out_frames/frame_%06d.png", "png", fps: 10);
Assert.DoesNotContain("-q:v", png);
Assert.Contains("fps=10", After(png, "-vf"));
Assert.Equal("out_frames/frame_%06d.png", png[^1]);
}
+ [Fact]
+ public async Task Every_frame_of_a_variable_frame_rate_video_is_saved_once()
+ {
+ SkipUnlessInstalled();
+ var folder = fx.NewFolder();
+ // One second at 30 fps, then three seconds at 10 fps: 60 frames, as a phone records a darker scene. Paced to a
+ // constant 30 fps the slow part would come out three times over (120 pictures).
+ var source = Path.Combine(folder, "vfr.mkv");
+ await RunAsync(Tools!.Value.Ffmpeg,
+ [
+ "-hide_banner", "-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=160x120:rate=30:duration=2",
+ "-vf", "setpts='if(lt(N,30),N/30,1+(N-30)/10)/TB'", "-fps_mode", "vfr", "-c:v", "libx264", "-pix_fmt", "yuv420p", source,
+ ]);
+ var decoded = (await ProbeAsync(source)).Video!.Frames;
+ Assert.Equal(60, decoded);
+
+ var frames = await ConvertAsync(source, "png", new MediaOptions { SaveEveryFrame = true });
+
+ Assert.Equal(decoded, Directory.GetFiles(frames, "*.png").Length);
+ }
+
[Fact]
public async Task Odd_sized_surround_video_is_encoded_everywhere()
{