MythTV  master
metaiooggvorbis.cpp
Go to the documentation of this file.
1 
2 // libmythmetadata
3 #include "metaiooggvorbis.h"
4 #include "musicmetadata.h"
5 #include "musicutils.h"
6 
7 // Libmyth
8 #include <mythcontext.h>
9 
16 TagLib::Ogg::Vorbis::File *MetaIOOggVorbis::OpenFile(const QString &filename)
17 {
18  QByteArray fname = filename.toLocal8Bit();
19  auto *oggfile = new TagLib::Ogg::Vorbis::File(fname.constData());
20 
21  if (!oggfile->isOpen())
22  {
23  delete oggfile;
24  oggfile = nullptr;
25  }
26 
27  return oggfile;
28 }
29 
30 
34 bool MetaIOOggVorbis::write(const QString &filename, MusicMetadata* mdata)
35 {
36  if (!mdata)
37  return false;
38 
40 
41  if (m_filename.isEmpty())
42  return false;
43 
44  TagLib::Ogg::Vorbis::File *oggfile = OpenFile(m_filename);
45 
46  if (!oggfile)
47  return false;
48 
49  TagLib::Ogg::XiphComment *tag = oggfile->tag();
50 
51  if (!tag)
52  {
53  delete oggfile;
54  return false;
55  }
56 
57  WriteGenericMetadata(tag, mdata);
58 
59  // Compilation
60  if (mdata->Compilation())
61  {
62  tag->addField("MUSICBRAINZ_ALBUMARTISTID",
64  tag->addField("COMPILATION_ARTIST",
65  QStringToTString(mdata->CompilationArtist()), true);
66  }
67  else
68  {
69  // Don't remove the musicbrainz field unless it indicated a compilation
70  if (tag->contains("MUSICBRAINZ_ALBUMARTISTID") &&
71  (tag->fieldListMap()["MUSICBRAINZ_ALBUMARTISTID"].toString() ==
73  {
74  tag->removeField("MUSICBRAINZ_ALBUMARTISTID");
75  }
76  tag->removeField("COMPILATION_ARTIST");
77  }
78 
80  bool result = oggfile->save();
82 
83  delete oggfile;
84 
85  return (result);
86 }
87 
92 {
93  TagLib::Ogg::Vorbis::File *oggfile = OpenFile(filename);
94 
95  if (!oggfile)
96  return nullptr;
97 
98  TagLib::Ogg::XiphComment *tag = oggfile->tag();
99 
100  if (!tag)
101  {
102  delete oggfile;
103  return nullptr;
104  }
105 
106  auto *metadata = new MusicMetadata(filename);
107 
108  ReadGenericMetadata(tag, metadata);
109 
110  bool compilation = false;
111 
112  if (tag->contains("COMPILATION_ARTIST"))
113  {
114  QString compilation_artist = TStringToQString(
115  tag->fieldListMap()["COMPILATION_ARTIST"].toString()).trimmed();
116  if (compilation_artist != metadata->Artist())
117  {
118  metadata->setCompilationArtist(compilation_artist);
119  compilation = true;
120  }
121  }
122 
123  if (!compilation && tag->contains("MUSICBRAINZ_ALBUMARTISTID"))
124  {
125  QString musicbrainzcode = TStringToQString(
126  tag->fieldListMap()["MUSICBRAINZ_ALBUMARTISTID"].toString()).trimmed();
127  if (musicbrainzcode == MYTH_MUSICBRAINZ_ALBUMARTIST_UUID)
128  compilation = true;
129  }
130 
131  metadata->setCompilation(compilation);
132 
133  if (metadata->Length() <= 0)
134  metadata->setLength(getTrackLength(oggfile));
135 
136  delete oggfile;
137 
138  return metadata;
139 }
MetaIOTagLib::WriteGenericMetadata
static void WriteGenericMetadata(TagLib::Tag *tag, const MusicMetadata *metadata)
Writes metadata common to all tag formats to the tag.
Definition: metaiotaglib.cpp:24
MetaIOOggVorbis::OpenFile
static TagLib::Ogg::Vorbis::File * OpenFile(const QString &filename)
Open the file to read the tag.
Definition: metaiooggvorbis.cpp:16
MusicMetadata::CompilationArtist
QString CompilationArtist() const
Definition: musicmetadata.h:138
MYTH_MUSICBRAINZ_ALBUMARTIST_UUID
#define MYTH_MUSICBRAINZ_ALBUMARTIST_UUID
Definition: metaio.h:15
MetaIO::m_filename
QString m_filename
Definition: metaio.h:181
MusicMetadata
Definition: musicmetadata.h:81
MetaIOOggVorbis::write
bool write(const QString &filename, MusicMetadata *mdata) override
Writes all metadata back to a file.
Definition: metaiooggvorbis.cpp:34
MetaIOOggVorbis::read
MusicMetadata * read(const QString &filename) override
Reads MusicMetadata from a file.
Definition: metaiooggvorbis.cpp:91
musicutils.h
MetaIOTagLib::ReadGenericMetadata
void ReadGenericMetadata(TagLib::Tag *tag, MusicMetadata *metadata)
Writes metadata common to all tag formats to the tag.
Definition: metaiotaglib.cpp:54
MetaIO::restoreTimeStamps
void restoreTimeStamps(void)
Definition: metaio.cpp:231
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
QStringToTString
#define QStringToTString(s)
Definition: musicutils.h:14
TStringToQString
#define TStringToQString(s)
Definition: musicutils.h:16
MetaIOTagLib::getTrackLength
static int getTrackLength(TagLib::File *file)
Find the length of the track (in seconds)
Definition: metaiotaglib.cpp:89
MusicMetadata::Compilation
bool Compilation() const
Definition: musicmetadata.h:251
mythcontext.h
musicmetadata.h
metaiooggvorbis.h
MetaIO::saveTimeStamps
void saveTimeStamps(void)
Definition: metaio.cpp:222