Warn on multiple cover arts

This commit is contained in:
Frédéric Mangano 2023-02-28 15:39:04 +09:00
parent ec68f5c0e9
commit 92b320f9d9
3 changed files with 11 additions and 3 deletions

View File

@ -402,7 +402,7 @@ static void output_cover(const ot::opus_tags& tags, const ot::options &opt)
{
std::optional<ot::picture> cover = extract_cover(tags);
if (!cover) {
fputs("warning: no cover found.\n", stderr);
fputs("warning: No cover found.\n", stderr);
return;
}

View File

@ -149,6 +149,9 @@ ot::picture::picture(std::string block)
picture_data = std::string_view(reinterpret_cast<const char*>(bytes + pic_offset + 4), pic_size);
}
/**
* \todo Take into account the picture types (first 4 bytes of the tag value).
*/
std::optional<ot::picture> ot::extract_cover(const ot::opus_tags& tags)
{
static const std::string_view prefix = "METADATA_BLOCK_PICTURE="sv;
@ -157,6 +160,11 @@ std::optional<ot::picture> ot::extract_cover(const ot::opus_tags& tags)
if (cover_tag == tags.comments.end())
return {}; // No cover art.
auto extra_cover_tag = std::find_if(std::next(cover_tag), tags.comments.end(), is_cover);
if (extra_cover_tag != tags.comments.end())
fputs("warning: Found multiple covers; only the first will be extracted."
" Please report your use case if you need a finer selection.\n", stderr);
std::string_view cover_value = *cover_tag;
cover_value.remove_prefix(prefix.size());
return picture(decode_base64(cover_value));

View File

@ -148,7 +148,7 @@ static void extract_cover()
"\x00\x00\x00\x0C" "Picture data";
ot::opus_tags tags;
tags.comments.push_front("METADATA_BLOCK_PICTURE=" + ot::encode_base64(picture_data));
tags.comments = { "METADATA_BLOCK_PICTURE=" + ot::encode_base64(picture_data) };
std::optional<ot::picture> cover = ot::extract_cover(tags);
if (!cover)
throw failure("could not extract the cover");
@ -158,7 +158,7 @@ static void extract_cover()
throw failure("bad extracted picture data");
std::string_view truncated_data = picture_data.substr(0, picture_data.size() - 1);
tags.comments.push_front("METADATA_BLOCK_PICTURE=" + ot::encode_base64(truncated_data));
tags.comments = { "METADATA_BLOCK_PICTURE=" + ot::encode_base64(truncated_data) };
try {
ot::extract_cover(tags);
throw failure("accepted a bad picture block");