add --export-picture-to option to metaflac

This commit is contained in:
Josh Coalson
2006-11-02 03:40:44 +00:00
parent 7afb1553c7
commit 989f26701d
9 changed files with 162 additions and 31 deletions

View File

@@ -360,6 +360,7 @@ FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_f
ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
break;
case OP__IMPORT_PICTURE:
case OP__EXPORT_PICTURE_TO:
ok = do_shorthand_operation__picture(filename, chain, operation, needs_write);
break;
case OP__ADD_SEEKPOINT:

View File

@@ -28,6 +28,7 @@
#include "share/grabbag.h" /* for grabbag__picture_parse_specification */
static FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write);
static FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename);
FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
{
@@ -53,6 +54,52 @@ FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_
ok = false;
}
}
if(ok) {
/* check global PICTURE constraints (max 1 block each of type=1 and type=2) */
while(FLAC__metadata_iterator_prev(iterator))
;
do {
FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_PICTURE) {
if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
if(has_type1) {
print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename);
ok = false;
}
has_type1 = true;
}
else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
if(has_type2) {
print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename);
ok = false;
}
has_type2 = true;
}
}
} while(FLAC__metadata_iterator_next(iterator));
}
break;
case OP__EXPORT_PICTURE_TO:
{
const Argument_BlockNumber *a = operation->argument.export_picture_to.block_number_link;
int block_number = (a && a->num_entries > 0)? (int)a->entries[0] : -1;
unsigned i = 0;
do {
FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_PICTURE && (block_number < 0 || i == (unsigned)block_number))
picture = block;
i++;
} while(FLAC__metadata_iterator_next(iterator) && 0 == picture);
if(0 == picture) {
if(block_number < 0)
fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block\n", filename);
else
fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block at block #%d\n", filename, block_number);
ok = false;
}
else
ok = export_pic_to(filename, picture, operation->argument.filename.value);
}
break;
default:
ok = false;
@@ -60,30 +107,6 @@ FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_
break;
};
/* check global PICTURE constraints (max 1 block each of type=1 and type=2) */
while(FLAC__metadata_iterator_prev(iterator))
;
do {
FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
if(block->type == FLAC__METADATA_TYPE_PICTURE) {
if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
if(has_type1) {
print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename);
ok = false;
}
has_type1 = true;
}
else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
if(has_type2) {
print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename);
ok = false;
}
has_type2 = true;
}
}
} while(FLAC__metadata_iterator_next(iterator));
FLAC__metadata_iterator_delete(iterator);
return ok;
}
@@ -116,3 +139,33 @@ FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture,
*needs_write = true;
return true;
}
FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename)
{
FILE *f;
const FLAC__uint32 len = picture->data.picture.data_length;
if(0 == pic_filename || strlen(pic_filename) == 0) {
fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
return false;
}
if(0 == strcmp(pic_filename, "-"))
f = stdout;
else
f = fopen(pic_filename, "w");
if(0 == f) {
fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, pic_filename, strerror(errno));
return false;
}
if(fwrite(picture->data.picture.data, 1, len, f) != len) {
fprintf(stderr, "%s: ERROR: writing PICTURE data to file\n", filename);
return false;
}
if(f != stdout)
fclose(f);
return true;
}

View File

