add --export-picture-to option to metaflac
authorJosh Coalson <jcoalson@users.sourceforce.net>
Thu, 2 Nov 2006 03:40:44 +0000 (03:40 +0000)
committerJosh Coalson <jcoalson@users.sourceforce.net>
Thu, 2 Nov 2006 03:40:44 +0000 (03:40 +0000)
doc/html/changelog.html
doc/html/documentation.html
man/metaflac.sgml
src/metaflac/operations.c
src/metaflac/operations_shorthand_picture.c
src/metaflac/options.c
src/metaflac/options.h
src/metaflac/usage.c
test/test_metaflac.sh

index 3aedd12..9551127 100644 (file)
@@ -70,7 +70,7 @@
                                        <li>Better multichannel support</li>
                                        <li>Large file (&gt;2GB) support everywhere</li>
                                        <li><span class="commandname">flac</span> now supports FLAC as input to the encoder (i.e. can re-encode FLAC to FLAC) and preserve all the metadata like tags, etc.</li>
-                                       <li>New <a href="format.html#def_PICTURE"><span class="code">PICTURE</span></a> metadata block for storing things like cover art, new <span class="argument"><a href="documentation.html#flac_options_picture">--picture</a></span> option to <span class="commandname">flac</span> and <span class="argument"><a href="documentation.html#metaflac_shorthand_import_picture">--import-picture</a></span> option to <span class="commandname">metaflac</span> for importing pictures, and metadata API <a href="api/group__flac__metadata__level0.html#ga3">additions</a> for searching for suitable pictures based on type, size and color constraints.</li>
+                                       <li>New <a href="format.html#def_PICTURE"><span class="code">PICTURE</span></a> metadata block for storing things like cover art, new <span class="argument"><a href="documentation.html#flac_options_picture">--picture</a></span> option to <span class="commandname">flac</span> and <span class="argument"><a href="documentation.html#metaflac_shorthand_import_picture">--import-picture</a></span> option to <span class="commandname">metaflac</span> for importing pictures, new <a href="documentation.html#metaflac_shorthand_export_picture_to">--export-picture-to</a></span> option to <span class="commandname">metaflac</span> for exporting pictures, and metadata API <a href="api/group__flac__metadata__level0.html#ga3">additions</a> for searching for suitable pictures based on type, size and color constraints.</li>
                                        <li>Support for new <tt>REPLAYGAIN_REFERENCE_LOUDNESS</tt> tag.</li>
                                        <li>In the developer libraries, the interface has been simplfied by merging the three decoding layers into a single class; ditto for the encoders.  Also, libOggFLAC has been merged into libFLAC and libOggFLAC++ has been merged into libFLAC++ so there is a single API supporting both native FLAC and Ogg FLAC.</li>
                                </ul>
                                <ul>
                                        <li>Added a new option <a href="documentation.html#metaflac_shorthand_set_tag_from_file"><span class="argument">--set-tag-from-file</span></a> for setting a tag from file (e.g. for importing a cuesheet as a tag).</li>
                                        <li>Added a new option <span class="argument"><a href="documentation.html#metaflac_shorthand_import_picture">--import-picture</a></span> for importing pictures.</li>
