Skip to content

Commit

Permalink
Warn on multiple cover arts
Browse files Browse the repository at this point in the history
  • Loading branch information
fmang committed Feb 28, 2023
1 parent ec68f5c commit 92b320f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions src/opus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions t/opus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down

0 comments on commit 92b320f

Please sign in to comment.