mirror of
https://github.com/claunia/flac.git
synced 2025-12-16 18:54:26 +00:00
add --import-picture command to metaflac
This commit is contained in:
@@ -25,6 +25,7 @@ metaflac_SOURCES = \
|
||||
main.c \
|
||||
operations.c \
|
||||
operations_shorthand_cuesheet.c \
|
||||
operations_shorthand_picture.c \
|
||||
operations_shorthand_seektable.c \
|
||||
operations_shorthand_streaminfo.c \
|
||||
operations_shorthand_vorbiscomment.c \
|
||||
|
||||
@@ -36,6 +36,7 @@ SRCS_C = \
|
||||
main.c \
|
||||
operations.c \
|
||||
operations_shorthand_cuesheet.c \
|
||||
operations_shorthand_picture.c \
|
||||
operations_shorthand_seektable.c \
|
||||
operations_shorthand_streaminfo.c \
|
||||
operations_shorthand_vorbiscomment.c \
|
||||
|
||||
@@ -101,6 +101,10 @@ SOURCE=.\operations_shorthand_cuesheet.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\operations_shorthand_picture.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\operations_shorthand_seektable.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
@@ -58,6 +58,9 @@ extern FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, F
|
||||
/* from operations_shorthand_cuesheet.c */
|
||||
extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
|
||||
|
||||
/* from operations_shorthand_picture.c */
|
||||
extern FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
|
||||
|
||||
|
||||
FLAC__bool do_operations(const CommandLineOptions *options)
|
||||
{
|
||||
@@ -356,6 +359,9 @@ FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_f
|
||||
case OP__EXPORT_CUESHEET_TO:
|
||||
ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
|
||||
break;
|
||||
case OP__IMPORT_PICTURE:
|
||||
ok = do_shorthand_operation__picture(filename, chain, operation, needs_write);
|
||||
break;
|
||||
case OP__ADD_SEEKPOINT:
|
||||
ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
|
||||
break;
|
||||
|
||||
119
src/metaflac/operations_shorthand_picture.c
Normal file
119
src/metaflac/operations_shorthand_picture.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/* metaflac - Command-line FLAC metadata editor
|
||||
* Copyright (C) 2001,2002,2003,2004,2005,2006 Josh Coalson
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h> /* for snprintf() */
|
||||
#include <string.h>
|
||||
#include "options.h"
|
||||
#include "utils.h"
|
||||
#include "FLAC/assert.h"
|
||||
#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);
|
||||
|
||||
FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
|
||||
{
|
||||
FLAC__bool ok = true, has_type1 = false, has_type2 = false;
|
||||
FLAC__StreamMetadata *picture = 0;
|
||||
FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
|
||||
|
||||
if(0 == iterator)
|
||||
die("out of memory allocating iterator");
|
||||
|
||||
FLAC__metadata_iterator_init(iterator, chain);
|
||||
|
||||
switch(operation->type) {
|
||||
case OP__IMPORT_PICTURE:
|
||||
ok = import_pic_from(filename, &picture, operation->argument.specification.value, needs_write);
|
||||
if(ok) {
|
||||
/* append PICTURE block */
|
||||
while(FLAC__metadata_iterator_next(iterator))
|
||||
;
|
||||
if(!FLAC__metadata_iterator_insert_block_after(iterator, picture)) {
|
||||
print_error_with_chain_status(chain, "%s: ERROR: adding new PICTURE block to metadata", filename);
|
||||
FLAC__metadata_object_delete(picture);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ok = false;
|
||||
FLAC__ASSERT(0);
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
* local routines
|
||||
*/
|
||||
|
||||
FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write)
|
||||
{
|
||||
const char *error_message;
|
||||
|
||||
if(0 == specification || strlen(specification) == 0) {
|
||||
fprintf(stderr, "%s: ERROR: empty picture specification\n", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
*picture = grabbag__picture_parse_specification(specification, &error_message);
|
||||
|
||||
if(0 == *picture) {
|
||||
fprintf(stderr, "%s: ERROR: while parsing picture specification \"%s\": %s\n", filename, specification, error_message);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!FLAC__format_picture_is_legal(&(*picture)->data.picture, &error_message)) {
|
||||
fprintf(stderr, "%s: ERROR: new PICTURE block for \"%s\" is illegal: %s\n", filename, specification, error_message);
|
||||
return false;
|
||||
}
|
||||
|
||||
*needs_write = true;
|
||||
return true;
|
||||
}
|
||||
@@ -33,8 +33,8 @@ static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *bloc
|
||||
static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
|
||||
static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
|
||||
static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw);
|
||||
static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw);
|
||||
static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool raw);
|
||||
static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw);
|
||||
static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw);
|
||||
|
||||
FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw)
|
||||
{
|
||||
@@ -280,7 +280,7 @@ FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const
|
||||
}
|
||||
}
|
||||
|
||||
FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw)
|
||||
FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw)
|
||||
{
|
||||
FILE *f;
|
||||
char line[65536];
|
||||
@@ -338,7 +338,7 @@ FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, con
|
||||
return ret;
|
||||
}
|
||||
|
||||
FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool raw)
|
||||
FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw)
|
||||
{
|
||||
FILE *f;
|
||||
FLAC__bool ret;
|
||||
|
||||
@@ -80,6 +80,7 @@ struct share__option long_options_[] = {
|
||||
{ "export-vc-to", 1, 0, 0 }, /* deprecated */
|
||||
{ "import-cuesheet-from", 1, 0, 0 },
|
||||
{ "export-cuesheet-to", 1, 0, 0 },
|
||||
{ "import-picture", 1, 0, 0 },
|
||||
{ "add-seekpoint", 1, 0, 0 },
|
||||
{ "add-replay-gain", 0, 0, 0 },
|
||||
{ "remove-replay-gain", 0, 0, 0 },
|
||||
@@ -113,7 +114,7 @@ static Argument *append_argument(CommandLineOptions *options, ArgumentType type)
|
||||
static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
|
||||
static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
|
||||
static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
|
||||
static FLAC__bool parse_filename(const char *src, char **dest);
|
||||
static FLAC__bool parse_string(const char *src, char **dest);
|
||||
static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
|
||||
static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
|
||||
static FLAC__bool parse_add_padding(const char *in, unsigned *out);
|
||||
@@ -274,6 +275,10 @@ void free_options(CommandLineOptions *options)
|
||||
if(0 != op->argument.import_cuesheet_from.filename)
|
||||
free(op->argument.import_cuesheet_from.filename);
|
||||
break;
|
||||
case OP__IMPORT_PICTURE:
|
||||
if(0 != op->argument.specification.value)
|
||||
free(op->argument.specification.value);
|
||||
break;
|
||||
case OP__ADD_SEEKPOINT:
|
||||
if(0 != op->argument.add_seekpoint.specification)
|
||||
free(op->argument.add_seekpoint.specification);
|
||||
@@ -531,7 +536,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
|
||||
fprintf(stderr, "WARNING: --%s is deprecated, the new name is --import-tags-from\n", opt);
|
||||
op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
|
||||
FLAC__ASSERT(0 != option_argument);
|
||||
if(!parse_filename(option_argument, &(op->argument.filename.value))) {
|
||||
if(!parse_string(option_argument, &(op->argument.filename.value))) {
|
||||
fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
|
||||
ok = false;
|
||||
}
|
||||
@@ -541,7 +546,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
|
||||
fprintf(stderr, "WARNING: --%s is deprecated, the new name is --export-tags-to\n", opt);
|
||||
op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
|
||||
FLAC__ASSERT(0 != option_argument);
|
||||
if(!parse_filename(option_argument, &(op->argument.filename.value))) {
|
||||
if(!parse_string(option_argument, &(op->argument.filename.value))) {
|
||||
fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
|
||||
ok = false;
|
||||
}
|
||||
@@ -553,7 +558,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
|
||||
}
|
||||
op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
|
||||
FLAC__ASSERT(0 != option_argument);
|
||||
if(!parse_filename(option_argument, &(op->argument.import_cuesheet_from.filename))) {
|
||||
if(!parse_string(option_argument, &(op->argument.import_cuesheet_from.filename))) {
|
||||
fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
|
||||
ok = false;
|
||||
}
|
||||
@@ -561,11 +566,19 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
|
||||
else if(0 == strcmp(opt, "export-cuesheet-to")) {
|
||||
op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
|
||||
FLAC__ASSERT(0 != option_argument);
|
||||
if(!parse_filename(option_argument, &(op->argument.filename.value))) {
|
||||
if(!parse_string(option_argument, &(op->argument.filename.value))) {
|
||||
fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
else if(0 == strcmp(opt, "import-picture")) {
|
||||
op = append_shorthand_operation(options, OP__IMPORT_PICTURE);
|
||||
FLAC__ASSERT(0 != option_argument);
|
||||
if(!parse_string(option_argument, &(op->argument.specification.value))) {
|
||||
fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
else if(0 == strcmp(opt, "add-seekpoint")) {
|
||||
const char *violation;
|
||||
char *spec;
|
||||
@@ -829,7 +842,7 @@ FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
|
||||
return true;
|
||||
}
|
||||
|
||||
FLAC__bool parse_filename(const char *src, char **dest)
|
||||
FLAC__bool parse_string(const char *src, char **dest)
|
||||
{
|
||||
if(0 == src || strlen(src) == 0)
|
||||
return false;
|
||||
@@ -1024,6 +1037,9 @@ FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
|
||||
else if(0 == strcmp(q, "CUESHEET")) {
|
||||
out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
|
||||
}
|
||||
else if(0 == strcmp(q, "PICTURE")) {
|
||||
out->entries[entry++].type = FLAC__METADATA_TYPE_PICTURE;
|
||||
}
|
||||
else {
|
||||
free(s);
|
||||
return false;
|
||||
|
||||
@@ -60,6 +60,7 @@ typedef enum {
|
||||
OP__EXPORT_VC_TO,
|
||||
OP__IMPORT_CUESHEET_FROM,
|
||||
OP__EXPORT_CUESHEET_TO,
|
||||
OP__IMPORT_PICTURE,
|
||||
OP__ADD_SEEKPOINT,
|
||||
OP__ADD_REPLAY_GAIN,
|
||||
OP__ADD_PADDING,
|
||||
@@ -106,7 +107,7 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
char *value;
|
||||
} Argument_Filename;
|
||||
} Argument_String;
|
||||
|
||||
typedef struct {
|
||||
unsigned num_entries;
|
||||
@@ -153,7 +154,8 @@ typedef struct {
|
||||
Argument_StreaminfoUInt64 streaminfo_uint64;
|
||||
Argument_VcFieldName vc_field_name;
|
||||
Argument_VcField vc_field;
|
||||
Argument_Filename filename;
|
||||
Argument_String filename;
|
||||
Argument_String specification;
|
||||
Argument_ImportCuesheetFrom import_cuesheet_from;
|
||||
Argument_AddSeekpoint add_seekpoint;
|
||||
Argument_AddPadding add_padding;
|
||||
|
||||
@@ -131,9 +131,9 @@ int long_usage(const char *message, ...)
|
||||
fprintf(out, " Unless --no-utf8-convert is specified, the contents will\n");
|
||||
fprintf(out, " be converted to UTF-8 from the local charset. This can\n");
|
||||
fprintf(out, " be used to store a cuesheet in a tag (e.g.\n");
|
||||
fprintf(out, " --set-tag-from-file=\"CUESHEET=image.cue\"). Do not try to\n");
|
||||
fprintf(out, " store binary data in tag fields! Use APPLICATION blocks\n");
|
||||
fprintf(out, " for that.\n");
|
||||
fprintf(out, " --set-tag-from-file=\"CUESHEET=image.cue\"). Do not try\n");
|
||||
fprintf(out, " to store binary data in tag fields! Use APPLICATION\n");
|
||||
fprintf(out, " blocks for that.\n");
|
||||
fprintf(out, "--import-tags-from=FILE Import tags from a file. Use '-' for stdin. Each line\n");
|
||||
fprintf(out, " should be of the form NAME=VALUE. Multi-line comments\n");
|
||||
fprintf(out, " are currently not supported. Specify --remove-all-tags\n");
|
||||
@@ -144,11 +144,53 @@ int long_usage(const char *message, ...)
|
||||
fprintf(out, " --no-utf8-convert if necessary.\n");
|
||||
fprintf(out, "--import-cuesheet-from=FILE Import a cuesheet from a file. Use '-' for stdin.\n");
|
||||
fprintf(out, " Only one FLAC file may be specified. A seekpoint will be\n");
|
||||
fprintf(out, " added for each index point in the cuesheet to the SEEKTABLE\n");
|
||||
fprintf(out, " unless --no-cued-seekpoints is specified.\n");
|
||||
fprintf(out, " added for each index point in the cuesheet to the\n");
|
||||
fprintf(out, " SEEKTABLE unless --no-cued-seekpoints is specified.\n");
|
||||
fprintf(out, "--export-cuesheet-to=FILE Export CUESHEET block to a cuesheet file, suitable\n");
|
||||
fprintf(out, " for use by CD authoring software. Use '-' for stdout.\n");
|
||||
fprintf(out, " Only one FLAC file may be specified on the command line.\n");
|
||||
fprintf(out, "--import-picture=SPECIFICATION Import a picture and store it in a PICTURE block.\n");
|
||||
fprintf(out, " The SPECIFICATION is a string whose parts are separated\n");
|
||||
fprintf(out, " by | characters. Some parts may be left empty to invoke\n");
|
||||
fprintf(out, " default values. The specification format is:\n");
|
||||
fprintf(out, " [TYPE]|MIME-TYPE|[DESCRIPTION]|[WIDTHxHEIGHTxDEPTH[/COLORS]]|FILE\n");
|
||||
fprintf(out, " TYPE is optional; it is a number from one of:\n");
|
||||
fprintf(out, " 0: Other\n");
|
||||
fprintf(out, " 1: 32x32 pixels 'file icon' (PNG only)\n");
|
||||
fprintf(out, " 2: Other file icon\n");
|
||||
fprintf(out, " 3: Cover (front)\n");
|
||||
fprintf(out, " 4: Cover (back)\n");
|
||||
fprintf(out, " 5: Leaflet page\n");
|
||||
fprintf(out, " 6: Media (e.g. label side of CD)\n");
|
||||
fprintf(out, " 7: Lead artist/lead performer/soloist\n");
|
||||
fprintf(out, " 8: Artist/performer\n");
|
||||
fprintf(out, " 9: Conductor\n");
|
||||
fprintf(out, " 10: Band/Orchestra\n");
|
||||
fprintf(out, " 11: Composer\n");
|
||||
fprintf(out, " 12: Lyricist/text writer\n");
|
||||
fprintf(out, " 13: Recording Location\n");
|
||||
fprintf(out, " 14: During recording\n");
|
||||
fprintf(out, " 15: During performance\n");
|
||||
fprintf(out, " 16: Movie/video screen capture\n");
|
||||
fprintf(out, " 17: A bright coloured fish\n");
|
||||
fprintf(out, " 18: Illustration\n");
|
||||
fprintf(out, " 19: Band/artist logotype\n");
|
||||
fprintf(out, " 20: Publisher/Studio logotype\n");
|
||||
fprintf(out, " The default is 3 (front cover). There may only be one picture each\n");
|
||||
fprintf(out, " of type 1 and 2 in a file.\n");
|
||||
fprintf(out, " MIME-TYPE is mandatory; for best compatibility with players, use\n");
|
||||
fprintf(out, " pictures with MIME type image/jpeg or image/png. The MIME type can\n");
|
||||
fprintf(out, " also be --> to mean that FILE is actually a URL to an image, though\n");
|
||||
fprintf(out, " this use is discouraged.\n");
|
||||
fprintf(out, " DESCRIPTION is optional; the default is an empty string\n");
|
||||
fprintf(out, " The next part specfies the resolution and color information. If\n");
|
||||
fprintf(out, " the MIME-TYPE is image/jpeg, image/png, or image/gif, you can\n");
|
||||
fprintf(out, " usually leave this empty and they can be detected from the file.\n");
|
||||
fprintf(out, " Otherwise, you must specify the width in pixels, height in pixels,\n");
|
||||
fprintf(out, " and color depth in bits-per-pixel. If the image has indexed colors\n");
|
||||
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, "--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");
|
||||
|
||||
Reference in New Issue
Block a user