+                                       <li>Added a new option <span class="argument"><a href="documentation.html#metaflac_shorthand_export_picture_to">--export-picture-to</a></span> for exporting pictures.</li>
                                        <li>Added shorthand operation <a href="documentation.html#metaflac_shorthand_remove_replay_gain"><span class="argument">--remove-replay-gain</span></a> for removing ReplayGain tags.</li>
                                        <li><a href="documentation.html#metaflac_shorthand_export_cuesheet_to"><span class="argument">--export-cuesheet-to</span></a> now properly specifies the FLAC file name (<a href="https://sourceforge.net/tracker/index.php?func=detail&amp;aid=1272825&amp;group_id=13478&amp;atid=363478">SF #1272825</a>).</li>
                                        <li>Importing of non-CDDA-compliant cuesheets now issues a warning.</li>
index 52dc5c2..1d6ef23 100644 (file)
                                        <span class="argument">--import-tags-from=FILE</span>
                                </td>
                                <td>
-                                       Import tags from a file.  Use '-' for stdin.  Each line should be of the form <span class="argument">NAME=VALUE</span>.  Multi-line comments are currently not supported.  Specify <span class="argument">--remove-all-tags</span> and/or <a href="#metaflac_options_no_utf8_convert"><span class="argument">--no-utf8-convert</span></a> before <span class="argument">--import-tags-from</span> if necessary.
+                                       Import tags from a file.  Use <span class="argument">-</span> for stdin.  Each line should be of the form <span class="argument">NAME=VALUE</span>.  Multi-line comments are currently not supported.  Specify <span class="argument">--remove-all-tags</span> and/or <a href="#metaflac_options_no_utf8_convert"><span class="argument">--no-utf8-convert</span></a> before <span class="argument">--import-tags-from</span> if necessary.
                                </td>
                        </tr>
                        <tr>
                                        <span class="argument">--export-tags-to=FILE</span>
                                </td>
                                <td>
-                                       Export tags to a file.  Use '-' for stdin.  Each line will be of the form <span class="argument">NAME=VALUE</span>.  Specify <a href="#metaflac_options_no_utf8_convert"><span class="argument">--no-utf8-convert</span></a> if necessary.
+                                       Export tags to a file.  Use <span class="argument">-</span> for stdin.  Each line will be of the form <span class="argument">NAME=VALUE</span>.  Specify <a href="#metaflac_options_no_utf8_convert"><span class="argument">--no-utf8-convert</span></a> if necessary.
                                </td>
                        </tr>
                        <tr>
                                        <span class="argument">--import-cuesheet-from=FILE</span>
                                </td>
                                <td>
-                                       Import a cuesheet from a file.  Use '-' for stdin.  Only one FLAC file may be specified.  A seekpoint will be added for each index point in the cuesheet to the <span class="code">SEEKTABLE</span> unless <span class="argument">--no-cued-seekpoints</span> is specified.
+                                       Import a cuesheet from a file.  Use <span class="argument">-</span> for stdin.  Only one FLAC file may be specified.  A seekpoint will be added for each index point in the cuesheet to the <span class="code">SEEKTABLE</span> unless <span class="argument">--no-cued-seekpoints</span> is specified.
                                </td>
                        </tr>
                        <tr>
                                        <span class="argument">--export-cuesheet-to=FILE</span>
                                </td>
                                <td>
-                                       Export <span class="code">CUESHEET</span> block to a cuesheet file, suitable for use by CD authoring software.  Use '-' for stdout.  Only one FLAC file may be specified on the command line.
+                                       Export <span class="code">CUESHEET</span> block to a cuesheet file, suitable for use by CD authoring software.  Use <span class="argument">-</span> for stdout.  Only one FLAC file may be specified on the command line.
                                </td>
                        </tr>
                        <tr>
                        </tr>
                        <tr>
                                <td nowrap="nowrap" align="right" valign="top" bgcolor="#F4F4CC">
+                                       <a name="metaflac_shorthand_export_picture_to" />
+                                       <span class="argument">--export-picture-to=FILE</span>
+                               </td>
+                               <td>
+                                       Export <span class="code">PICTURE</span> block to a file.  Use <span class="argument">-</span> for stdout.  Only one FLAC file may be specified on the command line.  The first <span class="code">PICTURE</span> block will be exported unless <span class="argument">--export-picture-to</span> is preceded by a <span class="argument">--block-number=#</span> option to specify the exact metadata block to extract.  Note that the block number is the one shown by --list.
+                               </td>
+                       </tr>
+                       <tr>
+                               <td nowrap="nowrap" align="right" valign="top" bgcolor="#F4F4CC">
                                        <a name="metaflac_shorthand_add_replay_gain" />
                                        <span class="argument">--add-replay-gain</span>
                                </td>
index 480ede7..2fbca4b 100644 (file)
@@ -343,6 +343,14 @@ manpage.1: manpage.sgml
        </listitem>
       </varlistentry>
       <varlistentry>
+       <term><option>--export-picture-to=file</option></term>
+        <listitem>
+         <para>
+           Export PICTURE block to a file.  Use '-' for stdout.  Only one FLAC file may be specified on the command line.  The first PICTURE block will be exported unless --export-picture-to is preceded by a --block-number=# option to specify the exact metadata block to extract.  Note that the block number is the one shown by --list.
+         </para>
+        </listitem>
+      </varlistentry>
+      <varlistentry>
        <term><option>--add-replay-gain</option></term>
         <listitem>
          <para>
index de13383..764f47a 100644 (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:
index aefc187..8685cf5 100644 (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;
+}
index 6d42599..86b217c 100644 (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;
index a410e62..47bc0dd 100644 (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,
@@ -143,6 +144,11 @@ typedef struct {
 } 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;
index 3969422..ef83448 100644 (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");
index 592846f..050d155 100755 (executable)
@@ -351,6 +351,21 @@ for f in \
        ncase=`expr $ncase + 1`
 done
 [ $ncase = 60 ] || die "expected case# to be 60"
+
+fn=export-picture-check
+echo -n "Testing --export-picture-to... "
+run_metaflac --export-picture-to=$fn $flacfile
+check_flac
+cmp $fn pictures/0.gif || die "ERROR, exported picture file and original differ"
+echo OK
+rm -f $fn
+echo -n "Testing --block-number --export-picture-to... "
+run_metaflac --block-number=9 --export-picture-to=$fn $flacfile
+check_flac
+cmp $fn pictures/0.png || die "ERROR, exported picture file and original differ"
+echo OK
+rm -f $fn
+
 run_metaflac --remove --block-type=PICTURE $flacfile
 check_flac
 metaflac_test case60 "--remove --block-type=PICTURE" "--list"