Prefer first embedded image that contains the 'front' keyword
This commit is contained in:
@@ -281,9 +281,7 @@ namespace lms::av
|
||||
|
||||
const AVPacket& pkt{ avstream->attached_pic };
|
||||
|
||||
picture.data = reinterpret_cast<const std::byte*>(pkt.data);
|
||||
picture.dataSize = pkt.size;
|
||||
|
||||
picture.data = std::span{ reinterpret_cast<const std::byte*>(pkt.data), static_cast<std::size_t>(pkt.size) };
|
||||
func(picture, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
@@ -60,8 +61,7 @@ namespace lms::av
|
||||
struct Picture
|
||||
{
|
||||
std::string mimeType;
|
||||
const std::byte* data{}; // valid as long as IAudioFile exists
|
||||
std::size_t dataSize{};
|
||||
std::span<const std::byte> data; // valid as long as IAudioFile exists
|
||||
};
|
||||
|
||||
struct ContainerInfo
|
||||
|
||||
@@ -321,6 +321,18 @@ namespace lms::core::stringUtils
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string_view::size_type stringCaseInsensitiveContains(std::string_view str, std::string_view strtoFind)
|
||||
{
|
||||
if (str.empty() && strtoFind.empty())
|
||||
return true; // same as std
|
||||
|
||||
const auto it{ std::search(
|
||||
std::cbegin(str), std::cend(str),
|
||||
std::cbegin(strtoFind), std::cend(strtoFind),
|
||||
[](char chA, char chB) { return std::tolower(chA) == std::tolower(chB); }) };
|
||||
return (it != std::cend(str));
|
||||
}
|
||||
|
||||
void capitalize(std::string& str)
|
||||
{
|
||||
for (auto it{ std::begin(str) }; it != std::end(str); ++it)
|
||||
|
||||
@@ -62,6 +62,7 @@ namespace lms::core::stringUtils
|
||||
[[nodiscard]] std::string bufferToString(std::span<const unsigned char> data);
|
||||
|
||||
[[nodiscard]] bool stringCaseInsensitiveEqual(std::string_view strA, std::string_view strB);
|
||||
[[nodiscard]] std::string_view::size_type stringCaseInsensitiveContains(std::string_view str, std::string_view strtoFind);
|
||||
|
||||
void capitalize(std::string& str);
|
||||
|
||||
|
||||
@@ -322,4 +322,16 @@ namespace lms::core::stringUtils::tests
|
||||
EXPECT_FALSE(stringEndsWith("FooBar", "1FooBar"));
|
||||
EXPECT_FALSE(stringEndsWith("FooBar", "R"));
|
||||
}
|
||||
|
||||
TEST(StringUtils, stringCaseInsensitiveContains)
|
||||
{
|
||||
EXPECT_TRUE(stringCaseInsensitiveContains("FooBar", "Bar"));
|
||||
EXPECT_TRUE(stringCaseInsensitiveContains("FooBar", "bar"));
|
||||
EXPECT_TRUE(stringCaseInsensitiveContains("FooBar", "Foo"));
|
||||
EXPECT_TRUE(stringCaseInsensitiveContains("FooBar", "foo"));
|
||||
EXPECT_FALSE(stringCaseInsensitiveContains("something", "foo"));
|
||||
EXPECT_TRUE(stringCaseInsensitiveContains("FooBar", ""));
|
||||
EXPECT_TRUE(stringCaseInsensitiveContains("", ""));
|
||||
EXPECT_FALSE(stringCaseInsensitiveContains("", "Foo"));
|
||||
}
|
||||
} // namespace lms::core::stringUtils::tests
|
||||
@@ -19,12 +19,15 @@
|
||||
|
||||
#include "ArtworkService.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
|
||||
#include "av/IAudioFile.hpp"
|
||||
#include "av/Types.hpp"
|
||||
#include "core/IConfig.hpp"
|
||||
#include "core/ILogger.hpp"
|
||||
#include "core/String.hpp"
|
||||
#include "core/Utils.hpp"
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/Db.hpp"
|
||||
@@ -68,21 +71,47 @@ namespace lms::cover
|
||||
|
||||
std::unique_ptr<image::IEncodedImage> ArtworkService::getFromAvMediaFile(const av::IAudioFile& input, std::optional<image::ImageSize> width) const
|
||||
{
|
||||
struct CandidatePicture
|
||||
{
|
||||
av::Picture picture;
|
||||
bool isFront{};
|
||||
std::size_t index;
|
||||
|
||||
// > means is better candidate
|
||||
bool operator>(const CandidatePicture& other) const
|
||||
{
|
||||
if (!isFront && other.isFront)
|
||||
return false;
|
||||
if (isFront && !other.isFront)
|
||||
return true;
|
||||
|
||||
return index < other.index;
|
||||
}
|
||||
};
|
||||
|
||||
auto metadataHasFrontKeyword{ [](const av::IAudioFile::MetadataMap& metadata) {
|
||||
return std::any_of(std::cbegin(metadata), std::cend(metadata), [](const auto& keyValue) { return core::stringUtils::stringCaseInsensitiveContains(keyValue.second, "front"); });
|
||||
} };
|
||||
|
||||
std::vector<CandidatePicture> candidatePictures;
|
||||
std::size_t pictureIndex{};
|
||||
input.visitAttachedPictures([&](const av::Picture& picture, const av::IAudioFile::MetadataMap& metadata) {
|
||||
candidatePictures.emplace_back(picture, metadataHasFrontKeyword(metadata), pictureIndex++);
|
||||
});
|
||||
std::stable_sort(std::begin(candidatePictures), std::end(candidatePictures), std::greater<>());
|
||||
|
||||
std::unique_ptr<image::IEncodedImage> image;
|
||||
|
||||
input.visitAttachedPictures([&](const av::Picture& picture, const av::IAudioFile::MetadataMap& /* metadata */) {
|
||||
if (image)
|
||||
return;
|
||||
|
||||
for (const CandidatePicture& candidatePicture : candidatePictures)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!width)
|
||||
{
|
||||
image = image::readImage(std::span{ picture.data, picture.dataSize }, picture.mimeType);
|
||||
image = image::readImage(candidatePicture.picture.data, candidatePicture.picture.mimeType);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto rawImage{ image::decodeImage(std::span{ picture.data, picture.dataSize }) };
|
||||
auto rawImage{ image::decodeImage(candidatePicture.picture.data) };
|
||||
rawImage->resize(*width);
|
||||
image = image::encodeToJPEG(*rawImage, _jpegQuality);
|
||||
}
|
||||
@@ -91,7 +120,7 @@ namespace lms::cover
|
||||
{
|
||||
LMS_LOG(COVER, ERROR, "Cannot read embedded cover: " << e.what());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user