Update the video conversion: Ensure the correct file order

This commit is contained in:
2026-04-16 22:32:03 +10:00
parent 308843bc6b
commit 2abfd6e87c
2 changed files with 91 additions and 6 deletions

View File

@@ -2021,8 +2021,23 @@ namespace ANSCENTER
return false;
}
// Sort for consistent ordering
std::sort(imageFiles.begin(), imageFiles.end());
// Sort by filename (lexicographic, case-sensitive) so playback order
// follows the on-disk name order. Sorting by std::filesystem::path::
// filename() is explicit and robust even if cv::glob ever returns
// entries with different path prefixes or separators.
// Example input:
// 20260114_181439.703.jpg
// 20260114_181439.703_000.jpg
// 20260114_181439.703_001.jpg
// 20260114_181439.703_002.jpg
// 20260114_181439.803.jpg
// Byte-wise '.' (0x2E) < '_' (0x5F), so "...703.jpg" correctly
// precedes "...703_000.jpg".
std::sort(imageFiles.begin(), imageFiles.end(),
[](const cv::String& a, const cv::String& b) {
return std::filesystem::path(a).filename().string() <
std::filesystem::path(b).filename().string();
});
// Cap at 5 minutes max duration
const int maxFrames = fps * 300;
@@ -2307,7 +2322,13 @@ namespace ANSCENTER
std::cerr << "Error: No images found in folder: " << imageFolder << std::endl;
return false;
}
std::sort(imageFiles.begin(), imageFiles.end());
// Sort by filename (lexicographic, case-sensitive). See matching
// comment in ImagesToMP4() for rationale and ordering example.
std::sort(imageFiles.begin(), imageFiles.end(),
[](const cv::String& a, const cv::String& b) {
return std::filesystem::path(a).filename().string() <
std::filesystem::path(b).filename().string();
});
// Cap at 5 minutes max duration
const int maxFrames = fps * 300;
@@ -2712,7 +2733,13 @@ namespace ANSCENTER
std::cerr << "Error: No images found in folder: " << imageFolder << std::endl;
return false;
}
std::sort(imageFiles.begin(), imageFiles.end());
// Sort by filename (lexicographic, case-sensitive). See matching
// comment in ImagesToMP4() for rationale and ordering example.
std::sort(imageFiles.begin(), imageFiles.end(),
[](const cv::String& a, const cv::String& b) {
return std::filesystem::path(a).filename().string() <
std::filesystem::path(b).filename().string();
});
const int maxFrames = fps * 300;
if (static_cast<int>(imageFiles.size()) > maxFrames) {
@@ -5551,7 +5578,7 @@ extern "C" __declspec(dllexport) int ANSCV_ImagesToMP4_S(
const char* imageFolder,
const char* outputVideoPath,
int maxWidth, int fps) {
return ANSCV_ImagesToMP4FF_S(imageFolder,outputVideoPath,maxWidth,fps);
try {
if (!imageFolder || strlen(imageFolder) == 0) {
std::cerr << "Error: Invalid image folder path!" << std::endl;