1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002 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 more powerful operations yet to add:
21 add a seektable, using same args as flac
28 #include "FLAC/metadata.h"
37 # include "share/getopt.h"
40 /* getopt format struct; note we don't use short options so we just
41 set the 'val' field to 1 everywhere to indicate a valid option. */
42 static struct option long_options_[] = {
44 { "preserve-modtime", 0, 0, 1 },
45 { "with-filename", 0, 0, 1 },
46 { "no-filename", 0, 0, 1 },
47 { "dont-use-padding", 0, 0, 1 },
48 /* shorthand operations */
49 { "show-md5sum", 0, 0, 1 },
50 { "show-min-blocksize", 0, 0, 1 },
51 { "show-max-blocksize", 0, 0, 1 },
52 { "show-min-framesize", 0, 0, 1 },
53 { "show-max-framesize", 0, 0, 1 },
54 { "show-sample-rate", 0, 0, 1 },
55 { "show-channels", 0, 0, 1 },
56 { "show-bps", 0, 0, 1 },
57 { "show-total-samples", 0, 0, 1 },
58 { "show-vc-vendor", 0, 0, 1 },
59 { "show-vc-field", 1, 0, 1 },
60 { "remove-vc-all", 0, 0, 1 },
61 { "remove-vc-field", 1, 0, 1 },
62 { "remove-vc-firstfield", 1, 0, 1 },
63 { "set-vc-field", 1, 0, 1 },
64 /* major operations */
66 { "append", 0, 0, 1 },
67 { "remove", 0, 0, 1 },
68 { "remove-all", 0, 0, 1 },
69 { "merge-padding", 0, 0, 1 },
70 { "sort-padding", 0, 0, 1 },
71 { "block-number", 1, 0, 1 },
72 { "block-type", 1, 0, 1 },
73 { "except-block-type", 1, 0, 1 },
74 { "data-format", 1, 0, 1 },
75 { "application-data-format", 1, 0, 1 },
76 { "from-file", 1, 0, 1 },
82 OP__SHOW_MIN_BLOCKSIZE,
83 OP__SHOW_MAX_BLOCKSIZE,
84 OP__SHOW_MIN_FRAMESIZE,
85 OP__SHOW_MAX_FRAMESIZE,
89 OP__SHOW_TOTAL_SAMPLES,
93 OP__REMOVE_VC_FIRSTFIELD,
103 OP__EXCEPT_BLOCK_TYPE,
105 OP__APPLICATION_DATA_FORMAT,
111 } Argument_VcFieldName;
119 } Argument_BlockNumber;
123 } Argument_BlockType;
127 } Argument_DataFormat;
131 } Argument_ApplicationDataFormat;
138 OperationType operation;
140 Argument_VcFieldName show_vc_field;
141 Argument_VcFieldName remove_vc_field;
142 Argument_VcFieldName remove_vc_firstfield;
143 Argument_VcField set_vc_field;
144 Argument_BlockNumber block_number;
145 Argument_BlockType block_type;
146 Argument_BlockType except_block_type;
147 Argument_DataFormat data_format;
148 Argument_ApplicationDataFormat application_data_format;
149 Argument_FromFile from_file;
154 FLAC__bool preserve_modtime;
155 FLAC__bool prefix_with_filename;
156 FLAC__bool use_padding;
157 Operation *operations;
159 FLAC__bool has_shorthand_op;
160 FLAC__bool has_major_op;
161 FLAC__bool has_block_type;
162 FLAC__bool has_except_block_type;
164 } CommandLineOptions;
166 static void parse_options(int argc, char *argv[], CommandLineOptions *options);
167 static int short_usage(const char *message, ...);
168 static int long_usage(const char *message, ...);
169 static void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent);
171 int main(int argc, char *argv[])
173 CommandLineOptions o;
174 parse_options(argc, argv, &o);
178 void parse_options(int argc, char *argv[], CommandLineOptions *options)
181 int option_index = 1;
183 while ((ret = getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
186 fprintf(stderr, "Internal error parsing command options\n");
202 /* remaining bits must be the filenames */
203 if((param->mode == MODE_LIST && (argc-optind) != 1) ||
204 ((param->mode == MODE_WRITE || param->mode == MODE_APPEND) &&
205 ((argc-optind) < 1 || (argc-optind) > 2))) {
210 param->infilename = strdup(argv[optind]);
211 if (param->mode == MODE_WRITE || param->mode == MODE_APPEND)
215 param->tempoutfile = 1;
216 param->outfilename = malloc(strlen(param->infilename)+8);
217 strcpy(param->outfilename, param->infilename);
218 strcat(param->outfilename, ".vctemp");
221 param->outfilename = strdup(argv[optind+1]);
226 static void usage_header(FILE *out)
228 fprintf(out, "==============================================================================\n");
229 fprintf(out, "metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
230 fprintf(out, "Copyright (C) 2001,2002 Josh Coalson\n");
232 fprintf(out, "This program is free software; you can redistribute it and/or\n");
233 fprintf(out, "modify it under the terms of the GNU General Public License\n");
234 fprintf(out, "as published by the Free Software Foundation; either version 2\n");
235 fprintf(out, "of the License, or (at your option) any later version.\n");
237 fprintf(out, "This program is distributed in the hope that it will be useful,\n");
238 fprintf(out, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
239 fprintf(out, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
240 fprintf(out, "GNU General Public License for more details.\n");
242 fprintf(out, "You should have received a copy of the GNU General Public License\n");
243 fprintf(out, "along with this program; if not, write to the Free Software\n");
244 fprintf(out, "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
245 fprintf(out, "==============================================================================\n");
248 static void usage_summary(FILE *out)
250 fprintf(out, "Usage:\n");
251 fprintf(out, " metaflac [options] [operations] FLACfile [FLACfile ...]\n");
253 fprintf(out, "Use metaflac to list, add, remove, or edit metadata in one or more FLAC files.\n");
254 fprintf(out, "You may perform one major operation, or many shorthand operations at a time.\n");
256 fprintf(out, "Options:\n");
257 fprintf(out, "--preserve-modtime Preserve the original modification time in spite of edits\n");
258 fprintf(out, "--with-filename Prefix each output line with the FLAC file name\n");
259 fprintf(out, " (the default if more than one FLAC file is specified)\n");
260 fprintf(out, "--no-filename Do not prefix each output line with the FLAC file name\n");
261 fprintf(out, " (the default if only one FLAC file is specified)\n");
262 fprintf(out, "--dont-use-padding By default metaflac tries to use padding where possible\n");
263 fprintf(out, " to avoid rewriting the entire file if the metadata size\n");
264 fprintf(out, " changes. Use this option to tell metaflac to not use\n");
265 fprintf(out, " padding at all.\n");
268 int short_usage(const char *message, ...)
273 va_start(args, message);
275 (void) vfprintf(stderr, message, args);
280 usage_header(stderr);
281 fprintf(stderr, "\n");
282 fprintf(stderr, "This is the short help; for full help use 'metaflac --help'\n");
283 fprintf(stderr, "\n");
284 usage_summary(stderr);
286 return message? 1 : 0;
289 int long_usage(const char *message, ...)
291 FILE *out = (message? stderr : stdout);
295 va_start(args, message);
297 (void) vfprintf(stderr, message, args);
306 fprintf(out, "Shorthand operations:\n");
307 fprintf(out, "--show-md5sum Show the MD5 signature from the STREAMINFO block.\n");
308 fprintf(out, "--show-min-blocksize Show the minimum block size from the STREAMINFO block.\n");
309 fprintf(out, "--show-max-blocksize Show the maximum block size from the STREAMINFO block.\n");
310 fprintf(out, "--show-min-framesize Show the minimum frame size from the STREAMINFO block.\n");
311 fprintf(out, "--show-max-framesize Show the maximum frame size from the STREAMINFO block.\n");
312 fprintf(out, "--show-sample-rate Show the sample rate from the STREAMINFO block.\n");
313 fprintf(out, "--show-channels Show the number of channels from the STREAMINFO block.\n");
314 fprintf(out, "--show-bps Show the # of bits per sample from the STREAMINFO block.\n");
315 fprintf(out, "--show-total-samples Show the total # of samples from the STREAMINFO block.\n");
317 fprintf(out, "--show-vc-vendor Show the vendor string from the VORBIS_COMMENT block.\n");
318 fprintf(out, "--show-vc-field=name Show all Vorbis comment fields where the the field name\n");
319 fprintf(out, " matches 'name'.\n");
320 fprintf(out, "--remove-vc-field=name\n");
321 fprintf(out, " Remove all Vorbis comment fields whose field name is\n");
322 fprintf(out, " 'name'.\n");
323 fprintf(out, "--remove-vc-firstfield=name\n");
324 fprintf(out, " Remove first Vorbis comment field whose field name is\n");
325 fprintf(out, " 'name'.\n");
326 fprintf(out, "--remove-vc-all Remove all Vorbis comment fields, leaving only the\n");
327 fprintf(out, " vendor string in the VORBIS_COMMENT block.\n");
328 fprintf(out, "--set-vc-field=field Add a Vorbis comment field. The field must comply with\n");
329 fprintf(out, " the Vorbis comment spec, of the form \"NAME=VALUE\". If\n");
330 fprintf(out, " there is currently no VORBIS_COMMENT block, one will be\n");
331 fprintf(out, " created.\n");
333 fprintf(out, "Major operations:\n");
334 fprintf(out, "--list : List the contents of one or more metadata blocks to stdout. By\n");
335 fprintf(out, " default, all metadata blocks are listed in text format. Use the\n");
336 fprintf(out, " following options to change this behavior:\n");
338 fprintf(out, " --block-number=#[,#[...]]\n");
339 fprintf(out, " An optional comma-separated list of block numbers to display. The first\n");
340 fprintf(out, " block, the STREAMINFO block, is block 0.\n");
342 fprintf(out, " --block-type=type[,type[...]]\n");
343 fprintf(out, " --except-block-type=type[,type[...]]\n");
344 fprintf(out, " An optional comma-separated list of block types to included or ignored\n");
345 fprintf(out, " with this option. Use only one of --block-type or --except-block-type.\n");
346 fprintf(out, " The valid block types are: STREAMINFO, PADDING, APPLICATION, SEEKTABLE,\n");
347 fprintf(out, " VORBIS_COMMENT. You may narrow down the types of APPLICATION blocks\n");
348 fprintf(out, " displayed as follows:\n");
349 fprintf(out, " APPLICATION:abcd The APPLICATION block(s) whose textual repre-\n");
350 fprintf(out, " sentation of the 4-byte ID is \"abcd\"\n");
351 fprintf(out, " APPLICATION:0xXXXXXXXX The APPLICATION block(s) whose hexadecimal big-\n");
352 fprintf(out, " endian representation of the 4-byte ID is\n");
353 fprintf(out, " \"0xXXXXXXXX\". For the example \"abcd\" above the\n");
354 fprintf(out, " hexadecimal equivalalent is 0x61626364\n");
356 fprintf(out, " NOTE: if both --block-number and --[except-]block-type are specified,\n");
357 fprintf(out, " the result is the logical AND of both arguments.\n");
360 /*@@@ not implemented yet */
361 fprintf(out, " --data-format=binary|text\n");
362 fprintf(out, " By default a human-readable text representation of the data is displayed.\n");
363 fprintf(out, " You may specify --data-format=binary to dump the raw binary form of each\n");
364 fprintf(out, " metadata block. The output can be read in using a subsequent call to\n");
365 fprintf(out, " "metaflac --append --from-file=..."\n");
368 fprintf(out, " --application-data-format=hexdump|text\n");
369 fprintf(out, " If the application block you are displaying contains binary data but your\n");
370 fprintf(out, " --data-format=text, you can display a hex dump of the application data\n");
371 fprintf(out, " contents instead using --application-data-format=hexdump\n");
374 /*@@@ not implemented yet */
375 fprintf(out, "--append : Insert a metadata block from a file. The input file must be in the\n");
376 fprintf(out, " same format as generated with --list.\n");
378 fprintf(out, " --block-number=#\n");
379 fprintf(out, " Specify the insertion point (defaults to last block). The new block will\n");
380 fprintf(out, " be added after the given block number. This prevents the illegal insertion\n");
381 fprintf(out, " of a block before the first STREAMINFO block. You may not --append another\n");
382 fprintf(out, " STREAMINFO block.\n");
384 fprintf(out, " --from-file=filename\n");
385 fprintf(out, " Mandatory 'option' to specify the input file containing the block contents.\n");
387 fprintf(out, " --data-format=binary|text\n");
388 fprintf(out, " By default the block contents are assumed to be in binary format. You can\n");
389 fprintf(out, " override this by specifying --data-format=text\n");
392 fprintf(out, "--remove : Remove one or more metadata blocks from the metadata. Unless\n");
393 fprintf(out, " --dont-use-padding is specified, the blocks will be replaced with\n");
394 fprintf(out, " padding. You may not remove the STREAMINFO block.\n");
396 fprintf(out, " --block-number=#[,#[...]]\n");
397 fprintf(out, " --block-type=type[,type[...]]\n");
398 fprintf(out, " --except-block-type=type[,type[...]]\n");
399 fprintf(out, " See --list above for usage.\n");
401 fprintf(out, " NOTE: if both --block-number and --[except-]block-type are specified,\n");
402 fprintf(out, " the result is the logical AND of both arguments.\n");
404 fprintf(out, "--remove-all : Remove all metadata blocks (except the STREAMINFO block) from\n");
405 fprintf(out, " the metadata. Unless --dont-use-padding is specified, the\n");
406 fprintf(out, " blocks will be replaced with padding.\n");
408 fprintf(out, "--merge-padding : Merge adjacent PADDING blocks into single blocks.\n");
410 fprintf(out, "--sort-padding : Move all PADDING blocks to the end of the metadata and merge\n");
411 fprintf(out, " them into a single block.\n");
413 return message? 1 : 0;
416 void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent)
418 unsigned i, left = bytes;
419 const FLAC__byte *b = buf;
421 for(i = 0; i < bytes; i += 16) {
423 "%02X %02X %02X %02X %02X %02X %02X %02X "
424 "%02X %02X %02X %02X %02X %02X %02X %02X "
425 "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
427 left > 0? (unsigned char)b[ 0] : 0,
428 left > 1? (unsigned char)b[ 1] : 0,
429 left > 2? (unsigned char)b[ 2] : 0,
430 left > 3? (unsigned char)b[ 3] : 0,
431 left > 4? (unsigned char)b[ 4] : 0,
432 left > 5? (unsigned char)b[ 5] : 0,
433 left > 6? (unsigned char)b[ 6] : 0,
434 left > 7? (unsigned char)b[ 7] : 0,
435 left > 8? (unsigned char)b[ 8] : 0,
436 left > 9? (unsigned char)b[ 9] : 0,
437 left > 10? (unsigned char)b[10] : 0,
438 left > 11? (unsigned char)b[11] : 0,
439 left > 12? (unsigned char)b[12] : 0,
440 left > 13? (unsigned char)b[13] : 0,
441 left > 14? (unsigned char)b[14] : 0,
442 left > 15? (unsigned char)b[15] : 0,
443 (left > 0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
444 (left > 1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
445 (left > 2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
446 (left > 3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
447 (left > 4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
448 (left > 5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
449 (left > 6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
450 (left > 7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
451 (left > 8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
452 (left > 9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
453 (left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
454 (left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
455 (left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
456 (left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
457 (left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
458 (left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '