-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadataextractor.cpp
More file actions
71 lines (59 loc) · 2.58 KB
/
Copy pathmetadataextractor.cpp
File metadata and controls
71 lines (59 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "metadataextractor.h"
#include <taglib/fileref.h>
#include <taglib/tag.h>
#include <taglib/mpegfile.h>
#include <taglib/id3v2tag.h>
#include <taglib/attachedpictureframe.h>
#include <QFileInfo>
#include <QDebug>
MetadataExtractor::MetadataExtractor(QObject *parent) : QObject(parent) {}
TrackMetadata MetadataExtractor::extract(const QString &filePath) {
TrackMetadata data;
QFileInfo fileInfo(filePath);
data.title = fileInfo.baseName();
if (!fileInfo.exists() || !fileInfo.isFile()) {
qWarning() << "File does not exist or is not a regular file:" << filePath;
return data;
}
TagLib::FileName fileName(filePath.toUtf8().constData());
TagLib::FileRef fileRef(fileName, true, TagLib::AudioProperties::Accurate);
if (fileRef.isNull() || !fileRef.tag()) {
qWarning() << "TagLib cannot open file or no tag found:" << filePath;
return data;
}
TagLib::Tag *tag = fileRef.tag();
if (tag) {
if (!tag->title().isEmpty())
data.title = tag->title().toCString(true);
if (!tag->artist().isEmpty())
data.artist = tag->artist().toCString(true);
if (!tag->album().isEmpty())
data.album = tag->album().toCString(true);
data.year = tag->year();
}
TagLib::MPEG::File *mpegFile = dynamic_cast<TagLib::MPEG::File*>(fileRef.file());
if (mpegFile && mpegFile->ID3v2Tag()) {
TagLib::ID3v2::Tag *id3v2Tag = mpegFile->ID3v2Tag();
if (id3v2Tag) {
const TagLib::ID3v2::FrameListMap &frameListMap = id3v2Tag->frameListMap();
if (frameListMap.contains("APIC")) {
const TagLib::ID3v2::FrameList &pictures = frameListMap["APIC"];
if (!pictures.isEmpty()) {
const TagLib::ID3v2::AttachedPictureFrame *picFrame =
dynamic_cast<const TagLib::ID3v2::AttachedPictureFrame*>(pictures.front());
if (picFrame && picFrame->picture().size() > 0) {
const TagLib::ByteVector &pictureData = picFrame->picture();
if (data.cover.loadFromData((const uchar*)pictureData.data(),
(int)pictureData.size())) {
qDebug() << "Cover image successfully loaded.";
} else {
qWarning() << "Failed to load cover image data.";
}
}
}
}
}
}
qDebug() << "Metadata:" << data.title << data.artist << data.album;
return data;
}