-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagger.cpp
343 lines (324 loc) · 9.42 KB
/
tagger.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Copyright (C) 2011 Lukas Lalinsky <lalinsky@gmail.com>
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <tbytevector.h>
#include <mpegfile.h>
#include <id3v2tag.h>
#include <id3v2frame.h>
#include <id3v2header.h>
#include <urllinkframe.h>
#include <uniquefileidentifierframe.h>
#include <textidentificationframe.h>
#include <attachedpictureframe.h>
#include <tfilestream.h>
using namespace std;
using namespace TagLib;
struct Metadata {
Metadata() :
hasArtist(0),
hasAlbum(0),
hasAlbumArtist(0),
hasTitle(0),
hasGenre(0),
hasTrackNumber(0),
hasTrackCount(0),
hasYear(0),
hasImage(0),
hasPublisher(0)
{}
String artist, album, albumartist, title, genre, publisher;
Map<String, String> customUserTextFrames;
Map<String, String> customTextFrames;
Map<String, String> customUserUrlFrames;
Map<String, String> customUrlFrames;
Map<String, String> customUniqueFileIdentifierFrames;
int trackNumber, trackCount, year;
ByteVector image;
int hasArtist : 1;
int hasAlbum : 1;
int hasAlbumArtist : 1;
int hasTitle : 1;
int hasGenre : 1;
int hasTrackNumber : 1;
int hasTrackCount : 1;
int hasYear : 1;
int hasImage : 1;
int hasPublisher : 1;
};
void usage()
{
cout << endl;
cout << "Usage: zvq-tagger <fields> <files>" << endl;
cout << endl;
cout << "Where the valid fields are:" << endl;
cout << " -t <title>" << endl;
cout << " -a <artist>" << endl;
cout << " -A <album>" << endl;
cout << " -b <album artist>" << endl;
cout << " -n <track number>" << endl;
cout << " -N <track count>" << endl;
cout << " -G <genre>" << endl;
cout << " -Y <year>" << endl;
cout << " -p <publisher>" << endl;
cout << " -i <image>" << endl;
cout << " -x <frame>=<value> (e.g. TIT1=Grouping, TXXX=ASIN=B00000J6AG)" << endl;
cout << endl;
exit(1);
}
ByteVector readFile(char *path)
{
FileStream imageStream(optarg);
return imageStream.readBlock(imageStream.length());
}
void setTextFrame(ID3v2::Tag *tag, const char *id, const String &text)
{
ID3v2::TextIdentificationFrame *f = new ID3v2::TextIdentificationFrame(id, String::UTF16);
f->setText(text);
tag->removeFrames(id);
tag->addFrame(f);
}
void setUserTextFrame(ID3v2::Tag *tag, const String &description, const String &text)
{
ID3v2::UserTextIdentificationFrame *f = new ID3v2::UserTextIdentificationFrame(String::UTF16);
f->setDescription(description);
f->setText(text);
ID3v2::UserTextIdentificationFrame *of = ID3v2::UserTextIdentificationFrame::find(tag, description);
if (of) {
tag->removeFrame(of);
}
tag->addFrame(f);
}
void setUrlFrame(ID3v2::Tag *tag, const char *id, const String &text)
{
ID3v2::UrlLinkFrame *f = new ID3v2::UrlLinkFrame(id);
f->setUrl(text);
tag->removeFrames(id);
tag->addFrame(f);
}
ID3v2::UserUrlLinkFrame *findUserUrlLinkFrame(ID3v2::Tag *tag, const String &description)
{
ID3v2::FrameList l = tag->frameList("WXXX");
for (ID3v2::FrameList::Iterator it = l.begin(); it != l.end(); ++it) {
ID3v2::UserUrlLinkFrame *f = dynamic_cast<ID3v2::UserUrlLinkFrame *>(*it);
if (f && f->description() == description) {
return f;
}
}
return 0;
}
void setUserUrlFrame(ID3v2::Tag *tag, const String &description, const String &text)
{
ID3v2::UserUrlLinkFrame *f = new ID3v2::UserUrlLinkFrame(String::UTF16);
f->setDescription(description);
f->setUrl(text);
ID3v2::UserUrlLinkFrame *of = findUserUrlLinkFrame(tag, description);
if (of) {
tag->removeFrame(of);
}
tag->addFrame(f);
}
ID3v2::UniqueFileIdentifierFrame *findUniqueFileIdentifierFrame(ID3v2::Tag *tag, const String &owner)
{
ID3v2::FrameList l = tag->frameList("UFID");
for (ID3v2::FrameList::Iterator it = l.begin(); it != l.end(); ++it) {
ID3v2::UniqueFileIdentifierFrame *f = dynamic_cast<ID3v2::UniqueFileIdentifierFrame *>(*it);
if (f && f->owner() == owner) {
return f;
}
}
return 0;
}
void setUniqueFileIdentifierFrame(ID3v2::Tag *tag, const String &owner, const String &identifier)
{
ID3v2::UniqueFileIdentifierFrame *f = new ID3v2::UniqueFileIdentifierFrame(owner, identifier.toCString(true));
ID3v2::UniqueFileIdentifierFrame *of = findUniqueFileIdentifierFrame(tag, owner);
if (of) {
tag->removeFrame(of);
}
tag->addFrame(f);
}
String detectImageMimeType(const ByteVector &image)
{
if (image.size() > 4) {
const unsigned char *data = (const unsigned char *)image.data();
if (data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF && data[3] == 0xE0) {
return "image/jpeg";
}
else if (data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47) {
return "image/png";
}
}
return "application/octet-stream";
}
bool updateTags(char *path, Metadata *meta)
{
MPEG::File file(path);
if (!file.isValid() || file.readOnly()) {
return false;
}
ID3v2::Tag *tag = file.ID3v2Tag(true);
if (!tag) {
return false;
}
if (meta->hasArtist) {
setTextFrame(tag, "TPE1", meta->artist);
}
if (meta->hasAlbum) {
setTextFrame(tag, "TALB", meta->album);
}
if (meta->hasAlbumArtist) {
setTextFrame(tag, "TPE2", meta->albumartist);
}
if (meta->hasTitle) {
setTextFrame(tag, "TIT2", meta->title);
}
if (meta->hasTrackNumber) {
String text = String::number(meta->trackNumber);
if (meta->hasTrackCount) {
text += "/" + String::number(meta->trackCount);
}
setTextFrame(tag, "TRCK", text);
}
if (meta->hasGenre) {
setTextFrame(tag, "TCON", meta->genre);
}
if (meta->hasPublisher) {
setTextFrame(tag, "TPUB", meta->publisher);
}
if (meta->hasYear) {
setTextFrame(tag, "TDRC", String::number(meta->year));
}
if (meta->hasImage) {
ID3v2::AttachedPictureFrame *f = new ID3v2::AttachedPictureFrame();
f->setType(ID3v2::AttachedPictureFrame::Other);
f->setPicture(meta->image);
f->setMimeType(detectImageMimeType(meta->image));
tag->removeFrames("APIC");
tag->addFrame(f);
}
for (Map<String, String>::Iterator it = meta->customTextFrames.begin(); it != meta->customTextFrames.end(); ++it) {
setTextFrame(tag, it->first.toCString(true), it->second);
}
for (Map<String, String>::Iterator it = meta->customUserTextFrames.begin(); it != meta->customUserTextFrames.end(); ++it) {
setUserTextFrame(tag, it->first, it->second);
}
for (Map<String, String>::Iterator it = meta->customUrlFrames.begin(); it != meta->customUrlFrames.end(); ++it) {
setUrlFrame(tag, it->first.toCString(true), it->second);
}
for (Map<String, String>::Iterator it = meta->customUserUrlFrames.begin(); it != meta->customUserUrlFrames.end(); ++it) {
setUserUrlFrame(tag, it->first, it->second);
}
for (Map<String, String>::Iterator it = meta->customUniqueFileIdentifierFrames.begin(); it != meta->customUniqueFileIdentifierFrames.end(); ++it) {
setUniqueFileIdentifierFrame(tag, it->first, it->second);
}
return file.save(MPEG::File::ID3v1 | MPEG::File::ID3v2, false, 3);
}
int main(int argc, char **argv)
{
int ch;
Metadata meta;
while ((ch = getopt(argc, argv, "a:b:A:t:n:N:G:Y:p:i:x:")) != -1) {
switch (ch) {
case 'a':
meta.artist = String(optarg, String::UTF8);
meta.hasArtist = 1;
break;
case 'b':
meta.albumartist = String(optarg, String::UTF8);
meta.hasAlbumArtist = 1;
break;
case 'A':
meta.album = String(optarg, String::UTF8);
meta.hasAlbum = 1;
break;
case 't':
meta.title = String(optarg, String::UTF8);
meta.hasTitle = 1;
break;
case 'n':
meta.trackNumber = atoi(optarg);
meta.hasTrackNumber = 1;
break;
case 'N':
meta.trackCount = atoi(optarg);
meta.hasTrackCount = 1;
break;
case 'G':
meta.genre = String(optarg, String::UTF8);
meta.hasGenre = 1;
break;
case 'p':
meta.publisher = String(optarg, String::UTF8);
meta.hasPublisher = 1;
break;
case 'Y':
meta.year = atoi(optarg);
meta.hasYear = 1;
break;
case 'i':
meta.image = readFile(optarg);
meta.hasImage = 1;
break;
case 'x':
{
String tag(optarg, String::UTF8);
int offset = tag.find("=");
if (offset == -1) {
cerr << "invalid tag name " << tag.to8Bit() << endl;
break;
}
String name = tag.substr(0, offset).upper();
String value = tag.substr(offset + 1);
if (name.size() != 4) {
cerr << "invalid tag name " << tag.to8Bit() << endl;
break;
}
cerr << "parsing " << name.to8Bit() << " " << value.to8Bit() << endl;
if (name == "TXXX" || name == "WXXX" || name == "UFID") {
offset = value.find("=");
if (offset == -1) {
cerr << "invalid tag name " << tag.to8Bit() << endl;
break;
}
String description = value.substr(0, offset);
value = value.substr(offset + 1);
if (name == "TXXX") {
meta.customUserTextFrames[description] = value;
}
else if (name == "UFID") {
meta.customUniqueFileIdentifierFrames[description] = value;
}
else {
meta.customUserUrlFrames[description] = value;
}
}
else if (name[0] == 'T') {
meta.customTextFrames[name] = value;
}
else if (name[0] == 'W') {
meta.customUrlFrames[name] = value;
}
}
break;
case '?':
default:
usage();
}
}
int exitCode = 0;
for (int i = optind; i < argc; i++) {
if (!updateTags(argv[i], &meta)) {
cerr << "unable to update file " << argv[i] << endl;
exitCode = 2;
}
}
return exitCode;
}