encode: implement --oset-metadata, and --oremove-metadata

This commit introduces a new --oset-metadata key-value-list option,
allowing the user to specify output metadata when encoding
(eg. --oset-metadata=title="Hello",comment="World").

A second option --oremove-metadata is added to exclude existing metadata
from the output file (assuming --ocopy-metadata is enabled).

Not all output formats support all tags, but luckily libavcodec
simply discards unsupported keys.
This commit is contained in:
TheAMM 2017-12-16 14:21:18 +02:00 committed by Kevin Mitchell
parent c8d955571d
commit 8b7da7a8e5
3 changed files with 48 additions and 1 deletions

View File

@ -153,3 +153,24 @@ You can encode files from one format/codec to another using this facility.
``--no-ocopy-metadata``
Turns off copying of metadata from input files to output files when
encoding (which is enabled by default).
``--oset-metadata=<metadata-tag[,metadata-tag,...]>``
Specifies metadata to include in the output file.
Supported keys vary between output formats. For example, Matroska (MKV) and
FLAC allow almost arbitrary keys, while support in MP4 and MP3 is more
limited.
.. admonition:: Example
"``--oset-metadata=title="Output title",comment="Another tag"``"
adds a title and a comment to the output file.
``--oremove-metadata=<metadata-tag[,metadata-tag,...]>``
Specifies metadata to exclude from the output file when copying from the
input file.
.. admonition:: Example
"``--oremove-metadata=comment,genre``"
excludes copying of the the comment and genre tags to the output
file.

View File

@ -50,6 +50,8 @@ struct encode_opts {
int video_first;
int audio_first;
int copy_metadata;
char **set_metadata;
char **remove_metadata;
};
// interface for mplayer.c

View File

@ -56,6 +56,8 @@ const struct m_sub_options encode_config = {
OPT_FLAG("ovfirst", video_first, M_OPT_FIXED),
OPT_FLAG("oafirst", audio_first, M_OPT_FIXED),
OPT_FLAG("ocopy-metadata", copy_metadata, M_OPT_FIXED),
OPT_KEYVALUELIST("oset-metadata", set_metadata, M_OPT_FIXED),
OPT_STRINGLIST("oremove-metadata", remove_metadata, M_OPT_FIXED),
{0}
},
.size = sizeof(struct encode_opts),
@ -278,8 +280,30 @@ struct encode_lavc_context *encode_lavc_init(struct encode_opts *options,
void encode_lavc_set_metadata(struct encode_lavc_context *ctx,
struct mp_tags *metadata)
{
if (ctx->options->copy_metadata)
if (ctx->options->copy_metadata) {
ctx->metadata = metadata;
} else {
ctx->metadata = talloc_zero(ctx, struct mp_tags);
}
if (ctx->options->set_metadata) {
char **kv = ctx->options->set_metadata;
// Set all user-provided metadata tags
for (int n = 0; kv[n * 2]; n++) {
MP_VERBOSE(ctx, "setting metadata value '%s' for key '%s'\n",
kv[n*2 + 0], kv[n*2 +1]);
mp_tags_set_str(ctx->metadata, kv[n*2 + 0], kv[n*2 +1]);
}
}
if (ctx->options->remove_metadata) {
char **k = ctx->options->remove_metadata;
// Remove all user-provided metadata tags
for (int n = 0; k[n]; n++) {
MP_VERBOSE(ctx, "removing metadata key '%s'\n", k[n]);
mp_tags_remove_str(ctx->metadata, k[n]);
}
}
}
int encode_lavc_start(struct encode_lavc_context *ctx)