@@ -73,6 +73,7 @@ struct share__option long_options_[] = {
{ "import-cuesheet-from", 1, 0, 0 },
{ "export-cuesheet-to", 1, 0, 0 },
{ "import-picture", 1, 0, 0 },
{ "export-picture-to", 1, 0, 0 },
{ "add-seekpoint", 1, 0, 0 },
{ "add-replay-gain", 0, 0, 0 },
{ "remove-replay-gain", 0, 0, 0 },
@@ -101,6 +102,7 @@ static void append_new_operation(CommandLineOptions *options, Operation operatio
static void append_new_argument(CommandLineOptions *options, Argument argument);
static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
static Argument *find_argument(CommandLineOptions *options, ArgumentType type);
static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
@@ -201,8 +203,14 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
}
/* check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to */
if((0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) || 0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) && options->num_files > 1) {
fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from' or '--export-cuesheet-to'\n");
if(
(
0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) ||
0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO) ||
0 != find_shorthand_operation(options, OP__EXPORT_PICTURE_TO)
) && options->num_files > 1
) {
fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from', '--export-cuesheet-to' or '--export-picture-to'\n");
had_error = true;
}
@@ -271,6 +279,10 @@ void free_options(CommandLineOptions *options)
if(0 != op->argument.specification.value)
free(op->argument.specification.value);
break;
case OP__EXPORT_PICTURE_TO:
if(0 != op->argument.export_picture_to.filename)
free(op->argument.export_picture_to.filename);
break;
case OP__ADD_SEEKPOINT:
if(0 != op->argument.add_seekpoint.specification)
free(op->argument.add_seekpoint.specification);
@@ -555,6 +567,16 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
ok = false;
}
}
else if(0 == strcmp(opt, "export-picture-to")) {
const Argument *arg = find_argument(options, ARG__BLOCK_NUMBER);
op = append_shorthand_operation(options, OP__EXPORT_PICTURE_TO);
FLAC__ASSERT(0 != option_argument);
if(!parse_string(option_argument, &(op->argument.export_picture_to.filename))) {
fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
ok = false;
}
op->argument.export_picture_to.block_number_link = arg? &(arg->value.block_number) : 0;
}
else if(0 == strcmp(opt, "add-seekpoint")) {
const char *violation;
char *spec;
@@ -733,6 +755,15 @@ Operation *append_shorthand_operation(CommandLineOptions *options, OperationType
return options->ops.operations + (options->ops.num_operations - 1);
}
Argument *find_argument(CommandLineOptions *options, ArgumentType type)
{
unsigned i;
for(i = 0; i < options->args.num_arguments; i++)
if(options->args.arguments[i].type == type)
return &options->args.arguments[i];
return 0;
}
Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
{
unsigned i;

View File

@@ -61,6 +61,7 @@ typedef enum {
OP__IMPORT_CUESHEET_FROM,
OP__EXPORT_CUESHEET_TO,
OP__IMPORT_PICTURE,
OP__EXPORT_PICTURE_TO,
OP__ADD_SEEKPOINT,
OP__ADD_REPLAY_GAIN,
OP__ADD_PADDING,
@@ -142,6 +143,11 @@ typedef struct {
Argument_AddSeekpoint *add_seekpoint_link;
} Argument_ImportCuesheetFrom;
typedef struct {
char *filename;
const Argument_BlockNumber *block_number_link; /* may be NULL to mean 'first PICTURE block' */
} Argument_ExportPictureTo;
typedef struct {
unsigned length;
} Argument_AddPadding;
@@ -157,6 +163,7 @@ typedef struct {
Argument_String filename;
Argument_String specification;
Argument_ImportCuesheetFrom import_cuesheet_from;
Argument_ExportPictureTo export_picture_to;
Argument_AddSeekpoint add_seekpoint;
Argument_AddPadding add_padding;
} argument;

View File

@@ -191,6 +191,12 @@ int long_usage(const char *message, ...)
fprintf(out, " you should also specify the number of colors used.\n");
fprintf(out, " FILE is the path to the picture file to be imported, or the URL if\n");
fprintf(out, " MIME type is -->\n");
fprintf(out, "--export-picture-to=FILE Export PICTURE block to a file. Use '-' for stdout.\n");
fprintf(out, " Only one FLAC file may be specified. The first PICTURE\n");
fprintf(out, " block will be exported unless --export-picture-to is\n");
fprintf(out, " preceded by a --block-number=# option to specify the exact\n");
fprintf(out, " metadata block to extract. Note that the block number is\n");
fprintf(out, " the one shown by --list.\n");
fprintf(out, "--add-replay-gain Calculates the title and album gains/peaks of the given\n");
fprintf(out, " FLAC files as if all the files were part of one album,\n");
fprintf(out, " then stores them in the VORBIS_COMMENT block. The tags\n");