1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * WATCHOUT - this is meant to be very lightweight an not even dependent
21 * on libFLAC, so there are a couple places where FLAC__* variables are
22 * duplicated here. Look for 'DUPLICATE:' in comments.
33 static const char *sync_string_ = "fLaC"; /* DUPLICATE:FLAC__STREAM_SYNC_STRING */
34 static const char *metadata_type_string_[] = { /* DUPLICATE:FLAC__MetaDataTypeString */
40 static int usage(const char *message, ...);
41 static bool list(FILE *f, bool verbose);
42 static uint32 unpack_uint32(byte *b, unsigned bytes);
43 static uint64 unpack_uint64(byte *b, unsigned bytes);
44 static void hexdump(const byte *buf, unsigned bytes);
46 int main(int argc, char *argv[])
49 bool verbose = false, list_mode = true;
55 for(i = 1; i < argc; i++) {
56 if(argv[i][0] != '-' || argv[i][1] == 0)
58 if(0 == strcmp(argv[i], "-l"))
60 else if(0 == strcmp(argv[i], "-v"))
62 else if(0 == strcmp(argv[i], "-v-"))
65 return usage("ERROR: invalid option '%s'\n", argv[i]);
68 if(i + (list_mode? 1:2) != argc)
69 return usage("ERROR: invalid arguments (more/less than %d filename%s?)\n", (list_mode? 1:2), (list_mode? "":"s"));
72 FILE *f = fopen(argv[i], "r");
75 fprintf(stderr, "ERROR opening %s\n", argv[i]);
88 int usage(const char *message, ...)
93 va_start(args, message);
95 (void) vfprintf(stderr, message, args);
100 printf("==============================================================================\n");
101 printf("metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
102 printf("Copyright (C) 2001 Josh Coalson\n");
104 printf("This program is free software; you can redistribute it and/or\n");
105 printf("modify it under the terms of the GNU General Public License\n");
106 printf("as published by the Free Software Foundation; either version 2\n");
107 printf("of the License, or (at your option) any later version.\n");
109 printf("This program is distributed in the hope that it will be useful,\n");
110 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
111 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
112 printf("GNU General Public License for more details.\n");
114 printf("You should have received a copy of the GNU General Public License\n");
115 printf("along with this program; if not, write to the Free Software\n");
116 printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
117 printf("==============================================================================\n");
119 printf(" metaflac [options] infile [outfile]\n");
121 printf("options:\n");
122 printf(" -l : list metadata blocks\n");
123 printf(" -v : verbose\n");
128 bool list(FILE *f, bool verbose)
132 FLAC__StreamMetaData metadata;
133 unsigned blocknum = 0, byte_offset = 0, i;
135 /* read the stream sync code */
136 if(fread(buf, 1, 4, f) < 4 || memcmp(buf, sync_string_, 4)) {
137 fprintf(stderr, "ERROR: not a FLAC file (no '%s' header)\n", sync_string_);
142 /* read the metadata blocks */
144 /* read the metadata block header */
145 if(fread(buf, 1, 4, f) < 4) {
146 fprintf(stderr, "ERROR: short count reading metadata block header\n");
149 metadata.is_last = (buf[0] & 0x80)? true:false;
150 metadata.type = (FLAC__MetaDataType)(buf[0] & 0x7f);
151 metadata.length = unpack_uint32(buf+1, 3);
154 printf("METADATA block #%u\n", blocknum);
155 printf("byte offset: %u\n", byte_offset);
156 printf("type: %u (%s)\n", (unsigned)metadata.type, metadata.type<=FLAC__METADATA_TYPE_APPLICATION? metadata_type_string_[metadata.type] : "UNKNOWN");
157 printf("is last: %s\n", metadata.is_last? "true":"false");
158 printf("length: %u\n", metadata.length);
160 if(metadata.length > sizeof(buf)) {
161 printf("SKIPPING large block\n\n");
165 /* read the metadata block data */
166 if(fread(buf, 1, metadata.length, f) < metadata.length) {
167 fprintf(stderr, "ERROR: short count reading metadata block data\n\n");
170 switch(metadata.type) {
171 case FLAC__METADATA_TYPE_STREAMINFO:
173 metadata.data.stream_info.min_blocksize = unpack_uint32(b, 2); b += 2;
174 metadata.data.stream_info.max_blocksize = unpack_uint32(b, 2); b += 2;
175 metadata.data.stream_info.min_framesize = unpack_uint32(b, 3); b += 3;
176 metadata.data.stream_info.max_framesize = unpack_uint32(b, 3); b += 3;
177 metadata.data.stream_info.sample_rate = (unpack_uint32(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
178 metadata.data.stream_info.channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
179 metadata.data.stream_info.bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 1) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
180 metadata.data.stream_info.total_samples = (((uint64)(b[3] & 0x0f)) << 32) | unpack_uint64(b+4, 4);
181 memcpy(metadata.data.stream_info.md5sum, b+8, 16);
183 case FLAC__METADATA_TYPE_PADDING:
188 case FLAC__METADATA_TYPE_APPLICATION:
189 memcpy(buf, metadata.data.application.id, 4);
190 metadata.data.application.data = buf+4;
193 printf("SKIPPING block of unknown type\n\n");
198 switch(metadata.type) {
199 case FLAC__METADATA_TYPE_STREAMINFO:
200 printf("minumum blocksize: %u samples\n", metadata.data.stream_info.min_blocksize);
201 printf("maxumum blocksize: %u samples\n", metadata.data.stream_info.max_blocksize);
202 printf("minimum framesize: %u bytes\n", metadata.data.stream_info.min_framesize);
203 printf("maximum framesize: %u bytes\n", metadata.data.stream_info.max_framesize);
204 printf("sample_rate: %u Hz\n", metadata.data.stream_info.sample_rate);
205 printf("channels: %u\n", metadata.data.stream_info.channels);
206 printf("bits-per-sample: %u\n", metadata.data.stream_info.bits_per_sample);
207 printf("total samples: %llu\n", metadata.data.stream_info.total_samples);
208 printf("MD5 signature: ");
209 for(i = 0; i < 16; i++)
210 printf("%02x", metadata.data.stream_info.md5sum[i]);
213 case FLAC__METADATA_TYPE_PADDING:
215 printf("pad contents:\n");
216 hexdump(buf, metadata.length);
219 case FLAC__METADATA_TYPE_APPLICATION:
220 printf("Application ID: ");
221 for(i = 0; i < 4; i++)
222 printf("%02x", metadata.data.application.id[i]);
225 printf("data contents:\n");
226 hexdump(metadata.data.application.data, metadata.length);
233 if(!metadata.is_last)
237 byte_offset += (4 + metadata.length);
238 } while (!metadata.is_last);
243 uint32 unpack_uint32(byte *b, unsigned bytes)
248 for(i = 0; i < bytes; i++)
249 ret = (ret << 8) | (uint32)(*b++);
254 uint64 unpack_uint64(byte *b, unsigned bytes)
259 for(i = 0; i < bytes; i++)
260 ret = (ret << 8) | (uint64)(*b++);
265 void hexdump(const byte *buf, unsigned bytes)
267 unsigned i, left = bytes;
270 for(i = 0; i < bytes; i += 16) {
272 "%02X %02X %02X %02X %02X %02X %02X %02X "
273 "%02X %02X %02X %02X %02X %02X %02X %02X "
274 "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
276 left > 0? (unsigned char)b[ 0] : 0,
277 left > 1? (unsigned char)b[ 1] : 0,
278 left > 2? (unsigned char)b[ 2] : 0,
279 left > 3? (unsigned char)b[ 3] : 0,
280 left > 4? (unsigned char)b[ 4] : 0,
281 left > 5? (unsigned char)b[ 5] : 0,
282 left > 6? (unsigned char)b[ 6] : 0,
283 left > 7? (unsigned char)b[ 7] : 0,
284 left > 8? (unsigned char)b[ 8] : 0,
285 left > 9? (unsigned char)b[ 9] : 0,
286 left > 10? (unsigned char)b[10] : 0,
287 left > 11? (unsigned char)b[11] : 0,
288 left > 12? (unsigned char)b[12] : 0,
289 left > 13? (unsigned char)b[13] : 0,
290 left > 14? (unsigned char)b[14] : 0,
291 left > 15? (unsigned char)b[15] : 0,
292 (left > 0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
293 (left > 1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
294 (left > 2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
295 (left > 3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
296 (left > 4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
297 (left > 5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
298 (left > 6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
299 (left > 7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
300 (left > 8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
301 (left > 9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
302 (left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
303 (left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
304 (left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
305 (left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
306 (left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
307 (left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '