Tag Parser 12.5.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mediafileinfo.h
Go to the documentation of this file.
1#ifndef TAG_PARSER_MEDIAINFO_H
2#define TAG_PARSER_MEDIAINFO_H
3
5#include "./basicfileinfo.h"
6#include "./settings.h"
7#include "./signature.h"
8
9#include <cstdint>
10#include <memory>
11#include <unordered_set>
12#include <vector>
13
14#include <c++utilities/misc/flagenumclass.h>
15
16namespace TagParser {
17
18class Tag;
19class Id3v1Tag;
20class Id3v2Tag;
21class Mp4Container;
22class Mp4Atom;
23class Mp4Tag;
25class OggContainer;
26class EbmlElement;
27class MatroskaTag;
28class AbstractTrack;
29class VorbisComment;
30class Diagnostics;
32
33enum class MediaType : unsigned int;
34enum class TagType : unsigned int;
35
46
50enum class MediaFileStructureFlags : std::uint64_t {
53};
54
58enum class MediaFileHandlingFlags : std::uint64_t {
61 = (1 << 0),
62 ForceRewrite = (1 << 1),
63 ForceTagPosition = (1 << 2),
64 ForceIndexPosition = (1 << 3),
69 ConvertTotalFields = (1 << 11),
72};
73
74} // namespace TagParser
75
78
79namespace TagParser {
80
82
84public:
85 // constructor, destructor
86 explicit MediaFileInfo();
87 explicit MediaFileInfo(std::string_view path);
88 explicit MediaFileInfo(std::string &&path);
89 MediaFileInfo(const MediaFileInfo &) = delete;
91 ~MediaFileInfo() override;
92
93 // methods to parse file
96 void parseTags(Diagnostics &diag, AbortableProgressFeedback &progress);
100
101 // methods to apply changes
103
104 // methods to get parsed information regarding ...
105 // ... the container
107 std::string_view containerFormatName() const;
108 std::string_view containerFormatAbbreviation() const;
109 std::string_view containerFormatSubversion() const;
110 std::string_view mimeType() const;
111 std::uint64_t containerOffset() const;
112 std::uint64_t paddingSize() const;
113 std::uint64_t effectiveSize() const;
116 // ... the chapters
118 std::vector<AbstractChapter *> chapters() const;
119 bool areChaptersSupported() const;
120 // ... the attachments
122 std::vector<AbstractAttachment *> attachments() const;
123 bool areAttachmentsSupported() const;
124 // ... the tracks
126 std::size_t trackCount() const;
127 std::vector<AbstractTrack *> tracks() const;
128 bool hasTracksOfType(TagParser::MediaType type) const;
129 CppUtilities::TimeSpan duration() const;
130 double overallAverageBitrate() const;
131 std::unordered_set<std::string> availableLanguages(TagParser::MediaType type = TagParser::MediaType::Audio) const;
132 std::string technicalSummary() const;
133 bool areTracksSupported() const;
134 // ... the tags
136 bool hasId3v1Tag() const;
137 bool hasId3v2Tag() const;
138 bool hasAnyTag() const;
139 Id3v1Tag *id3v1Tag() const;
140 const std::vector<std::unique_ptr<Id3v2Tag>> &id3v2Tags() const;
141 void tags(std::vector<Tag *> &tags) const;
142 std::vector<Tag *> tags() const;
143 void parsedTags(std::vector<Tag *> &tags) const;
144 std::vector<Tag *> parsedTags() const;
145 Mp4Tag *mp4Tag() const;
146 const std::vector<std::unique_ptr<MatroskaTag>> &matroskaTags() const;
148 bool areTagsSupported() const;
149
150 // methods to create/remove tags
152 bool removeId3v1Tag();
154 bool removeId3v2Tag(Id3v2Tag *tag);
155 bool removeAllId3v2Tags();
157 bool removeTag(Tag *tag);
158 void removeAllTags();
159 void mergeId3v2Tags();
160 bool id3v1ToId3v2();
161 bool id3v2ToId3v1();
163 bool removeVorbisComment();
164 void clearParsingResults();
165 void reportPaddingSizeChanged(std::uint64_t newPaddingSize);
166
167 // methods to get, set object behaviour
168 const std::string &backupDirectory() const;
169 void setBackupDirectory(std::string_view backupDirectory);
170 void setBackupDirectory(std::string &&backupDirectory);
171 const std::string &saveFilePath() const;
172 void setSaveFilePath(std::string_view saveFilePath);
173 void setSaveFilePath(std::string &&saveFilePath);
174 const std::string &writingApplication() const;
175 void setWritingApplication(std::string_view writingApplication);
178 bool isForcingFullParse() const;
179 void setForceFullParse(bool forceFullParse);
180 bool isForcingRewrite() const;
181 void setForceRewrite(bool forceRewrite);
182 std::size_t minPadding() const;
183 void setMinPadding(std::size_t minPadding);
184 std::size_t maxPadding() const;
185 void setMaxPadding(std::size_t maxPadding);
186 std::size_t preferredPadding() const;
187 void setPreferredPadding(std::size_t preferredPadding);
190 bool forceTagPosition() const;
194 bool forceIndexPosition() const;
196 std::uint64_t maxFullParseSize() const;
197 void setMaxFullParseSize(std::uint64_t maxFullParseSize);
198
199 // helper functions
200 static void writePadding(std::ostream &outputStream, uint64_t size);
201
202protected:
203 void invalidated() override;
204
205private:
206 // private methods internally used when rewriting the file to apply new tag information
207 // currently only the makeMp3File() methods is present; corresponding methods for
208 // other formats are outsourced to container classes
209 void makeMp3File(Diagnostics &diag, AbortableProgressFeedback &progress);
210
211 // fields related to the container
212 ParsingStatus m_containerParsingStatus;
213 ContainerFormat m_containerFormat;
214 std::streamoff m_containerOffset;
215 std::uint64_t m_paddingSize;
216 std::uint64_t m_effectiveSize;
217 std::vector<std::streamoff> m_actualId3v2TagOffsets;
218 std::unique_ptr<AbstractContainer> m_container;
219 MediaFileStructureFlags m_fileStructureFlags;
220
221 // fields related to the tracks
222 ParsingStatus m_tracksParsingStatus;
223 std::unique_ptr<AbstractTrack> m_singleTrack;
224
225 // fields related to the tag
226 ParsingStatus m_tagsParsingStatus;
227 std::unique_ptr<Id3v1Tag> m_id3v1Tag;
228 std::vector<std::unique_ptr<Id3v2Tag>> m_id3v2Tags;
229
230 // fields related to the chapters and the attachments
231 ParsingStatus m_chaptersParsingStatus;
232 ParsingStatus m_attachmentsParsingStatus;
233
234 // fields specifying object behaviour
235 std::string m_backupDirectory;
236 std::string m_saveFilePath;
237 std::string m_writingApplication;
238 std::size_t m_minPadding;
239 std::size_t m_maxPadding;
240 std::size_t m_preferredPadding;
241 ElementPosition m_tagPosition;
242 ElementPosition m_indexPosition;
243 MediaFileHandlingFlags m_fileHandlingFlags;
244 std::uint64_t m_maxFullParseSize;
245 std::unique_ptr<MediaFileInfoPrivate> m_p;
246};
247
252{
253 return m_containerParsingStatus;
254}
255
263{
264 return m_containerFormat;
265}
266
277inline std::string_view MediaFileInfo::containerFormatName() const
278{
279 return TagParser::containerFormatName(m_containerFormat);
280}
281
292inline std::string_view MediaFileInfo::containerFormatSubversion() const
293{
294 return TagParser::containerFormatSubversion(m_containerFormat);
295}
296
300inline std::uint64_t MediaFileInfo::containerOffset() const
301{
302 return static_cast<std::uint64_t>(m_containerOffset);
303}
304
308inline std::uint64_t MediaFileInfo::paddingSize() const
309{
310 return m_paddingSize;
311}
312
317inline std::uint64_t MediaFileInfo::effectiveSize() const
318{
319 return m_effectiveSize;
320}
321
326{
327 return m_tagsParsingStatus;
328}
329
334{
335 return m_tracksParsingStatus;
336}
337
346inline std::size_t MediaFileInfo::trackCount() const
347{
348 return m_singleTrack ? 1 : (m_container ? m_container->trackCount() : 0);
349}
350
355{
356 return m_chaptersParsingStatus;
357}
358
363{
364 return m_attachmentsParsingStatus;
365}
366
370inline bool MediaFileInfo::hasId3v1Tag() const
371{
372 return m_id3v1Tag != nullptr;
373}
374
378inline bool MediaFileInfo::hasId3v2Tag() const
379{
380 return !m_id3v2Tags.empty();
381}
382
391{
392 return m_id3v1Tag.get();
393}
394
402inline const std::vector<std::unique_ptr<Id3v2Tag>> &MediaFileInfo::id3v2Tags() const
403{
404 return m_id3v2Tags;
405}
406
411inline void MediaFileInfo::reportPaddingSizeChanged(uint64_t newPaddingSize)
412{
413 m_paddingSize = newPaddingSize;
414}
415
421inline const std::string &MediaFileInfo::backupDirectory() const
422{
423 return m_backupDirectory;
424}
425
431{
432 m_backupDirectory = backupDirectory;
433}
434
440{
441 m_backupDirectory = std::move(backupDirectory);
442}
443
448inline const std::string &MediaFileInfo::saveFilePath() const
449{
450 return m_saveFilePath;
451}
452
467inline void MediaFileInfo::setSaveFilePath(std::string_view saveFilePath)
468{
469 m_saveFilePath = saveFilePath;
470}
471
476{
477 m_saveFilePath = std::move(saveFilePath);
478}
479
485inline const std::string &MediaFileInfo::writingApplication() const
486{
487 return m_writingApplication;
488}
489
497{
498 m_writingApplication = writingApplication;
499}
500
512{
513 return m_container.get();
514}
515
520{
521 return m_fileHandlingFlags;
522}
523
528{
529 m_fileHandlingFlags = flags;
530}
531
541{
542 return m_fileHandlingFlags & MediaFileHandlingFlags::ForceFullParse;
543}
544
550inline void MediaFileInfo::setForceFullParse(bool forceFullParse)
551{
552 CppUtilities::modFlagEnum(m_fileHandlingFlags, MediaFileHandlingFlags::ForceFullParse, forceFullParse);
553}
554
559{
560 return m_fileHandlingFlags & MediaFileHandlingFlags::ForceRewrite;
561}
562
566inline void MediaFileInfo::setForceRewrite(bool forceRewrite)
567{
568 CppUtilities::modFlagEnum(m_fileHandlingFlags, MediaFileHandlingFlags::ForceRewrite, forceRewrite);
569}
570
583inline std::size_t MediaFileInfo::minPadding() const
584{
585 return m_minPadding;
586}
587
594{
595 m_minPadding = minPadding;
596}
597
611inline std::size_t MediaFileInfo::maxPadding() const
612{
613 return m_maxPadding;
614}
615
622{
623 m_maxPadding = maxPadding;
624}
625
632inline std::size_t MediaFileInfo::preferredPadding() const
633{
634 return m_preferredPadding;
635}
636
643{
644 m_preferredPadding = preferredPadding;
645}
646
653{
654 return m_tagPosition;
655}
656
668{
669 m_tagPosition = tagPosition;
670}
671
678{
679 return m_fileHandlingFlags & MediaFileHandlingFlags::ForceTagPosition;
680}
681
688{
689 CppUtilities::modFlagEnum(m_fileHandlingFlags, MediaFileHandlingFlags::ForceTagPosition, forceTagPosition);
690}
691
698{
699 return m_indexPosition;
700}
701
711
718{
719 return m_fileHandlingFlags & MediaFileHandlingFlags::ForceIndexPosition;
720}
721
728{
729 CppUtilities::modFlagEnum(m_fileHandlingFlags, MediaFileHandlingFlags::ForceIndexPosition, forceIndexPosition);
730}
731
745inline std::uint64_t MediaFileInfo::maxFullParseSize() const
746{
747 return m_maxFullParseSize;
748}
749
755{
756 m_maxFullParseSize = maxFullParseSize;
757}
758
759} // namespace TagParser
760
761#endif // TAG_PARSER_MEDIAINFO_H
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks.
The AbstractContainer class provides an interface and common functionality to parse and make a certai...
The AbstractTrack class parses and stores technical information about video, audio and other kinds of...
BasicFileInfo()
Constructs a new BasicFileInfo for the specified file.
const std::string & path() const
Returns the path of the current file.
std::uint64_t size() const
Returns size of the current file in bytes.
The Diagnostics class is a container for DiagMessage.
The EbmlElement class helps to parse EBML files such as Matroska files.
Definition ebmlelement.h:32
Implementation of TagParser::Tag for ID3v1 tags.
Definition id3v1tag.h:10
Implementation of TagParser::Tag for ID3v2 tags.
Definition id3v2tag.h:78
Implementation of GenericContainer<MediaFileInfo, MatroskaTag, MatroskaTrack, EbmlElement>.
Implementation of TagParser::Tag for the Matroska container.
Definition matroskatag.h:72
std::uint64_t maxFullParseSize() const
Returns the maximal file size for a "full parse" in byte.
bool isForcingRewrite() const
Returns whether forcing rewriting (when applying changes) is enabled.
const std::vector< std::unique_ptr< Id3v2Tag > > & id3v2Tags() const
Returns pointers to the assigned ID3v2 tags.
void parseEverything(Diagnostics &diag, AbortableProgressFeedback &progress)
Parses the container format, the tracks and the tag information of the current file.
std::uint64_t paddingSize() const
Returns the padding size.
std::string_view containerFormatName() const
Returns the name of the container format as C-style string.
void setSaveFilePath(std::string_view saveFilePath)
Sets the "save file path".
std::string_view containerFormatSubversion() const
Returns the subversion of the container format as C-style string.
const std::vector< std::unique_ptr< MatroskaTag > > & matroskaTags() const
Returns pointers to the assigned Matroska tags.
bool forceIndexPosition() const
Returns whether indexPosition() is forced.
std::string_view containerFormatAbbreviation() const
Returns the abbreviation of the container format as C-style string.
std::vector< AbstractTrack * > tracks() const
Returns the tracks for the current file.
bool areChaptersSupported() const
Returns an indication whether this library supports parsing the chapters of the current file.
MediaFileHandlingFlags fileHandlingFlags()
Returns the currently configured file handling flags.
ParsingStatus tagsParsingStatus() const
Returns an indication whether tag information has been parsed yet.
void setForceRewrite(bool forceRewrite)
Sets whether forcing rewriting (when applying changes) is enabled.
const std::string & saveFilePath() const
Returns the "save file path" which has been set using setSaveFilePath().
void setFileHandlingFlags(MediaFileHandlingFlags flags)
Replaces all currently configured file handling flags with the specified flags.
VorbisComment * createVorbisComment()
Creates a Vorbis comment for the current file.
VorbisComment * vorbisComment() const
Returns a pointer to the first assigned Vorbis comment or nullptr if none is assigned.
void removeAllTags()
Removes all assigned tags from the file.
bool removeId3v1Tag()
Removes a possibly assigned ID3v1 tag from the current file.
void setMaxFullParseSize(std::uint64_t maxFullParseSize)
Sets the maximal file size for a "full parse" in byte.
bool hasTracksOfType(TagParser::MediaType type) const
Returns an indication whether the current file has tracks of the specified type.
void invalidated() override
Reimplemented from BasicFileInfo::invalidated().
void setMaxPadding(std::size_t maxPadding)
Sets the maximum padding to be written before the data blocks when applying changes.
std::size_t trackCount() const
Returns the number of tracks that could be parsed.
void parseAttachments(Diagnostics &diag, AbortableProgressFeedback &progress)
Parses the attachments of the current file.
void setMinPadding(std::size_t minPadding)
Sets the minimum padding to be written before the data blocks when applying changes.
void parseTracks(Diagnostics &diag, AbortableProgressFeedback &progress)
Parses the tracks of the current file.
bool removeId3v2Tag(Id3v2Tag *tag)
Removes an assigned ID3v2 tag from the current file.
bool areAttachmentsSupported() const
Returns an indication whether this library supports attachment format of the current file.
MediaFileInfo & operator=(const MediaFileInfo &)=delete
void parseContainerFormat(Diagnostics &diag, AbortableProgressFeedback &progress)
Parses the container format of the current file.
std::size_t maxPadding() const
Returns the maximum padding to be written before the data blocks when applying changes.
ParsingStatus tracksParsingStatus() const
Returns an indication whether tracks have been parsed yet.
Id3v1Tag * id3v1Tag() const
Returns a pointer to the assigned ID3v1 tag or nullptr if none is assigned.
std::string technicalSummary() const
Generates a short technical summary about the file's tracks.
CppUtilities::TimeSpan duration() const
Returns the overall duration of the file if known; otherwise returns a TimeSpan with zero ticks.
void parseChapters(Diagnostics &diag, AbortableProgressFeedback &progress)
Parses the chapters of the current file.
std::size_t preferredPadding() const
Returns the padding to be written before the data block when applying changes and the file needs to b...
bool removeVorbisComment()
Removes all assigned Vorbis comment from the current file.
void parseTags(Diagnostics &diag, AbortableProgressFeedback &progress)
Parses the tag(s) of the current file.
const std::string & writingApplication() const
Sets the writing application as container-level meta-data.
bool id3v2ToId3v1()
Converts the existing ID3v2 tags into an ID3v1 tag.
void setForceTagPosition(bool forceTagPosition)
Sets whether tagPosition() is forced.
Id3v2Tag * createId3v2Tag()
Creates an ID3v2 tag for the current file.
bool areTracksSupported() const
Returns an indication whether this library supports parsing the tracks information of the current fil...
bool createAppropriateTags(const TagCreationSettings &settings=TagCreationSettings())
Ensures appropriate tags are created according the given settings.
void clearParsingResults()
Clears all parsing results and assigned/created/changed information such as detected container format...
void setForceFullParse(bool forceFullParse)
Sets whether forcing a full parse is enabled.
void tags(std::vector< Tag * > &tags) const
Stores all tags assigned to the current file in the specified vector.
std::size_t minPadding() const
Returns the minimum padding to be written before the data blocks when applying changes.
ParsingStatus attachmentsParsingStatus() const
Returns whether the attachments have been parsed yet.
Id3v1Tag * createId3v1Tag()
Creates an ID3v1 tag for the current file.
bool hasId3v2Tag() const
Returns an indication whether an ID3v2 tag is assigned.
void setTagPosition(ElementPosition tagPosition)
Sets the position (in the output file) where the tag information is written when applying changes.
void setWritingApplication(std::string_view writingApplication)
Sets the writing application as container-level meta-data.
bool isForcingFullParse() const
Returns an indication whether forcing a full parse is enabled.
ElementPosition tagPosition() const
Returns the position (in the output file) where the tag information is written when applying changes.
std::uint64_t effectiveSize() const
Returns the "effective size" of the file if know; otherwise returns 0.
std::unordered_set< std::string > availableLanguages(TagParser::MediaType type=TagParser::MediaType::Audio) const
Determines the available languages for specified media type (by default MediaType::Audio).
bool areTagsSupported() const
Returns an indication whether this library supports the tag format of the current file.
bool removeAllId3v2Tags()
Removes all assigned ID3v2 tags from the current file.
void setBackupDirectory(std::string_view backupDirectory)
Sets the directory used to store backup files.
AbstractContainer * container() const
Returns the container for the current file.
std::string_view mimeType() const
Returns the MIME-type of the container format as C-style string.
ContainerFormat containerFormat() const
Returns the container format of the current file.
void parsedTags(std::vector< Tag * > &tags) const
Returns all tags parsed from the current file.
ParsingStatus containerParsingStatus() const
Returns an indication whether the container format has been parsed yet.
bool id3v1ToId3v2()
Converts an existing ID3v1 tag into an ID3v2 tag.
double overallAverageBitrate() const
Returns the overall average bitrate in kbit/s of the file if known; otherwise returns 0....
MediaFileInfo(const MediaFileInfo &)=delete
std::vector< AbstractChapter * > chapters() const
Returns all chapters assigned to the current file.
void applyChanges(Diagnostics &diag, AbortableProgressFeedback &progress)
Applies assigned/changed tag information to the current file.
std::uint64_t containerOffset() const
Returns the actual container start offset.
std::vector< AbstractAttachment * > attachments() const
Returns all attachments assigned to the current file.
bool forceTagPosition() const
Returns whether tagPosition() is forced.
static void writePadding(std::ostream &outputStream, uint64_t size)
Writes the specified number of zeroes to outputStream.
void mergeId3v2Tags()
Merges the assigned ID3v2 tags into a single ID3v2 tag.
void reportPaddingSizeChanged(std::uint64_t newPaddingSize)
Sets the padding size.
bool hasId3v1Tag() const
Returns an indication whether an ID3v1 tag is assigned.
ParsingStatus chaptersParsingStatus() const
Returns whether the chapters have been parsed yet.
bool removeTag(Tag *tag)
Removes a possibly assigned tag from the current file.
void setIndexPosition(ElementPosition indexPosition)
Sets the position (in the output file) where the index is written when applying changes.
void setPreferredPadding(std::size_t preferredPadding)
Sets the padding to be written before the data block when applying changes and the file needs to be r...
ElementPosition indexPosition() const
Returns the position (in the output file) where the index is written when applying changes.
MediaFileInfo()
Constructs a new MediaFileInfo.
bool hasAnyTag() const
Returns an indication whether a tag of any format is assigned.
void setForceIndexPosition(bool forceTagPosition)
Sets whether indexPosition() is forced.
const std::string & backupDirectory() const
Returns the directory used to store backup files.
Mp4Tag * mp4Tag() const
Returns a pointer to the assigned MP4 tag or nullptr if none is assigned.
The Mp4Atom class helps to parse MP4 files.
Definition mp4atom.h:38
Implementation of GenericContainer<MediaFileInfo, Mp4Tag, Mp4Track, Mp4Atom>.
Implementation of TagParser::Tag for the MP4 container.
Definition mp4tag.h:97
Implementation of TagParser::AbstractContainer for Ogg files.
The Tag class is used to store, read and write tag information.
Definition tag.h:179
Implementation of TagParser::Tag for Vorbis comments.
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
Definition global.h:14
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
TAG_PARSER_EXPORT std::string_view containerFormatSubversion(ContainerFormat containerFormat)
Returns the subversion of the container format as C-style string.
ElementPosition
Definition settings.h:13
TAG_PARSER_EXPORT std::string_view containerFormatName(ContainerFormat containerFormat)
Returns the name of the specified container format as C-style string.
TagType
Specifies the tag type.
Definition tagtype.h:11
ParsingStatus
The ParsingStatus enum specifies whether a certain part of the file (tracks, tags,...
MediaType
The MediaType enum specifies the type of media data (audio, video, text, ...).
Definition mediaformat.h:14
MediaFileStructureFlags
The MediaFileStructureFlags enum specifies flags which describing the structure of a media file.
ContainerFormat
Specifies the container format.
Definition signature.h:18
MediaFileHandlingFlags
The MediaFileHandlingFlags enum specifies flags which controls the behavior of MediaFileInfo objects.
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(TagParser, TagParser::TagCreationFlags)
The MediaFileInfoPrivate struct contains private fields of the MediaFileInfo class.
The TagSettings struct contains settings which can be passed to MediaFileInfo::createAppropriateTags(...
Definition settings.h:50