-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediaextractor.cpp
174 lines (158 loc) · 4.81 KB
/
mediaextractor.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <QDebug>
#include <QImage>
#include <QFile>
#include <QDir>
#include <QIODevice>
#include <QTextStream>
#include "mediaextractor.h"
// Constructor
MediaExtractor::MediaExtractor(QObject *parent) : QObject(parent)
{
#ifdef Q_OS_LINUX
cvrArtPath = linuxPath;
svPath = cvrArtPath;
QDir folder(cvrArtPath);
if(!folder.exists())
folder.mkdir(cvrArtPath);
#endif
#ifdef Q_OS_ANDROID
QUrl url(androidPathCoverArtList);
cvrArtPath = url.toLocalFile();
QDir fldr(cvrArtPath);
if(!fldr.exists())
fldr.mkdir(cvrArtPath);
url = androidPathCoverArt;
cvrArtPath = url.toLocalFile();
fldr.setPath(cvrArtPath);
if(!fldr.exists())
fldr.mkdir(cvrArtPath);
svPath = cvrArtPath;
#endif
initCvrArtList(); // initializes list of cover arts
}
void MediaExtractor::initCvrArtList()
{
QDir dir(cvrArtPath);
cvrArtListInfo.clear();
cvrArtListInfo = dir.entryInfoList(QDir::Files);
}
QString MediaExtractor::getTitle(QString mediaPath)
{
QUrl mediaURL(mediaPath);
QString localFile = mediaURL.toLocalFile();
QString title;
TagLib::FileRef mediaFile(localFile.toStdString().c_str());
if(!mediaFile.isNull() && mediaFile.tag())
{
TagLib::Tag *mediaTag = mediaFile.tag();
title = mediaTag->title().toCString();
if(title.isEmpty() | title.isNull())
title = mediaURL.fileName();
}
return title;
}
QString MediaExtractor::getArtist(QString mediaPath)
{
QUrl mediaURL(mediaPath);
QString localFile = mediaURL.toLocalFile();
QString artist;
TagLib::FileRef mediaFile(localFile.toStdString().c_str());
if(!mediaFile.isNull() && mediaFile.tag())
{
TagLib::Tag *mediaTag = mediaFile.tag();
artist = mediaTag->artist().toCString();
if(artist.isEmpty() | artist.isNull())
artist = "unknown";
}
return artist;
}
void MediaExtractor::extractAlbumCover(QString mediaPath)
{
static const char *IdPicture = "APIC";
bool already = false;
QString title;
QUrl mediaURL(mediaPath);
QString localFile = mediaURL.toLocalFile();
TagLib::FileRef mediaFile(localFile.toStdString().c_str());
if(!mediaFile.isNull() && mediaFile.tag())
{
TagLib::Tag *mediaTag = mediaFile.tag();
title = mediaTag->title().toCString();
if(title.isEmpty() | title.isNull())
title = mediaURL.fileName();
int index = title.lastIndexOf(".");
title = title.mid(0,index);
title.prepend(svPath+"/");
foreach(QFileInfo itm, cvrArtListInfo)
{
if(itm.filePath() == title)
{
already = true;
break;
}
}
}
if(!already)
{
TagLib::MPEG::File mpegFile(localFile.toStdString().c_str());
TagLib::ID3v2::Tag *id3v2tag = mpegFile.ID3v2Tag();
TagLib::ID3v2::FrameList Frame;
TagLib::ID3v2::AttachedPictureFrame *PicFrame;
void *SrcImage;
unsigned long Size;
if ( id3v2tag )
{
Frame = id3v2tag->frameListMap()[IdPicture];
if (!Frame.isEmpty() )
{
for(TagLib::ID3v2::FrameList::ConstIterator it = Frame.begin(); it != Frame.end(); ++it)
{
PicFrame = (TagLib::ID3v2::AttachedPictureFrame *)(*it);
if ( PicFrame->type() == TagLib::ID3v2::AttachedPictureFrame::FrontCover)
{
Size = PicFrame->picture().size();
SrcImage = malloc ( Size );
if ( SrcImage )
{
FILE *jpegFile;
jpegFile = fopen(title.toStdString().c_str(),"wb");
memcpy ( SrcImage, PicFrame->picture().data(), Size );
fwrite(SrcImage,Size,1, jpegFile);
fclose(jpegFile);
free( SrcImage );
}
}
}
}
}
already = false;
}
initCvrArtList();
}
QString MediaExtractor::getAlbumCover(QString mediaTitle)
{
QString path;
#ifdef Q_OS_LINUX
path = linuxPath+"/";
#endif
#ifdef Q_OS_ANDROID
path = androidPathCoverArt+"/";
#endif
QString cvrPath = "";
int index = mediaTitle.lastIndexOf(".");
mediaTitle = mediaTitle.mid(0,index);
foreach(QFileInfo itm, cvrArtListInfo)
{
if(itm.fileName().contains(mediaTitle))
{
cvrPath = mediaTitle.prepend(path);
qDebug()<<"find";
break;
}
}
return cvrPath;
}
MediaExtractor::~MediaExtractor()
{
qDebug()<<"C++ Destructor";
}