-
Notifications
You must be signed in to change notification settings - Fork 0
/
opusart.c
193 lines (179 loc) · 5.22 KB
/
opusart.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <opus/opusfile.h>
void printHelp();
OggOpusFile *openFile(const char *filename);
OpusPictureTag *extractPictureTag(OggOpusFile *f, const int type);
void writePicureTagToFile(OpusPictureTag *tag, const char *filename);
void writePicureTagToStdout(OpusPictureTag *tag);
void writePictureTagInfo(OpusPictureTag *tag, FILE* to);
const char *pictureTypeToString(const int type);
int main(int argc, char **argv)
{
if (argc < 3 || argc > 4) {
printHelp();
return 1;
}
const int type = (argc == 4) ? atoi(argv[1]) : 3;
const char *in = argv[argc - 2];
const char *out = argv[argc - 1];
if (type < 0 || type > 20) {
fprintf(stderr, "Invalid picture type: %d\n", type);
return 1;
}
OggOpusFile *f = openFile(in);
OpusPictureTag *pt = extractPictureTag(f, type);
if (strcmp(out, "-") == 0) {
writePictureTagInfo(pt, stderr);
writePicureTagToStdout(pt);
} else {
writePictureTagInfo(pt, stdout);
writePicureTagToFile(pt, out);
}
op_free(f);
opus_picture_tag_clear(pt);
}
OggOpusFile *openFile(const char *filename)
{
int error = 0;
OggOpusFile *f = op_open_file(filename, &error);
if (error != 0)
{
fprintf(stderr, "Error opening file %s: %d\n", filename, error);
exit(EXIT_FAILURE);
}
return f;
}
OpusPictureTag *extractPictureTag(OggOpusFile *f, const int type)
{
const OpusTags* t = op_tags(f, 0);
OpusPictureTag *pt = malloc(sizeof(OpusPictureTag));
int n = opus_tags_query_count(t, "METADATA_BLOCK_PICTURE");
for (int i = 0; i < n; i++) {
int error = opus_picture_tag_parse(pt, opus_tags_query(t, "METADATA_BLOCK_PICTURE", 0));
if (error == 0) {
if (pt->type == type) {
return pt;
}
} else {
fprintf(stderr, "Error parsing picture tag: %d\n", error);
}
}
opus_picture_tag_clear(pt);
fprintf(stderr, "No picture tag of type %d [%s] found!\n", type, pictureTypeToString(type));
exit(EXIT_FAILURE);
}
void writePictureTagInfo(OpusPictureTag *tag, FILE* to)
{
fprintf(to, "Info:\n"
" Type: %d [%s]\n"
" MIME: %s\n"
" Description: %s\n"
" Width: %d\n"
" Height: %d\n"
" Depth: %d\n"
" Colors (GIF): %d\n"
" Picture data length: %d\n",
tag->type,
pictureTypeToString(tag->type),
tag->mime_type,
tag->description,
tag->width,
tag->height,
tag->depth,
tag->colors,
tag->data_length);
}
const char *pictureTypeToString(const int type)
{
switch (type)
{
case 0:
return "Other";
case 1:
return "32x32 pixels 'file icon' (PNG only)";
case 2:
return "Other file icon";
case 3:
return "Cover (front)";
case 4:
return "Cover (back)";
case 5:
return "Leaflet page";
case 6:
return "Media (e.g. label side of CD)";
case 7:
return "Lead artist/lead performer/soloist";
case 8:
return "Artist/performer";
case 9:
return "Conductor";
case 10:
return "Band/Orchestra";
case 11:
return "Composer";
case 12:
return "Lyricist/text writer";
case 13:
return "Recording Location";
case 14:
return "During recording";
case 15:
return "During performance";
case 16:
return "Movie/video screen capture";
case 17:
return "A bright colored fish";
case 18:
return "Illustration";
case 19:
return "Band/artist logotype";
case 20:
return "Publisher/Studio logotype";
default:
return "Unknown";
}
}
void writePicureTagToFile(OpusPictureTag *tag, const char *filename)
{
FILE *img = fopen(filename, "w");
fwrite(tag->data, tag->data_length, 1, img);
fclose(img);
}
void writePicureTagToStdout(OpusPictureTag *tag) {
fwrite(tag->data, tag->data_length, 1, stdout);
}
void printHelp()
{
printf("Usage: opusart [type] opus_file image_file\n\n"
"Read opus cover art.\n\n"
"opus_file can be:\n"
"filename\tfile\n\n"
"image_file can be:\n"
"filename\tfile\n"
"\t-\t\tstdout\n\n"
"type can be:\n"
"0\t\tOther\n"
"1\t\t32x32 pixels 'file icon' (PNG only)\n"
"2\t\tOther file icon\n"
"3\t\tCover (front) [default]\n"
"4\t\tCover (back)\n"
"5\t\tLeaflet page\n"
"6\t\tMedia (e.g. label side of CD)\n"
"7\t\tLead artist/lead performer/soloist\n"
"8\t\tArtist/performer\n"
"9\t\tConductor\n"
"10\t\tBand/Orchestra\n"
"11\t\tComposer\n"
"12\t\tLyricist/text writer\n"
"13\t\tRecording Location\n"
"14\t\tDuring recording\n"
"15\t\tDuring performance\n"
"16\t\tMovie/video screen capture\n"
"17\t\tA bright colored fish\n"
"18\t\tIllustration\n"
"19\t\tBand/artist logotype\n"
"20\t\tPublisher/Studio logotype\n\n");
}