Tag Parser  7.1.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
mediafileinfo.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_MEDIAINFO_H
2 #define TAG_PARSER_MEDIAINFO_H
3 
4 #include "./abstractcontainer.h"
5 #include "./basicfileinfo.h"
6 #include "./settings.h"
7 #include "./signature.h"
8 
9 #include <memory>
10 #include <unordered_set>
11 #include <vector>
12 
13 namespace TagParser {
14 
15 class Tag;
16 class Id3v1Tag;
17 class Id3v2Tag;
18 class Mp4Container;
19 class Mp4Atom;
20 class Mp4Tag;
21 class MatroskaContainer;
22 class OggContainer;
23 class EbmlElement;
24 class MatroskaTag;
25 class AbstractTrack;
26 class VorbisComment;
27 class Diagnostics;
28 class AbortableProgressFeedback;
29 
30 DECLARE_ENUM_CLASS(MediaType, unsigned int);
31 DECLARE_ENUM_CLASS(TagType, unsigned int);
32 
37 enum class ParsingStatus : byte {
38  NotParsedYet,
39  Ok,
40  NotSupported,
42 };
43 
45 public:
46  // constructor, destructor
47  MediaFileInfo();
48  MediaFileInfo(const std::string &path);
49  MediaFileInfo(const MediaFileInfo &) = delete;
50  MediaFileInfo &operator=(const MediaFileInfo &) = delete;
51  ~MediaFileInfo() override;
52 
53  // methods to parse file
54  void parseContainerFormat(Diagnostics &diag);
55  void parseTracks(Diagnostics &diag);
56  void parseTags(Diagnostics &diag);
57  void parseChapters(Diagnostics &diag);
58  void parseAttachments(Diagnostics &diag);
59  void parseEverything(Diagnostics &diag);
60 
61  // methods to apply changes
62  void applyChanges(Diagnostics &diag, AbortableProgressFeedback &progress);
63 
64  // methods to get parsed information regarding ...
65  // ... the container
66  ContainerFormat containerFormat() const;
67  const char *containerFormatName() const;
68  const char *containerFormatAbbreviation() const;
69  const char *containerFormatSubversion() const;
70  const char *mimeType() const;
71  uint64 containerOffset() const;
72  uint64 paddingSize() const;
73  AbstractContainer *container() const;
74  ParsingStatus containerParsingStatus() const;
75  // ... the capters
76  ParsingStatus chaptersParsingStatus() const;
77  std::vector<AbstractChapter *> chapters() const;
78  bool areChaptersSupported() const;
79  // ... the attachments
80  ParsingStatus attachmentsParsingStatus() const;
81  std::vector<AbstractAttachment *> attachments() const;
82  bool areAttachmentsSupported() const;
83  // ... the tracks
84  ParsingStatus tracksParsingStatus() const;
85  std::size_t trackCount() const;
86  std::vector<AbstractTrack *> tracks() const;
87  bool hasTracksOfType(TagParser::MediaType type) const;
89  std::unordered_set<std::string> availableLanguages(TagParser::MediaType type = TagParser::MediaType::Audio) const;
90  std::string technicalSummary() const;
91  bool areTracksSupported() const;
92  // ... the tags
93  ParsingStatus tagsParsingStatus() const;
94  bool hasId3v1Tag() const;
95  bool hasId3v2Tag() const;
96  bool hasAnyTag() const;
97  Id3v1Tag *id3v1Tag() const;
98  const std::vector<std::unique_ptr<Id3v2Tag>> &id3v2Tags() const;
99  void tags(std::vector<Tag *> &tags) const;
100  std::vector<Tag *> tags() const;
101  Mp4Tag *mp4Tag() const;
102  const std::vector<std::unique_ptr<MatroskaTag>> &matroskaTags() const;
103  VorbisComment *vorbisComment() const;
104  bool areTagsSupported() const;
105 
106  // methods to create/remove tags
107  bool createAppropriateTags(const TagCreationSettings &settings = TagCreationSettings());
108  bool removeId3v1Tag();
109  Id3v1Tag *createId3v1Tag();
110  bool removeId3v2Tag(Id3v2Tag *tag);
111  bool removeAllId3v2Tags();
112  Id3v2Tag *createId3v2Tag();
113  void removeTag(Tag *tag);
114  void removeAllTags();
115  void mergeId3v2Tags();
116  bool id3v1ToId3v2();
117  bool id3v2ToId3v1();
118  VorbisComment *createVorbisComment();
119  bool removeVorbisComment();
120  void clearParsingResults();
121 
122  // methods to get, set object behaviour
123  const std::string &saveFilePath() const;
124  void setSaveFilePath(const std::string &saveFilePath);
125  const std::string writingApplication() const;
126  void setWritingApplication(const std::string &writingApplication);
127  void setWritingApplication(const char *writingApplication);
128  bool isForcingFullParse() const;
129  void setForceFullParse(bool forceFullParse);
130  bool isForcingRewrite() const;
131  void setForceRewrite(bool forceRewrite);
132  size_t minPadding() const;
133  void setMinPadding(size_t minPadding);
134  size_t maxPadding() const;
135  void setMaxPadding(size_t maxPadding);
136  size_t preferredPadding() const;
137  void setPreferredPadding(size_t preferredPadding);
138  ElementPosition tagPosition() const;
139  void setTagPosition(ElementPosition tagPosition);
140  bool forceTagPosition() const;
141  void setForceTagPosition(bool forceTagPosition);
142  ElementPosition indexPosition() const;
143  void setIndexPosition(ElementPosition indexPosition);
144  bool forceIndexPosition() const;
145  void setForceIndexPosition(bool forceTagPosition);
146 
147 protected:
148  void invalidated() override;
149 
150 private:
151  // private methods internally used when rewriting the file to apply new tag information
152  // currently only the makeMp3File() methods is present; corresponding methods for
153  // other formats are outsourced to container classes
154  void makeMp3File(Diagnostics &diag, AbortableProgressFeedback &progress);
155 
156  // fields related to the container
157  ParsingStatus m_containerParsingStatus;
158  ContainerFormat m_containerFormat;
159  std::streamoff m_containerOffset;
160  uint64 m_paddingSize;
161  bool m_actualExistingId3v1Tag;
162  std::vector<std::streamoff> m_actualId3v2TagOffsets;
163  std::unique_ptr<AbstractContainer> m_container;
164 
165  // fields related to the tracks
166  ParsingStatus m_tracksParsingStatus;
167  std::unique_ptr<AbstractTrack> m_singleTrack;
168 
169  // fields related to the tag
170  ParsingStatus m_tagsParsingStatus;
171  std::unique_ptr<Id3v1Tag> m_id3v1Tag;
172  std::vector<std::unique_ptr<Id3v2Tag>> m_id3v2Tags;
173 
174  // fields related to the chapters and the attachments
175  ParsingStatus m_chaptersParsingStatus;
176  ParsingStatus m_attachmentsParsingStatus;
177 
178  // fields specifying object behaviour
179  std::string m_saveFilePath;
180  std::string m_writingApplication;
181  size_t m_minPadding;
182  size_t m_maxPadding;
183  size_t m_preferredPadding;
184  ElementPosition m_tagPosition;
185  ElementPosition m_indexPosition;
186  bool m_forceFullParse;
187  bool m_forceRewrite;
188  bool m_forceTagPosition;
189  bool m_forceIndexPosition;
190 };
191 
196 {
197  return m_containerParsingStatus;
198 }
199 
207 {
208  return m_containerFormat;
209 }
210 
221 inline const char *MediaFileInfo::containerFormatName() const
222 {
223  return TagParser::containerFormatName(m_containerFormat);
224 }
225 
237 {
238  return TagParser::containerFormatSubversion(m_containerFormat);
239 }
240 
244 inline uint64 MediaFileInfo::containerOffset() const
245 {
246  return static_cast<uint64>(m_containerOffset);
247 }
248 
252 inline uint64 MediaFileInfo::paddingSize() const
253 {
254  return m_paddingSize;
255 }
256 
261 {
262  return m_tagsParsingStatus;
263 }
264 
269 {
270  return m_tracksParsingStatus;
271 }
272 
281 inline size_t MediaFileInfo::trackCount() const
282 {
283  return m_singleTrack ? 1 : (m_container ? m_container->trackCount() : 0);
284 }
285 
290 {
291  return m_chaptersParsingStatus;
292 }
293 
298 {
299  return m_attachmentsParsingStatus;
300 }
301 
305 inline bool MediaFileInfo::hasId3v1Tag() const
306 {
307  return m_id3v1Tag != nullptr;
308 }
309 
313 inline bool MediaFileInfo::hasId3v2Tag() const
314 {
315  return !m_id3v2Tags.empty();
316 }
317 
326 {
327  return m_id3v1Tag.get();
328 }
329 
337 inline const std::vector<std::unique_ptr<Id3v2Tag>> &MediaFileInfo::id3v2Tags() const
338 {
339  return m_id3v2Tags;
340 }
341 
346 inline const std::string &MediaFileInfo::saveFilePath() const
347 {
348  return m_saveFilePath;
349 }
350 
365 inline void MediaFileInfo::setSaveFilePath(const std::string &saveFilePath)
366 {
367  m_saveFilePath = saveFilePath;
368 }
369 
375 inline const std::string MediaFileInfo::writingApplication() const
376 {
377  return m_writingApplication;
378 }
379 
384 inline void MediaFileInfo::setWritingApplication(const std::string &writingApplication)
385 {
386  m_writingApplication = writingApplication;
387 }
388 
393 inline void MediaFileInfo::setWritingApplication(const char *writingApplication)
394 {
395  m_writingApplication = writingApplication;
396 }
397 
409 {
410  return m_container.get();
411 }
412 
422 {
423  return m_forceFullParse;
424 }
425 
431 inline void MediaFileInfo::setForceFullParse(bool forceFullParse)
432 {
433  m_forceFullParse = forceFullParse;
434 }
435 
440 {
441  return m_forceRewrite;
442 }
443 
447 inline void MediaFileInfo::setForceRewrite(bool forceRewrite)
448 {
449  m_forceRewrite = forceRewrite;
450 }
451 
464 inline size_t MediaFileInfo::minPadding() const
465 {
466  return m_minPadding;
467 }
468 
474 inline void MediaFileInfo::setMinPadding(size_t minPadding)
475 {
476  m_minPadding = minPadding;
477 }
478 
492 inline size_t MediaFileInfo::maxPadding() const
493 {
494  return m_maxPadding;
495 }
496 
502 inline void MediaFileInfo::setMaxPadding(size_t maxPadding)
503 {
504  m_maxPadding = maxPadding;
505 }
506 
513 inline size_t MediaFileInfo::preferredPadding() const
514 {
515  return m_preferredPadding;
516 }
517 
523 inline void MediaFileInfo::setPreferredPadding(size_t preferredPadding)
524 {
525  m_preferredPadding = preferredPadding;
526 }
527 
534 {
535  return m_tagPosition;
536 }
537 
549 {
550  m_tagPosition = tagPosition;
551 }
552 
559 {
560  return m_forceTagPosition;
561 }
562 
568 inline void MediaFileInfo::setForceTagPosition(bool forceTagPosition)
569 {
570  m_forceTagPosition = forceTagPosition;
571 }
572 
579 {
580  return m_indexPosition;
581 }
582 
589 {
590  m_indexPosition = indexPosition;
591 }
592 
599 {
600  return m_forceIndexPosition;
601 }
602 
608 inline void MediaFileInfo::setForceIndexPosition(bool forceIndexPosition)
609 {
610  m_forceIndexPosition = forceIndexPosition;
611 }
612 
613 } // namespace TagParser
614 
615 #endif // TAG_PARSER_MEDIAINFO_H
Implementation of TagParser::Tag for the MP4 container.
Definition: mp4tag.h:97
const char * containerFormatSubversion() const
Returns the subversion of the container format as C-style string.
size_t preferredPadding() const
Returns the padding to be written before the data block when applying changes and the file needs to b...
The Tag class is used to store, read and write tag information.
Definition: tag.h:95
Id3v1Tag * id3v1Tag() const
Returns a pointer to the assigned ID3v1 tag or nullptr if none is assigned.
DECLARE_ENUM_CLASS(MediaType, unsigned int)
bool forceTagPosition() const
Returns whether tagPosition() is forced.
ParsingStatus tagsParsingStatus() const
Returns an indication whether tag information has been parsed yet.
bool hasId3v1Tag() const
Returns an indication whether an ID3v1 tag is assigned.
std::size_t trackCount() const
Returns the number of tracks that could be parsed.
void setForceRewrite(bool forceRewrite)
Sets whether forcing rewriting (when applying changes) is enabled.
ParsingStatus attachmentsParsingStatus() const
Returns whether the attachments have been parsed yet.
MediaType
The MediaType enum specifies the type of media data (audio, video, text, ...).
Definition: mediaformat.h:13
const std::vector< std::unique_ptr< Id3v2Tag > > & id3v2Tags() const
Returns pointers to the assigned ID3v2 tags.
void setMaxPadding(size_t maxPadding)
Sets the maximum padding to be written before the data blocks when applying changes.
The TagSettings struct contains settings which can be passed to MediaFileInfo::createAppropriateTags(...
Definition: settings.h:70
bool isForcingRewrite() const
Returns whether forcing rewriting (when applying changes) is enabled.
uint64 paddingSize() const
Returns the padding size.
The AbstractContainer class provides an interface and common functionality to parse and make a certai...
ParsingStatus chaptersParsingStatus() const
Returns whether the chapters have been parsed yet.
size_t maxPadding() const
Returns the maximum padding to be written before the data blocks when applying changes.
void setTagPosition(ElementPosition tagPosition)
Sets the position (in the output file) where the tag information is written when applying changes...
The MediaFileInfo class allows to read and write tag information providing a container/tag format ind...
Definition: mediafileinfo.h:44
ContainerFormat containerFormat() const
Returns the container format of the current file.
void setForceTagPosition(bool forceTagPosition)
Sets whether tagPosition() is forced.
const char * containerFormatName() const
Returns the name of the container format as C-style string.
ContainerFormat
Specifies the container format.
Definition: signature.h:17
bool hasId3v2Tag() const
Returns an indication whether an ID3v2 tag is assigned.
Implementation of TagParser::Tag for Vorbis comments.
Definition: vorbiscomment.h:25
TAG_PARSER_EXPORT const char * containerFormatName(ContainerFormat containerFormat)
Returns the name of the specified container format as C-style string.
Definition: signature.cpp:361
void setPreferredPadding(size_t preferredPadding)
Sets the padding to be written before the data block when applying changes and the file needs to be r...
bool isForcingFullParse() const
Returns an indication whether forcing a full parse is enabled.
size_t minPadding() const
Returns the minimum padding to be written before the data blocks when applying changes.
bool forceIndexPosition() const
Returns whether indexPosition() is forced.
TAG_PARSER_EXPORT const char * 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...
Definition: signature.cpp:243
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks...
void setForceIndexPosition(bool forceTagPosition)
Sets whether indexPosition() is forced.
ParsingStatus
The ParsingStatus enum specifies whether a certain part of the file (tracks, tags, ...) has been parsed yet and if what the parsing result is.
Definition: mediafileinfo.h:37
void setForceFullParse(bool forceFullParse)
Sets whether forcing a full parse is enabled.
ParsingStatus tracksParsingStatus() const
Returns an indication whether tracks have been parsed yet.
const std::string & saveFilePath() const
Returns the "save file path" which has been set using setSaveFilePath().
ElementPosition tagPosition() const
Returns the position (in the output file) where the tag information is written when applying changes...
uint64 containerOffset() const
Returns the actual container start offset.
AbstractContainer * container() const
Returns the container for the current file.
ElementPosition indexPosition() const
Returns the position (in the output file) where the index is written when applying changes...
TAG_PARSER_EXPORT const char * duration()
Implementation of TagParser::Tag for ID3v2 tags.
Definition: id3v2tag.h:61
void setSaveFilePath(const std::string &saveFilePath)
Sets the "save file path".
Implementation of TagParser::Tag for ID3v1 tags.
Definition: id3v1tag.h:10
ParsingStatus containerParsingStatus() const
Returns an indication whether the container format has been parsed yet.
void setMinPadding(size_t minPadding)
Sets the minimum padding to be written before the data blocks when applying changes.
void setIndexPosition(ElementPosition indexPosition)
Sets the position (in the output file) where the index is written when applying changes.
TagType
Specifies the tag type.
Definition: tag.h:20
TAG_PARSER_EXPORT const char * containerFormatSubversion(ContainerFormat containerFormat)
Returns the subversion of the container format as C-style string.
Definition: signature.cpp:462
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
void setWritingApplication(const std::string &writingApplication)
Sets the writing application as container-level meta-data.
const std::string writingApplication() const
Sets the writing application as container-level meta-data.
ElementPosition
Definition: settings.h:10
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:154
The BasicFileInfo class provides basic file information such as file name, extension, directory and size for a specified file.
Definition: basicfileinfo.h:13