| 1 |
/* |
| 2 |
id3dump: Print ID3 tags of files, scanned using libmpg123. |
| 3 |
|
| 4 |
copyright 2007-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written by Thomas Orgis |
| 7 |
*/ |
| 8 |
|
| 9 |
/* Need snprintf(). */ |
| 10 |
#define _DEFAULT_SOURCE |
| 11 |
#define _BSD_SOURCE |
| 12 |
#include "mpg123config.h" |
| 13 |
#include "version.h" |
| 14 |
#include "compat/compat.h" |
| 15 |
#include "mpg123.h" |
| 16 |
#include "getlopt.h" |
| 17 |
#include <errno.h> |
| 18 |
#include <ctype.h> |
| 19 |
#ifdef _WIN32 |
| 20 |
#include "win32_support.h" |
| 21 |
#endif |
| 22 |
|
| 23 |
#include "common/debug.h" |
| 24 |
|
| 25 |
static int errors = 0; |
| 26 |
|
| 27 |
static struct |
| 28 |
{ |
| 29 |
int store_pics; |
| 30 |
int do_scan; |
| 31 |
int verbose; |
| 32 |
} param = |
| 33 |
{ |
| 34 |
FALSE |
| 35 |
, TRUE |
| 36 |
, FALSE |
| 37 |
}; |
| 38 |
|
| 39 |
static const char* progname; |
| 40 |
|
| 41 |
static void usage(int err) |
| 42 |
{ |
| 43 |
FILE* o = stdout; |
| 44 |
if(err) |
| 45 |
{ |
| 46 |
o = stderr; |
| 47 |
fprintf(o, "You made some mistake in program usage... let me briefly remind you:\n\n"); |
| 48 |
} |
| 49 |
fprintf(o, "Tool to dump ID3 meta data from MPEG audio files using libmpg123\n"); |
| 50 |
fprintf(o, "\tversion %s; written and copyright by Thomas Orgis and the mpg123 project\n", MPG123_VERSION); |
| 51 |
fprintf(o,"\nusage: %s [option(s)] file(s)\n", progname); |
| 52 |
fprintf(o,"\noptions:\n"); |
| 53 |
fprintf(o," -h --help give usage help\n"); |
| 54 |
fprintf(o," -n --no-scan do not scan entire file (just beginning)\n"); |
| 55 |
fprintf(o," -p --store-pics write APIC frames (album art pictures) to files\n"); |
| 56 |
fprintf(o," file names using whole input file name as prefix\n"); |
| 57 |
fprintf(o," -v --verbose verbose messages from parser\n"); |
| 58 |
fprintf(o,"\nNote that text output will always be in UTF-8, regardless of locale.\n"); |
| 59 |
exit(err); |
| 60 |
} |
| 61 |
static void want_usage(char* bla, topt *opts) |
| 62 |
{ |
| 63 |
usage(0); |
| 64 |
} |
| 65 |
|
| 66 |
static topt opts[] = |
| 67 |
{ |
| 68 |
{'h', "help", 0, want_usage, 0, 0} |
| 69 |
,{'n', "no-scan", GLO_INT, 0, ¶m.do_scan, FALSE} |
| 70 |
,{'p', "store-pics", GLO_INT, 0, ¶m.store_pics, TRUE} |
| 71 |
,{'v', "verbose", GLO_INT, 0, ¶m.verbose, TRUE} |
| 72 |
,{0, 0, 0, 0, 0, 0} |
| 73 |
}; |
| 74 |
|
| 75 |
/* Helper for v1 printing, get these strings their zero byte. */ |
| 76 |
void safe_print(char* name, char *data, size_t size) |
| 77 |
{ |
| 78 |
char safe[31]; |
| 79 |
if(size>30) return; |
| 80 |
|
| 81 |
memcpy(safe, data, size); |
| 82 |
safe[size] = 0; |
| 83 |
printf("%s: %s\n", name, safe); |
| 84 |
} |
| 85 |
|
| 86 |
/* Print out ID3v1 info. */ |
| 87 |
void print_v1(mpg123_id3v1 *v1) |
| 88 |
{ |
| 89 |
safe_print("Title", v1->title, sizeof(v1->title)); |
| 90 |
safe_print("Artist", v1->artist, sizeof(v1->artist)); |
| 91 |
safe_print("Album", v1->album, sizeof(v1->album)); |
| 92 |
safe_print("Year", v1->year, sizeof(v1->year)); |
| 93 |
safe_print("Comment", v1->comment, sizeof(v1->comment)); |
| 94 |
printf("Genre: %i", v1->genre); |
| 95 |
} |
| 96 |
|
| 97 |
/* Split up a number of lines separated by \n, \r, both or just zero byte |
| 98 |
and print out each line with specified prefix. */ |
| 99 |
void print_lines(const char* prefix, mpg123_string *inlines) |
| 100 |
{ |
| 101 |
size_t i; |
| 102 |
int hadcr = 0, hadlf = 0; |
| 103 |
char *lines = NULL; |
| 104 |
char *line = NULL; |
| 105 |
size_t len = 0; |
| 106 |
|
| 107 |
if(inlines != NULL && inlines->fill) |
| 108 |
{ |
| 109 |
lines = inlines->p; |
| 110 |
len = inlines->fill; |
| 111 |
} |
| 112 |
else return; |
| 113 |
|
| 114 |
line = lines; |
| 115 |
for(i=0; i<len; ++i) |
| 116 |
{ |
| 117 |
if(lines[i] == '\n' || lines[i] == '\r' || lines[i] == 0) |
| 118 |
{ |
| 119 |
char save = lines[i]; /* saving, changing, restoring a byte in the data */ |
| 120 |
if(save == '\n') ++hadlf; |
| 121 |
if(save == '\r') ++hadcr; |
| 122 |
if((hadcr || hadlf) && hadlf % 2 == 0 && hadcr % 2 == 0) line = ""; |
| 123 |
|
| 124 |
if(line) |
| 125 |
{ |
| 126 |
lines[i] = 0; |
| 127 |
printf("%s%s\n", prefix, line); |
| 128 |
line = NULL; |
| 129 |
lines[i] = save; |
| 130 |
} |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
hadlf = hadcr = 0; |
| 135 |
if(line == NULL) line = lines+i; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/* Print out the named ID3v2 fields. */ |
| 141 |
void print_v2(mpg123_id3v2 *v2) |
| 142 |
{ |
| 143 |
print_lines("Title: ", v2->title); |
| 144 |
print_lines("Artist: ", v2->artist); |
| 145 |
print_lines("Album: ", v2->album); |
| 146 |
print_lines("Year: ", v2->year); |
| 147 |
print_lines("Comment: ", v2->comment); |
| 148 |
print_lines("Genre: ", v2->genre); |
| 149 |
} |
| 150 |
|
| 151 |
/* Easy conversion to string via lookup. */ |
| 152 |
const char* pic_types[] = |
| 153 |
{ |
| 154 |
"other" |
| 155 |
,"icon" |
| 156 |
,"other icon" |
| 157 |
,"front cover" |
| 158 |
,"back cover" |
| 159 |
,"leaflet" |
| 160 |
,"media" |
| 161 |
,"lead" |
| 162 |
,"artist" |
| 163 |
,"conductor" |
| 164 |
,"orchestra" |
| 165 |
,"composer" |
| 166 |
,"lyricist" |
| 167 |
,"location" |
| 168 |
,"recording" |
| 169 |
,"performance" |
| 170 |
,"video" |
| 171 |
,"fish" |
| 172 |
,"illustration" |
| 173 |
,"artist logo" |
| 174 |
,"publisher logo" |
| 175 |
}; |
| 176 |
static const char* pic_type(int id) |
| 177 |
{ |
| 178 |
return (id >= 0 && id < (sizeof(pic_types)/sizeof(char*))) |
| 179 |
? pic_types[id] |
| 180 |
: "invalid type"; |
| 181 |
} |
| 182 |
|
| 183 |
/* Print out all stored ID3v2 fields with their 4-character IDs. */ |
| 184 |
void print_raw_v2(mpg123_id3v2 *v2) |
| 185 |
{ |
| 186 |
size_t i; |
| 187 |
for(i=0; i<v2->texts; ++i) |
| 188 |
{ |
| 189 |
char id[5]; |
| 190 |
char lang[4]; |
| 191 |
memcpy(id, v2->text[i].id, 4); |
| 192 |
id[4] = 0; |
| 193 |
memcpy(lang, v2->text[i].lang, 3); |
| 194 |
lang[3] = 0; |
| 195 |
if(v2->text[i].description.fill) |
| 196 |
printf("%s language(%s) description(%s)\n", id, lang, v2->text[i].description.p); |
| 197 |
else printf("%s language(%s)\n", id, lang); |
| 198 |
|
| 199 |
print_lines(" ", &v2->text[i].text); |
| 200 |
} |
| 201 |
for(i=0; i<v2->extras; ++i) |
| 202 |
{ |
| 203 |
char id[5]; |
| 204 |
memcpy(id, v2->extra[i].id, 4); |
| 205 |
id[4] = 0; |
| 206 |
printf( "%s description(%s)\n", |
| 207 |
id, |
| 208 |
v2->extra[i].description.fill ? v2->extra[i].description.p : "" ); |
| 209 |
print_lines(" ", &v2->extra[i].text); |
| 210 |
} |
| 211 |
for(i=0; i<v2->comments; ++i) |
| 212 |
{ |
| 213 |
char id[5]; |
| 214 |
char lang[4]; |
| 215 |
memcpy(id, v2->comment_list[i].id, 4); |
| 216 |
id[4] = 0; |
| 217 |
memcpy(lang, v2->comment_list[i].lang, 3); |
| 218 |
lang[3] = 0; |
| 219 |
printf( "%s description(%s) language(%s):\n", |
| 220 |
id, |
| 221 |
v2->comment_list[i].description.fill ? v2->comment_list[i].description.p : "", |
| 222 |
lang ); |
| 223 |
print_lines(" ", &v2->comment_list[i].text); |
| 224 |
} |
| 225 |
for(i=0; i<v2->pictures; ++i) |
| 226 |
{ |
| 227 |
mpg123_picture* pic; |
| 228 |
|
| 229 |
pic = &v2->picture[i]; |
| 230 |
fprintf(stderr, "APIC type(%i, %s) mime(%s) size(%zu)\n", |
| 231 |
pic->type, pic_type(pic->type), pic->mime_type.p, pic->size); |
| 232 |
print_lines(" ", &pic->description); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
const char* unknown_end = "picture"; |
| 237 |
|
| 238 |
static char* mime2end(mpg123_string* mime) |
| 239 |
{ |
| 240 |
size_t len; |
| 241 |
char* end; |
| 242 |
if(strncasecmp("image/",mime->p,6)) |
| 243 |
{ |
| 244 |
len = strlen(unknown_end)+1; |
| 245 |
end = malloc(len); |
| 246 |
memcpy(end, unknown_end, len); |
| 247 |
return end; |
| 248 |
} |
| 249 |
|
| 250 |
/* Else, use fmt out of image/fmt ... but make sure that usage stops at |
| 251 |
non-alphabetic character, as MIME can have funny stuff following a ";". */ |
| 252 |
for(len=1; len<mime->fill-6; ++len) |
| 253 |
{ |
| 254 |
if(!isalnum(mime->p[len-1+6])) break; |
| 255 |
} |
| 256 |
/* len now containing the number of bytes after the "/" up to the next |
| 257 |
invalid char or null */ |
| 258 |
if(len < 1) return "picture"; |
| 259 |
|
| 260 |
end = malloc(len); |
| 261 |
if(!end) exit(11); /* Come on, is it worth wasting lines for a message? |
| 262 |
If we're so broke, fprintf will also likely fail. */ |
| 263 |
|
| 264 |
memcpy(end, mime->p+6,len-1); |
| 265 |
end[len-1] = 0; |
| 266 |
return end; |
| 267 |
} |
| 268 |
|
| 269 |
/* Construct a sane file name without introducing spaces, then open. |
| 270 |
Example: /some/where/some.mp3.front_cover.jpeg |
| 271 |
If multiple ones are there: some.mp3.front_cover2.jpeg */ |
| 272 |
int open_picfile(const char* prefix, mpg123_picture* pic) |
| 273 |
{ |
| 274 |
char *end, *typestr, *pfn; |
| 275 |
const char* pictype; |
| 276 |
size_t i, len; |
| 277 |
int fd; |
| 278 |
unsigned long count = 1; |
| 279 |
|
| 280 |
pictype = pic_type(pic->type); |
| 281 |
len = strlen(pictype); |
| 282 |
if(!(typestr = malloc(len+1))) exit(11); |
| 283 |
memcpy(typestr, pictype, len); |
| 284 |
for(i=0; i<len; ++i) if(typestr[i] == ' ') typestr[i] = '_'; |
| 285 |
|
| 286 |
typestr[len] = 0; |
| 287 |
end = mime2end(&pic->mime_type); |
| 288 |
len = strlen(prefix)+1+strlen(typestr)+1+strlen(end); |
| 289 |
if(!(pfn = malloc(len+1))) exit(11); |
| 290 |
|
| 291 |
sprintf(pfn, "%s.%s.%s", prefix, typestr, end); |
| 292 |
pfn[len] = 0; |
| 293 |
|
| 294 |
errno = 0; |
| 295 |
fd = INT123_compat_open(pfn, O_CREAT|O_WRONLY|O_EXCL); |
| 296 |
while(fd < 0 && errno == EEXIST && ++count < ULONG_MAX) |
| 297 |
{ |
| 298 |
char dum[3]; |
| 299 |
size_t digits; |
| 300 |
|
| 301 |
// Just want to know the number of bytes needed. |
| 302 |
// Modern compiler diagnostics complain if limit is smaller than |
| 303 |
// format string, so increasing dummy to 3 characters. |
| 304 |
digits = snprintf(dum, 3, "%lu", count); |
| 305 |
if(!(pfn=INT123_safe_realloc(pfn, len+digits+1))) exit(11); |
| 306 |
|
| 307 |
sprintf(pfn, "%s.%s%lu.%s", prefix, typestr, count, end); |
| 308 |
pfn[len+digits] = 0; |
| 309 |
errno = 0; |
| 310 |
fd = INT123_compat_open(pfn, O_CREAT|O_WRONLY|O_EXCL); |
| 311 |
} |
| 312 |
printf("writing %s\n", pfn); |
| 313 |
if(fd < 0) |
| 314 |
{ |
| 315 |
error("Cannot open for writing (counter exhaust? permissions?)."); |
| 316 |
++errors; |
| 317 |
} |
| 318 |
|
| 319 |
free(end); |
| 320 |
free(typestr); |
| 321 |
free(pfn); |
| 322 |
return fd; |
| 323 |
} |
| 324 |
|
| 325 |
static void store_pictures(const char* prefix, mpg123_id3v2 *v2) |
| 326 |
{ |
| 327 |
int i; |
| 328 |
|
| 329 |
for(i=0; i<v2->pictures; ++i) |
| 330 |
{ |
| 331 |
int fd; |
| 332 |
mpg123_picture* pic; |
| 333 |
|
| 334 |
pic = &v2->picture[i]; |
| 335 |
fd = open_picfile(prefix, pic); |
| 336 |
if(fd >= 0) |
| 337 |
{ /* stream I/O for not having to care about interruptions */ |
| 338 |
FILE* picfile = INT123_compat_fdopen(fd, "w"); |
| 339 |
if(picfile) |
| 340 |
{ |
| 341 |
if(INT123_unintr_fwrite(pic->data, pic->size, 1, picfile) != 1) |
| 342 |
{ |
| 343 |
error("Failure to write data."); |
| 344 |
++errors; |
| 345 |
} |
| 346 |
if(fclose(picfile)) |
| 347 |
{ |
| 348 |
error("Failure to close (flush?)."); |
| 349 |
++errors; |
| 350 |
} |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
error1("Unable to fdopen output: %s)", INT123_strerror(errno)); |
| 355 |
++errors; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
int main(int argc, char **argv) |
| 362 |
{ |
| 363 |
int i, result; |
| 364 |
mpg123_handle* m; |
| 365 |
#if defined(WANT_WIN32_UNICODE) |
| 366 |
if(win32_cmdline_utf8(&argc,&argv) != 0) |
| 367 |
{ |
| 368 |
error("Cannot convert command line to UTF8!"); |
| 369 |
return 1; |
| 370 |
} |
| 371 |
#endif |
| 372 |
progname = argv[0]; |
| 373 |
|
| 374 |
while ((result = getlopt(argc, argv, opts))) |
| 375 |
switch (result) { |
| 376 |
case GLO_UNKNOWN: |
| 377 |
fprintf (stderr, "%s: Unknown option \"%s\".\n", |
| 378 |
progname, loptarg); |
| 379 |
usage(1); |
| 380 |
case GLO_NOARG: |
| 381 |
fprintf (stderr, "%s: Missing argument for option \"%s\".\n", |
| 382 |
progname, loptarg); |
| 383 |
usage(1); |
| 384 |
} |
| 385 |
|
| 386 |
#ifdef WIN32 |
| 387 |
fprintf(stderr, "WARNING: This tool is not yet adapted to run on Windows (file I/O, unicode arguments)!\n"); |
| 388 |
#endif |
| 389 |
if(loptind >= argc) usage(1); |
| 390 |
|
| 391 |
mpg123_init(); |
| 392 |
m = mpg123_new(NULL, NULL); |
| 393 |
mpg123_param(m, MPG123_ADD_FLAGS, MPG123_PICTURE, 0.); |
| 394 |
mpg123_param(m, MPG123_VERBOSE, param.verbose ? 4 : 0, 0.); |
| 395 |
mpg123_param(m, MPG123_RESYNC_LIMIT, -1, 0.0); |
| 396 |
|
| 397 |
for(i=loptind; i < argc; ++i) |
| 398 |
{ |
| 399 |
mpg123_id3v1 *v1; |
| 400 |
mpg123_id3v2 *v2; |
| 401 |
int meta; |
| 402 |
if(mpg123_open(m, argv[i]) != MPG123_OK) |
| 403 |
{ |
| 404 |
fprintf(stderr, "Cannot open %s: %s\n", argv[i], mpg123_strerror(m)); |
| 405 |
continue; |
| 406 |
} |
| 407 |
if(param.do_scan) mpg123_scan(m); |
| 408 |
mpg123_seek(m, 0, SEEK_SET); |
| 409 |
meta = mpg123_meta_check(m); |
| 410 |
if(meta & MPG123_ID3 && mpg123_id3(m, &v1, &v2) == MPG123_OK) |
| 411 |
{ |
| 412 |
printf("FILE: %s\n", argv[i]); |
| 413 |
printf("\n==== ID3v1 ====\n"); |
| 414 |
if(v1 != NULL) print_v1(v1); |
| 415 |
|
| 416 |
printf("\n==== ID3v2 ====\n"); |
| 417 |
if(v2 != NULL) print_v2(v2); |
| 418 |
|
| 419 |
printf("\n==== ID3v2 Raw frames ====\n"); |
| 420 |
if(v2 != NULL) |
| 421 |
{ |
| 422 |
print_raw_v2(v2); |
| 423 |
if(param.store_pics) |
| 424 |
store_pictures(argv[i], v2); |
| 425 |
} |
| 426 |
} |
| 427 |
else printf("Nothing found for %s.\n", argv[i]); |
| 428 |
|
| 429 |
mpg123_close(m); |
| 430 |
} |
| 431 |
mpg123_delete(m); |
| 432 |
mpg123_exit(); |
| 433 |
|
| 434 |
#if defined(WANT_WIN32_UNICODE) |
| 435 |
win32_cmdline_free(argc,argv); |
| 436 |
#endif |
| 437 |
if(errors) error1("Encountered %i errors along the way.", errors); |
| 438 |
return errors != 0; |
| 439 |
} |