From 49bb94841ef6e468ba6dfcade5321c6b814edb3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano?= Date: Thu, 4 May 2023 11:23:51 +0900 Subject: [PATCH] Add option --set-vendor --- README.md | 1 + opustags.1 | 4 ++++ src/cli.cc | 15 +++++++++++++++ src/opustags.h | 6 ++++++ t/opustags.t | 6 +++++- 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 972330e..3752922 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Documentation --output-cover FILE extract and save the cover art, if any --set-cover FILE sets the cover art --vendor print the vendor string + --set-vendor VALUE set the vendor string --raw disable encoding conversion See the man page, `opustags.1`, for extensive documentation. diff --git a/opustags.1 b/opustags.1 index 77a9af5..9237c55 100644 --- a/opustags.1 +++ b/opustags.1 @@ -125,6 +125,10 @@ the front cover really matters. opustags can currently only handle one front cov Print the vendor string from the OpusTags packet and do nothing else. Standard tags operations are not supported when specifying this flag. .TP +.B \-\-set-vendor \fIVALUE\fP +Replace the vendor string by the specified value. This action can be performed alongside tag +edition. +.TP .B \-\-raw OpusTags metadata should always be encoded in UTF-8, as per RFC 7845. However, some files may be corrupted or possibly even contain intentional binary data. In that case, --raw lets you edit that diff --git a/src/cli.cc b/src/cli.cc index bd5935f..59bc226 100644 --- a/src/cli.cc +++ b/src/cli.cc @@ -39,6 +39,7 @@ Options: --output-cover FILE extract and save the cover art, if any --set-cover FILE sets the cover art --vendor print the vendor string + --set-vendor VALUE set the vendor string --raw disable encoding conversion See the man page for extensive documentation. @@ -58,6 +59,7 @@ static struct option getopt_options[] = { {"output-cover", required_argument, 0, 'c'}, {"set-cover", required_argument, 0, 'C'}, {"vendor", no_argument, 0, 'v'}, + {"set-vendor", required_argument, 0, 'V'}, {"raw", no_argument, 0, 'r'}, {NULL, 0, 0, 0} }; @@ -71,6 +73,7 @@ ot::options ot::parse_options(int argc, char** argv, FILE* comments_input) std::list local_to_delete; // opt.to_delete before UTF-8 conversion. bool set_all = false; std::optional set_cover; + std::optional set_vendor; opt = {}; if (argc == 1) throw status {st::bad_arguments, "No arguments specified. Use -h for help."}; @@ -128,6 +131,11 @@ ot::options ot::parse_options(int argc, char** argv, FILE* comments_input) case 'v': opt.print_vendor = true; break; + case 'V': + if (set_vendor) + throw status {st::bad_arguments, "Cannot specify --set-vendor more than once."}; + set_vendor = optarg; + break; case 'r': opt.raw = true; break; @@ -166,12 +174,16 @@ ot::options ot::parse_options(int argc, char** argv, FILE* comments_input) std::back_inserter(opt.to_add), cast_to_utf8); std::transform(local_to_delete.begin(), local_to_delete.end(), std::back_inserter(opt.to_delete), cast_to_utf8); + if (set_vendor) + opt.set_vendor = cast_to_utf8(*set_vendor); } else { try { std::transform(local_to_add.begin(), local_to_add.end(), std::back_inserter(opt.to_add), encode_utf8); std::transform(local_to_delete.begin(), local_to_delete.end(), std::back_inserter(opt.to_delete), encode_utf8); + if (set_vendor) + opt.set_vendor = encode_utf8(*set_vendor); } catch (const ot::status& rc) { throw status {st::bad_arguments, "Could not encode argument into UTF-8: " + rc.message}; } @@ -365,6 +377,9 @@ void ot::delete_comments(std::list& comments, const std::u8string /** Apply the modifications requested by the user to the opustags packet. */ static void edit_tags(ot::opus_tags& tags, const ot::options& opt) { + if (opt.set_vendor) + tags.vendor = *opt.set_vendor; + if (opt.delete_all) { tags.comments.clear(); } else for (const std::u8string& name : opt.to_delete) { diff --git a/src/opustags.h b/src/opustags.h index 7d6ab04..e83ad9f 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -522,6 +522,12 @@ struct options { * Option: --vendor */ bool print_vendor = false; + /** + * Replace the vendor string by the one specified by the user. + * + * Option: --set-vendor + */ + std::optional set_vendor; /** * Disable encoding conversions. OpusTags are specified to always be encoded as UTF-8, but * if for some reason a specific file contains binary tags that someone would like to diff --git a/t/opustags.t b/t/opustags.t index ddd6e0d..039f8f6 100755 --- a/t/opustags.t +++ b/t/opustags.t @@ -4,7 +4,7 @@ use strict; use warnings; use utf8; -use Test::More tests => 60; +use Test::More tests => 62; use Test::Deep qw(cmp_deeply re); use Digest::MD5; @@ -323,3 +323,7 @@ END_OUT # Vendor string is_deeply(opustags(qw(--vendor gobble.opus)), ["Lavf58.12.100\n", '', 0], 'print the vendor string'); + +is_deeply(opustags(qw(--set-vendor opustags gobble.opus -o out.opus)), ['', '', 0], 'set the vendor string'); +is_deeply(opustags(qw(--vendor out.opus)), ["opustags\n", '', 0], 'the vendor string was updated'); +unlink('out.opus');