Tag Parser 12.3.1
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
Loading...
Searching...
No Matches
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;
24class MatroskaContainer;
25class OggContainer;
26class EbmlElement;
27class MatroskaTag;
28class AbstractTrack;
29class VorbisComment;
30class Diagnostics;
31class AbortableProgressFeedback;
32
33enum class MediaType : unsigned int;
34enum class TagType : unsigned int;
35
40enum class ParsingStatus : std::uint8_t {
42 Ok,
45};
46
50enum class MediaFileStructureFlags : std::uint64_t {
51 None,
52 ActualExistingId3v1Tag = (1 << 0),
53};
54
58enum class MediaFileHandlingFlags : std::uint64_t {
59 None,
61 = (1 << 0),
62 ForceRewrite = (1 << 1),
63 ForceTagPosition = (1 << 2),
64 ForceIndexPosition = (1 << 3),
65 NormalizeKnownTagFieldIds = (1 << 4),
66 PreserveRawTimingValues = (1 << 8),
67 PreserveMuxingApplication = (1 << 9),
68 PreserveWritingApplication = (1 << 10),
69 ConvertTotalFields = (1 << 11),
72};
73
74} // namespace TagParser
75
78
79namespace TagParser {
80
81struct MediaFileInfoPrivate;
82
83class TAG_PARSER_EXPORT MediaFileInfo : public BasicFileInfo {
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
94 void parseContainerFormat(Diagnostics &diag, AbortableProgressFeedback &progress);
95 void parseTracks(Diagnostics &diag, AbortableProgressFeedback &progress);
96 void parseTags(Diagnostics &diag, AbortableProgressFeedback &progress);
97 void parseChapters(Diagnostics &diag, AbortableProgressFeedback &progress);
98 void parseAttachments(Diagnostics &diag, AbortableProgressFeedback &progress);
99 void parseEverything(Diagnostics &diag, AbortableProgressFeedback &progress);
100
101 // methods to apply changes
102 void applyChanges(Diagnostics &diag, AbortableProgressFeedback &progress);
103
104 // methods to get parsed information regarding ...
105 // ... the container
106 ContainerFormat containerFormat() const;
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;
114 AbstractContainer *container() const;
115 ParsingStatus containerParsingStatus() const;
116 // ... the chapters
117 ParsingStatus chaptersParsingStatus() const;
118 std::vector<AbstractChapter *> chapters() const;
119 bool areChaptersSupported() const;
120 // ... the attachments
121 ParsingStatus attachmentsParsingStatus() const;
122 std::vector<AbstractAttachment *> attachments() const;
123 bool areAttachmentsSupported() const;
124 // ... the tracks
125 ParsingStatus tracksParsingStatus() const;
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
135 ParsingStatus tagsParsingStatus() const;
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;
147 VorbisComment *vorbisComment() const;
148 bool areTagsSupported() const;
149
150 // methods to create/remove tags
151 bool createAppropriateTags(const TagCreationSettings &settings = TagCreationSettings());
152 bool removeId3v1Tag();
153 Id3v1Tag *createId3v1Tag();
154 bool removeId3v2Tag(Id3v2Tag *tag);
155 bool removeAllId3v2Tags();
156 Id3v2Tag *createId3v2Tag();
157 bool removeTag(Tag *tag);
158 void removeAllTags();
159 void mergeId3v2Tags();
160 bool id3v1ToId3v2();
161 bool id3v2ToId3v1();
162 VorbisComment *createVorbisComment();
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);
176 MediaFileHandlingFlags fileHandlingFlags();
177 void setFileHandlingFlags(MediaFileHandlingFlags flags);
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);
188 ElementPosition tagPosition() const;
189 void setTagPosition(ElementPosition tagPosition);
190 bool forceTagPosition() const;
191 void setForceTagPosition(bool forceTagPosition);
192 ElementPosition indexPosition() const;
193 void setIndexPosition(ElementPosition indexPosition);
194 bool forceIndexPosition() const;
195 void setForceIndexPosition(bool forceTagPosition);
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
430inline void MediaFileInfo::setBackupDirectory(std::string_view backupDirectory)
431{
432 m_backupDirectory = backupDirectory;
433}
434
439inline void MediaFileInfo::setBackupDirectory(std::string &&backupDirectory)
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
475inline void MediaFileInfo::setSaveFilePath(std::string &&saveFilePath)
476{
477 m_saveFilePath = std::move(saveFilePath);
478}
479
485inline const std::string &MediaFileInfo::writingApplication() const
486{
487 return m_writingApplication;
488}
489
496inline void MediaFileInfo::setWritingApplication(std::string_view writingApplication)
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
593inline void MediaFileInfo::setMinPadding(std::size_t minPadding)
594{
595 m_minPadding = minPadding;
596}
597
611inline std::size_t MediaFileInfo::maxPadding() const
612{
613 return m_maxPadding;
614}
615
621inline void MediaFileInfo::setMaxPadding(std::size_t maxPadding)
622{
623 m_maxPadding = maxPadding;
624}
625
632inline std::size_t MediaFileInfo::preferredPadding() const
633{
634 return m_preferredPadding;
635}
636
642inline void MediaFileInfo::setPreferredPadding(std::size_t preferredPadding)
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
687inline void MediaFileInfo::setForceTagPosition(bool forceTagPosition)
688{
689 CppUtilities::modFlagEnum(m_fileHandlingFlags, MediaFileHandlingFlags::ForceTagPosition, forceTagPosition);
690}
691
698{
699 return m_indexPosition;
700}
701
708{
709 m_indexPosition = indexPosition;
710}
711
718{
719 return m_fileHandlingFlags & MediaFileHandlingFlags::ForceIndexPosition;
720}
721
727inline void MediaFileInfo::setForceIndexPosition(bool forceIndexPosition)
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
754inline void MediaFileInfo::setMaxFullParseSize(std::uint64_t maxFullParseSize)
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 Diagnostics class is a container for DiagMessage.
Implementation of TagParser::Tag for ID3v1 tags.
Implementation of TagParser::Tag for ID3v2 tags.
The MediaFileInfo class allows to read and write tag information providing a container/tag format ind...
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.
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.
bool forceIndexPosition() const
Returns whether indexPosition() is forced.
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.
void setMaxFullParseSize(std::uint64_t maxFullParseSize)
Sets the maximal file size for a "full parse" in byte.
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 setMinPadding(std::size_t minPadding)
Sets the minimum padding to be written before the data blocks when applying changes.
MediaFileInfo & operator=(const MediaFileInfo &)=delete
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::size_t preferredPadding() const
Returns the padding to be written before the data block when applying changes and the file needs to b...
const std::string & writingApplication() const
Sets the writing application as container-level meta-data.
void setForceTagPosition(bool forceTagPosition)
Sets whether tagPosition() is forced.
void setForceFullParse(bool forceFullParse)
Sets whether forcing a full parse is enabled.
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.
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.
void setBackupDirectory(std::string_view backupDirectory)
Sets the directory used to store backup files.
AbstractContainer * container() const
Returns the container for the current file.
ContainerFormat containerFormat() const
Returns the container format of the current file.
ParsingStatus containerParsingStatus() const
Returns an indication whether the container format has been parsed yet.
MediaFileInfo(const MediaFileInfo &)=delete
std::uint64_t containerOffset() const
Returns the actual container start offset.
bool forceTagPosition() const
Returns whether tagPosition() is forced.
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.
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.
void setForceIndexPosition(bool forceTagPosition)
Sets whether indexPosition() is forced.
const std::string & backupDirectory() const
Returns the directory used to store backup files.
Implementation of TagParser::Tag for the MP4 container.
The Tag class is used to store, read and write tag information.
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 containerFormatAbbreviation(ContainerFormat containerFormat, MediaType mediaType=MediaType::Unknown, unsigned int version=0)
Returns the abbreviation of the container format as C-style string considering the specified media ty...
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 TagSettings struct contains settings which can be passed to MediaFileInfo::createAppropriateTags(...
Definition settings.h:50