MythTV  master
metaiotaglib.cpp
Go to the documentation of this file.
1 // Std
2 #include <cmath>
3 
4 // Libmyth
5 #include <mythlogging.h>
6 
7 // Taglib
8 #include <audioproperties.h>
9 #include <tag.h>
10 #include <tstring.h>
11 #include <fileref.h>
12 
13 // libmythmetadata
14 #include "metaiotaglib.h"
15 #include "musicmetadata.h"
16 #include "musicutils.h"
17 
24 void MetaIOTagLib::WriteGenericMetadata(Tag *tag, const MusicMetadata *metadata)
25 {
26  if (!tag || !metadata)
27  return;
28 
29  if (!metadata->Artist().isEmpty())
30  tag->setArtist(QStringToTString(metadata->Artist()));
31 
32  if (!metadata->Title().isEmpty())
33  tag->setTitle(QStringToTString(metadata->Title()));
34 
35  if (!metadata->Album().isEmpty())
36  tag->setAlbum(QStringToTString(metadata->Album()));
37 
38  if (metadata->Year() > 999 && metadata->Year() < 10000) // 4 digit year.
39  tag->setYear(metadata->Year());
40 
41  if (!metadata->Genre().isEmpty())
42  tag->setGenre(QStringToTString(metadata->Genre()));
43 
44  if (0 != metadata->Track())
45  tag->setTrack(metadata->Track());
46 }
47 
55 {
56  if (!tag || ! metadata)
57  return;
58 
59  // Basic Tags
60  if (!tag->isEmpty())
61  {
62  metadata->setTitle(TStringToQString(tag->title()).trimmed());
63  metadata->setArtist(TStringToQString(tag->artist()).trimmed());
64  metadata->setAlbum(TStringToQString(tag->album()).trimmed());
65  metadata->setTrack(tag->track());
66  metadata->setYear(tag->year());
67  metadata->setGenre(TStringToQString(tag->genre()).trimmed());
68  }
69 
70  // Fallback to filename reading
71  if (metadata->Title().isEmpty())
72  readFromFilename(metadata);
73 
74  // If we don't have title and artist or don't have the length return nullptr
75  if (metadata->Title().isEmpty() && metadata->Artist().isEmpty())
76  {
77  LOG(VB_GENERAL, LOG_ERR,
78  QString("MetaIOTagLib: Failed to read metadata from '%1'")
79  .arg(metadata->Filename(false)));
80  }
81 }
82 
89 int MetaIOTagLib::getTrackLength(TagLib::File *file)
90 {
91  int milliseconds = 0;
92 
93  if (file && file->audioProperties())
94  milliseconds = file->audioProperties()->length() * 1000;
95 
96  return milliseconds;
97 }
98 
105 int MetaIOTagLib::getTrackLength(const QString &filename)
106 {
107  int milliseconds = 0;
108  QByteArray fname = filename.toLocal8Bit();
109  TagLib::FileRef *file = new TagLib::FileRef(fname.constData());
110 
111  if (file && file->audioProperties())
112  milliseconds = file->audioProperties()->length() * 1000;
113 
114  // If we didn't get a valid length, add the metadata but show warning.
115  if (milliseconds <= 1000)
116  LOG(VB_GENERAL, LOG_ERR,
117  QString("MetaIOTagLib: Failed to read length "
118  "from '%1'. It may be corrupt.").arg(filename));
119 
120  delete file;
121 
122  return milliseconds;
123 }
void setTrack(int ltrack)
void setYear(int lyear)
QString Genre() const
#define TStringToQString(s)
Definition: musicutils.h:16
void WriteGenericMetadata(TagLib::Tag *tag, const MusicMetadata *metadata)
Writes metadata common to all tag formats to the tag.
QString Artist() const
void readFromFilename(const QString &filename, QString &artist, QString &album, QString &title, QString &genre, int &tracknum)
Reads MusicMetadata based on the folder/filename.
Definition: metaio.cpp:103
void setAlbum(const QString &lalbum, const QString &lalbum_sort=nullptr)
int Track() const
void ReadGenericMetadata(TagLib::Tag *tag, MusicMetadata *metadata)
Writes metadata common to all tag formats to the tag.
QString Album() const
void setTitle(const QString &ltitle, const QString &ltitle_sort=nullptr)
void setArtist(const QString &lartist, const QString &lartist_sort=nullptr)
int getTrackLength(TagLib::File *file)
Find the length of the track (in seconds)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
int Year() const
QString Title() const
QString Filename(bool find=true)
#define QStringToTString(s)
Definition: musicutils.h:14
void setGenre(const QString &lgenre)