C++ library for reading and writing MP4/M4A/AAC (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
Go to file
Martchus dd787e2f2a Extend Ogg related tests
* Test writing trailing zero lacing value if required
* Test adding cover
2021-08-18 22:48:17 +02:00
aac Fix spelling and typos 2021-07-02 11:12:39 +02:00
adts Fix spelling and typos 2021-07-02 11:12:39 +02:00
av1 Adapt to changes in c++utilities 2019-06-12 20:40:45 +02:00
avc Fix spelling and typos 2021-07-02 11:12:39 +02:00
avi Adapt to changes in c++utilities 2019-06-10 23:11:39 +02:00
cmake/scripts Fix usage of bibliographic language codes 2021-01-20 22:39:42 +01:00
doc Improve refs in "Adding new fields" doc 2019-01-10 13:37:13 +01:00
flac Fix warnings 2021-03-20 21:26:25 +01:00
id3 Prevent showing warning about encoding wrongly when making ID3v1 fields 2021-08-08 01:20:39 +02:00
ivf Add API to allow aborting overall/expensive parsing functions 2021-02-04 23:21:50 +01:00
matroska Avoid useless cases when assigning tag size (which is now std::uint64_t) 2021-08-08 00:05:11 +02:00
mp4 Avoid useless cases when assigning tag size (which is now std::uint64_t) 2021-08-08 00:05:11 +02:00
mpegaudio Fix spelling and typos 2021-07-02 11:12:39 +02:00
ogg Write zero lacing value in Ogg header if Vorbis comment size divisible by 255 2021-08-16 19:22:15 +02:00
opus Adapt to changes in c++utilities 2019-06-10 23:11:39 +02:00
scripts Extend Ogg related tests 2021-08-18 22:48:17 +02:00
testfiles Add MP4 testfile with chapters 2019-03-28 17:54:18 +01:00
tests Extend Ogg related tests 2021-08-18 22:48:17 +02:00
vorbis Improve warnings when parsing Vorbis comments 2021-08-16 18:37:44 +02:00
wav Fix warnings 2021-03-20 21:26:25 +01:00
.gitignore Apply clang-format later 2017-05-01 03:19:44 +02:00
CMakeLists.txt Extend Ogg related tests 2021-08-18 22:48:17 +02:00
LICENSE Initial commit 2015-04-22 19:11:19 +02:00
README.md Fix spelling and typos 2021-07-02 11:12:39 +02:00
abstractattachment.cpp Fix warnings 2021-03-20 21:26:25 +01:00
abstractattachment.h Fix warnings 2021-03-20 21:26:25 +01:00
abstractchapter.cpp Add API to allow aborting overall/expensive parsing functions 2021-02-04 23:21:50 +01:00
abstractchapter.h Add API to allow aborting overall/expensive parsing functions 2021-02-04 23:21:50 +01:00
abstractcontainer.cpp Fix spelling and typos 2021-07-02 11:12:39 +02:00
abstractcontainer.h Add API to allow aborting overall/expensive parsing functions 2021-02-04 23:21:50 +01:00
abstracttrack.cpp Avoid implicit conversion 2021-04-07 20:53:14 +02:00
abstracttrack.h Add API to allow aborting overall/expensive parsing functions 2021-02-04 23:21:50 +01:00
aspectratio.cpp Adapt to c++utilities v5 2019-05-04 16:04:21 +02:00
aspectratio.h Make many constructors explicit to avoid unintended implicit conversions 2020-12-05 20:48:57 +01:00
backuphelper.cpp Add better context to error messages when restoring the original file fails 2021-08-14 13:52:21 +02:00
backuphelper.h Adapt to changes in c++utilities 2019-06-10 23:11:39 +02:00
basicfileinfo.cpp Fix spelling and typos 2021-07-02 11:12:39 +02:00
basicfileinfo.h Improve passing paths 2021-04-23 22:16:43 +02:00
caseinsensitivecomparer.h Fix spelling and typos 2021-07-02 11:12:39 +02:00
diagnostics.cpp Use std::string_view where it makes sense 2021-01-30 21:53:06 +01:00
diagnostics.h Use std::string_view where it makes sense 2021-01-30 21:53:06 +01:00
exceptions.cpp Improve documentation 2020-11-05 19:52:09 +01:00
exceptions.h Use noexcept directly 2019-02-17 17:14:07 +01:00
fieldbasedtag.h Fix warnings 2021-03-20 21:26:25 +01:00
genericcontainer.h Fix spelling and typos 2021-07-02 11:12:39 +02:00
genericfileelement.h Fix spelling and typos 2021-07-02 11:12:39 +02:00
generictagfield.h Improve customization point for clearing tag field 2021-01-30 19:10:16 +01:00
global.h Adapt to changes in c++utilities 2019-06-12 20:40:45 +02:00
localeawarestring.cpp Apply clang-format 2018-03-07 01:17:50 +01:00
localeawarestring.h Add Locale class to deal with differently specified languages/countries 2020-12-16 17:48:08 +01:00
localehelper.cpp Fix spelling and typos 2021-07-02 11:12:39 +02:00
localehelper.h Remove unused optional header from localehelper.h 2021-01-20 20:10:56 +01:00
margin.h Make many constructors explicit to avoid unintended implicit conversions 2020-12-05 20:48:57 +01:00
mediafileinfo.cpp Add functions to return only tags which have actually been parsed 2021-08-08 00:04:12 +02:00
mediafileinfo.h Add functions to return only tags which have actually been parsed 2021-08-08 00:04:12 +02:00
mediaformat.cpp Fix spelling and typos 2021-07-02 11:12:39 +02:00
mediaformat.h Use std::string_view where it makes sense 2021-01-30 21:53:06 +01:00
positioninset.h Make many constructors explicit to avoid unintended implicit conversions 2020-12-05 20:48:57 +01:00
progressfeedback.cpp Document progress feedback 2018-06-03 20:32:35 +02:00
progressfeedback.h Add API to allow aborting overall/expensive parsing functions 2021-02-04 23:21:50 +01:00
settings.h Use helper for flag enum from c++utilities 2019-08-12 20:22:57 +02:00
signature.cpp Use std::string_view where it makes sense 2021-01-30 21:53:06 +01:00
signature.h Use std::string_view where it makes sense 2021-01-30 21:53:06 +01:00
size.cpp Fix spelling and typos 2021-07-02 11:12:39 +02:00
size.h Fix spelling and typos 2021-07-02 11:12:39 +02:00
tag.cpp Extend documentation of Tag::size() 2021-08-08 00:04:28 +02:00
tag.h Fix spelling and typos 2021-07-02 11:12:39 +02:00
tagtarget.cpp Use std::string_view where it makes sense 2021-01-30 21:53:06 +01:00
tagtarget.h Use std::string_view where it makes sense 2021-01-30 21:53:06 +01:00
tagvalue.cpp Fix TagValue::convertDescriptionEncoding() 2021-05-09 18:37:31 +02:00
tagvalue.h Fix spelling and typos 2021-07-02 11:12:39 +02:00

README.md

Tag Parser

C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags.

Supported formats

The tag library can read and write the following tag formats:

  • iTunes-style MP4/M4A tags (MP4-DASH is supported)
  • ID3v1 and ID3v2 tags
    • conversion between ID3v1 and different versions of ID3v2 is possible
  • Vorbis, Opus and FLAC comments in Ogg streams
    • cover art via "METADATA_BLOCK_PICTURE" is supported
  • Vorbis comments and "METADATA_BLOCK_PICTURE" in raw FLAC streams
  • Matroska/WebM tags and attachments

File layout options

Tag position

The library allows you to choose whether tags should be placed at the beginning or at the end of an MP4/Matroska file.

Padding

Padding allows adding additional tag information without rewriting the entire file or appending the tag. Usage of padding can be configured:

  • minimum/maximum padding: The file is rewritten if the padding would fall below/exceed the specified limits.
  • preferred padding: If the file needs to be rewritten the preferred padding is used.

Default value for minimum and maximum padding is zero. Hence the library will almost always have to rewrite the entire file to apply changes. To prevent this, set at least the maximum padding to a higher value.

It is also possible to force rewriting the entire file always.

Taking advantage of padding is currently not supported when dealing with Ogg streams (it is supported when dealing with raw FLAC streams).

Additional features

The library can also display technical information such as the ID, format, language, bitrate, duration, size, timestamps, sampling frequency, FPS and other information of the tracks.

It also allows to inspect and validate the element structure of MP4 and Matroska files.

Text encoding, Unicode support

The library is aware of different text encodings and can convert between different encodings using iconv.

Usage

This example shows how to read and write tag fields in a format-independent way:

#include <tagparser/mediafileinfo.h>
#include <tagparser/diagnostics.h>
#include <tagparser/progressfeedback.h>

// create a MediaFileInfo for high-level access to overall functionality of the library
auto fileInfo = MediaFileInfo();

// create container for errors, warnings, etc.
auto diag = Diagnostics();

// create handle to abort gracefully and get feedback during during long operations
auto progress = AbortableProgressFeedback([callback for status update], [callback for percentage-only updates]);

// open file (might throw ios_base::failure)
fileInfo.setPath("/path/to/some/file");
fileInfo.open();

// parse container format, tags, attachments and/or chapters as needed
// notes:
// - These functions might throw exceptions derived from ios_base::failure for IO errors and
//   populate diag with possibly critical parsing messages you definitely want to check in production
//   code.
// - Parsing a file can be expensive if the file is big or the disk IO is slow. You might want to
//   run it in a separate thread.
// - At this point the parser does not make much use of the progress object.
fileInfo.parseContainerFormat(diag, progress);
fileInfo.parseTags(diag, progress);
fileInfo.parseAttachments(diag, progress);
fileInfo.parseChapters(diag, progress);
fileInfo.parseEverything(diag, progress); // just use that one if you want all over the above

// get tag as an object derived from the Tag class
// notes:
// - In real code you might want to check how many tags are assigned or use
//   fileInfo.createAppropriateTags(…) to create tags as needed.
auto tag = fileInfo.tags().at(0);

// extract a field value and convert it to UTF-8 std::string (toString() might throw ConversionException)
#include <tagparser/tag.h>
#include <tagparser/tagvalue.h>
auto title = tag->value(TagParser::KnownField::Title).toString(TagParser::TagTextEncoding::Utf8);

// change a field value using an encoding suitable for the tag format
tag->setValue(KnownField::Album, TagValue("some UTF-8 string", TagTextEncoding::Utf8, tag->proposedTextEncoding()));

// get/remove/create attachments
#include <tagparser/abstractattachment.h>
if (auto *const container = fileInfo.container()) {
    for (auto i = 0, count = container->attachmentCount(); i != count; ++i) {
        auto attachment = container->attachment(i);
        if (attachment->mimeType() == "image/jpeg") {
            attachment->setIgnored(true); // remove existing attachment
        }
    }
    // create new attachment
    auto attachment = container->createAttachment();
    attachment->setName("cover.jpg");
    attachment->setFile(cover, diag);
}

// apply changes to the file on disk
// notes:
// - Might throw exception derived from TagParser::Failure for fatal processing error or ios_base::failure
//   for IO errors.
// - Applying changes can be expensive if the file is big or the disk IO is slow. You might want to
//   run it in a separate thread.
// - Use progress.tryToAbort() from another thread or an interrupt handler to abort gracefully without leaving
//   the file in an inconsistent state.
// - Be sure everything has been parsed before as the library needs to be aware of the whole file structure.
fileInfo.parseEverything(diag, progress);
fileInfo.applyChanges(diag, progress);

Summary

  • The most important class is TagParser::MediaFileInfo providing access to everything else.
  • IO errors are propagated via standard std::ios_base::failure.
  • Fatal processing errors are propagated by throwing a class derived from TagParser::Failure.
  • All operations which might generate warnings, non-fatal errors, ... take a TagParser::Diagnostics object to store those messages.
  • All operations which might be aborted or might provide progress feedback take a TagParser::AbortableProgressFeedback object for callbacks and aborting.
  • Field values are stored using TagParser::TagValue objects. Those objects erase the actual type similar to QVariant from the Qt framework. The documentation of TagParser::TagValue covers how different types and encodings are handled.

Further documentation

For more examples check out the command line interface of Tag Editor. API documentation can be generated using Doxygen with make tagparser_apidoc.

Bugs, stability

Bugs can be reported on GitHub.

It is recommend to create backups before editing because I can not test whether the library works with all kinds of files. (When forcing rewrite a backup is always created.)

Build instructions

The tagparser library depends on c++utilities and is built in the same way. It also depends on zlib, iso-codes and requires at least CMake 3.19. For checking integrity of testfiles, the OpenSSL crypto library is required.

TODOs

  • Support more formats (EXIF, PDF metadata, Theora, ...)
  • Support adding cue-sheet to FLAC files

More TODOs are tracked in the issue section at GitHub.