-
Notifications
You must be signed in to change notification settings - Fork 1
/
audiotags.c
236 lines (213 loc) · 4.83 KB
/
audiotags.c
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
/* THIS FILE IS PUBLIC DOMAIN. NO WARRANTY OF ANY KIND. NO RIGHTS RESERVED. */
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tag_c.h>
static char *progname = "";
enum {
tag_item_artist,
tag_item_album,
tag_item_title,
tag_item_tracknumber,
tag_item_year,
tag_item_genre,
tag_item_comment,
tag_item_bitrate,
tag_item_samplerate,
tag_item_channels,
tag_item_duration,
tag_item_NUM
};
static int show_items[tag_item_NUM];
static int show_all = 1;
static void
usage(void)
{
fprintf(stderr,
"Usage: %s [OPTIONS] FILE\n", progname);
fprintf(stderr,
"Options:\n"
" --help Display this message.\n"
" --quiet Don't show any error output.\n"
" --show-album Display the album.\n"
" --show-artist Display the artist.\n"
" --show-bitrate Display the bitrate.\n"
" --show-channels Display the number of channels.\n"
" --show-duration Display the duration.\n"
" --show-genre Display the genre.\n"
" --show-samplerate Display the samplerate.\n"
" --show-title Display the title.\n"
" --show-tracknumber Display the tracknumber.\n"
" --show-year Display the year.\n"
);
exit(EXIT_FAILURE);
}
#define SELECTED(item) (show_items[tag_item_ ## item])
static int
print_tags(const char *filename)
{
TagLib_File *file;
TagLib_Tag *tag;
const TagLib_AudioProperties *properties;
if (show_all) {
int i;
for (i = 0; i < tag_item_NUM; i++) {
show_items[i] = 1;
}
}
file = taglib_file_new(filename);
if (NULL == file) {
return -1;
}
taglib_set_strings_unicode(1);
tag = taglib_file_tag(file);
properties = taglib_file_audioproperties(file);
if (SELECTED(artist)) {
printf("artist=%s\n", taglib_tag_artist(tag));
}
if (SELECTED(album)) {
printf("album=%s\n", taglib_tag_album(tag));
}
if (SELECTED(title)) {
printf("title=%s\n", taglib_tag_title(tag));
}
if (SELECTED(tracknumber)) {
unsigned value = taglib_tag_track(tag);
if (value > 0) {
printf("tracknumber=%u\n", value);
} else {
printf("tracknumber=\n");
}
}
if (SELECTED(year)) {
unsigned value = taglib_tag_year(tag);
if (value > 0) {
printf("year=%d\n", value);
} else {
printf("year=\n");
}
}
if (SELECTED(genre)) {
printf("genre=%s\n", taglib_tag_genre(tag));
}
if (SELECTED(comment)) {
printf("comment=%s\n", taglib_tag_comment(tag));
}
if (SELECTED(bitrate)) {
int value = taglib_audioproperties_bitrate(properties);
if (value > 0) {
printf("bitrate=%d\n", value);
} else {
printf("bitrate=\n");
}
}
if (SELECTED(samplerate)) {
int value = taglib_audioproperties_samplerate(properties);
if (value > 0) {
printf("samplerate=%d\n", value);
} else {
printf("samplerate=\n");
}
}
if (SELECTED(channels)) {
int value = taglib_audioproperties_channels(properties);
if (value > 0) {
printf("channels=%d\n", value);
} else {
printf("channels=\n");
}
}
if (SELECTED(duration)) {
int value = taglib_audioproperties_length(properties);
if (value >= 0) {
printf("duration=%u:%02u\n", value / 60, value % 60);
} else {
printf("duration=\n");
}
}
taglib_tag_free_strings();
taglib_file_free(file);
return 0;
}
static int
is_regular_file(const char *filename)
{
struct stat sb;
errno = 0;
if (stat(filename, &sb))
return 0;
else
return 0 != S_ISREG(sb.st_mode);
}
#define SHIFT do { if (argc > 0) { argc--; argv++; } } while (0)
#define SHOW(item) \
else if (0 == strcmp(s, "--show-" #item )) { \
show_all = 0; \
show_items[( tag_item_ ## item )] = 1;
int
main(int argc, char *argv[])
{
int help = 0;
int quiet = 0;
const char *filename;
progname = strrchr(*argv, '/');
progname = progname ? &progname[1] : *argv;
SHIFT;
while (argc > 0) {
const char *s = *argv;
if (0 == strcmp(s, "--help")) {
help = 1;
break;
} else if (0 == strcmp(s, "--quiet")) {
quiet = 1;
} SHOW(artist)
} SHOW(album)
} SHOW(title)
} SHOW(tracknumber)
} SHOW(year)
} SHOW(genre)
} SHOW(comment)
} SHOW(bitrate)
} SHOW(samplerate)
} SHOW(channels)
} SHOW(duration)
} else if (0 == strcmp(s, "--")) {
SHIFT;
break;
} else if ('-' == s[0]) {
fprintf(stderr, "ERROR: Unsupported argument \"%s\"\n", s);
usage();
} else {
break;
}
SHIFT;
}
if (1 != argc) {
fprintf(stderr, "ERROR: %s\n",
argc < 1 ? "Missing argument." : "Too many arguments.");
usage();
}
if (help) {
usage();
}
filename = *argv;
if (!is_regular_file(filename)) {
fprintf(stderr, "ERROR: %s: \"%s\"\n",
errno ? strerror(errno) : "Not a regular file",
filename);
exit(EXIT_FAILURE);
}
if (quiet) {
fflush(stderr);
if (NULL == freopen("/dev/null", "w", stderr)) {
exit(EXIT_FAILURE);
}
}
if (print_tags(filename)) {
fprintf(stderr, "ERROR: Cannot handle file: \"%s\"\n", filename);
exit(EXIT_FAILURE);
}
return 0;
}