Skip to content

Commit

Permalink
Improve image sequence parsing (#35)
Browse files Browse the repository at this point in the history
* (cmake) Remove unnecessary message
* Rework seq parsing to handle indices anywhere in stem
  • Loading branch information
csparker247 authored Jan 6, 2021
1 parent a669faa commit 3f08329
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
4 changes: 2 additions & 2 deletions apps/src/EncodingQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ void EncodingQueue::insert(std::vector<fs::path> files, const EncodeSettings& s)
for (const auto& f : files) {
try {
tempQueue.emplace_back(QueueItem::New(f, s));
} catch (const std::runtime_error& e) {
qDebug() << e.what() << ":" << f.c_str();
} catch (const std::exception& e) {
qDebug() << "QueueItem error ::" << f.c_str() << "::" << e.what();
}
}

Expand Down
1 change: 0 additions & 1 deletion cmake/FindDeployQt5.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function(qt5_deploy_bundle TARGET)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
set(DeployFlags -no-strip)
endif()
message("Deploy flags: ${DeployFlags}")
add_custom_command(
TARGET ${TARGET} POST_BUILD
COMMAND "${DeployQt5_EXECUTABLE}" "$<TARGET_BUNDLE_DIR:${TARGET}>" "${DeployFlags}"
Expand Down
20 changes: 18 additions & 2 deletions libdropenc/include/ffdropenc/QueueItem.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <exception>
#include <filesystem>
#include <iostream>

Expand All @@ -13,6 +14,20 @@
namespace ffdropenc
{

class QueueItemException : public std::exception
{
public:
explicit QueueItemException(const char* msg) : msg_(msg) {}
explicit QueueItemException(std::string msg) : msg_(std::move(msg)) {}
[[nodiscard]] const char* what() const noexcept override
{
return msg_.c_str();
}

protected:
std::string msg_;
};

class QueueItem
{
public:
Expand Down Expand Up @@ -71,8 +86,9 @@ class QueueItem
// image sequence
static Type determine_type_(const std::filesystem::path& p);
void convert_to_seq_();
std::string nameStem_;
std::string seqNumStr_;
std::string stemPrefix_;
std::string stemSeqNum_;
std::string stemSuffix_;
size_t determine_starting_index_() const;

// encoding parameters
Expand Down
51 changes: 34 additions & 17 deletions libdropenc/src/QueueItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <cmath>
#include <regex>

#include <nlohmann/json.hpp>

#include "ffdropenc/Filesystem.hpp"

using namespace ffdropenc;
Expand Down Expand Up @@ -40,7 +38,7 @@ QueueItem::QueueItem(std::filesystem::path path, EncodeSettings settings)
convert_to_seq_();
break;
case Type::Undefined:
throw std::runtime_error("Unable to determine type");
throw QueueItemException("Unable to determine type");
}
}

Expand Down Expand Up @@ -76,37 +74,56 @@ std::filesystem::path QueueItem::outputPath() const
// Convert this Item into an Img Sequence
void QueueItem::convert_to_seq_()
{
// TODO: This only works if numerical pattern is at end of filename
std::regex digits(R"(\d+)");
auto stem = inputPath_.stem().string();
auto last_letter_pos = stem.find_last_not_of("0123456789");
nameStem_ = stem.substr(0, last_letter_pos + 1);
seqNumStr_ = stem.substr(last_letter_pos + 1);
auto it = std::sregex_iterator(stem.begin(), stem.end(), digits);
auto end = std::sregex_iterator();
auto numMatches = std::distance(it, end);
if (numMatches == 0) {
throw QueueItemException("Zero numerical indices in stem");
} else if (numMatches > 1) {
std::string msg("Multiple numerical indices in stem: ");
size_t count{0};
for (; it != end; it++, count++) {
if (count > 0) {
msg += ", ";
}
msg += it->str();
}
throw QueueItemException(msg);
}

// Get the pieces
stemPrefix_ = it->prefix().str();
stemSeqNum_ = it->str();
stemSuffix_ = it->suffix().str();

// Set the output filename
outputFileName_ = (nameStem_.empty())
? inputPath_.parent_path().stem().string()
: nameStem_;
outputFileName_ = stemPrefix_ + stemSuffix_;
if (outputFileName_.empty()) {
outputFileName_ = inputPath_.parent_path().stem().string();
}

// Convert seqNumber to the %nd format
auto seqNumber = std::to_string(seqNumStr_.length());
auto seqNumber = std::to_string(stemSeqNum_.length());
if (seqNumber.length() < 2) {
seqNumber.insert(0, "0");
}
seqNumber = "%" + seqNumber + "d";

// Final file name
auto completeStem = nameStem_ + seqNumber + inputPath_.extension().string();
inputPath_ = inputPath_.parent_path() / completeStem;
auto newStem =
stemPrefix_ + seqNumber + stemSuffix_ + inputPath_.extension().string();
inputPath_ = inputPath_.parent_path() / newStem;
}

size_t QueueItem::determine_starting_index_() const
{
// TODO: This only works if numerical pattern is at end of filename

// Get list of all paths matching prefix in parent path
std::regex prefix{nameStem_ + "([0-9]+)"};
auto numSeqChars = std::to_string(stemSeqNum_.length());
std::regex prefix{stemPrefix_ + "(\\d{" + numSeqChars + "})" + stemSuffix_};
std::smatch match;
size_t startIdx = std::stoull(seqNumStr_);
size_t startIdx = std::stoull(stemSeqNum_);
for (const auto& it : fs::directory_iterator(inputPath_.parent_path())) {
// Skip entries that aren't files
if (not it.is_regular_file()) {
Expand Down

0 comments on commit 3f08329

Please sign in to comment.