) and substitution (C) operations, but note that braces within the expression must be balanced. The example below prints the camera Make with spaces translated to underlines, and multiple consecutive underlines replaced by a single underline: exiftool -p '${make;tr/ /_/;s/__+/_/g}' image.jpg A default expression of CE\0)()d> is assumed if the expression is empty, and removes the characters / \ ? * : | E E and null from the printed value. =item B<-php> Format output as a PHP Array. The B<-g>, B<-G>, B<-D>, B<-H>, B<-l>, B<-sep> and B<-struct> options combine with B<-php> in the same way as with B<-json>. As well, the B<-b> option may be added to output binary data. Here is a simple example showing how this could be used in a PHP script: =item B<-s>[I] (B<-short>) Short output format. Prints tag names instead of descriptions. Add I or up to 3 B<-s> options for even shorter formats: -s1 or -s - print tag names instead of descriptions -s2 or -s -s - no extra spaces to column-align values -s3 or -s -s -s - print values only (no tag names) Also effective when combined with B<-t>, B<-h>, B<-X> or B<-listx> options. =item B<-S> (B<-veryShort>) Very short format. The same as B<-s2> (or two B<-s> options). Tag names are printed instead of descriptions, and no extra spaces are added to column-align values. =item B<-sep> I (B<-separator>) Specify separator string for items in list-type tags. When reading, the default is to join list items with ", ". When writing, this option causes values assigned to list-type tags to be split into individual items at each substring matching I (otherwise they are not split by default). Space characters in I match zero or more whitespace characters in the value. =item B<-sort> Sort output by tag description, or by tag name if the B<-s> option is used. Tags are sorted within each group when combined with the B<-g> or B<-G> option. When sorting by description, the sort order will depend on the B<-lang> option setting. Without the B<-sort> option, tags appear in the order they were specified on the command line, or if not specified, the order they were extracted from the file. =item B<-struct>, B<--struct> Output structured XMP information instead of flattening to individual tags. This option works well when combined with the XML (B<-X>) and JSON (B<-j>) output formats. For other output formats, the structures are serialized into the same format as when writing structured information (see L for details). When copying, structured tags are copied by default unless B<--struct> is used to disable this feature (although flattened tags may still be copied by specifying them individually unless B<-struct> is used). These options have no effect when assigning new values since both flattened and structured tags may always be used when writing. =item B<-t> (B<-tab>) Output a tab-delimited list of description/values (useful for database import). May be combined with B<-s> to print tag names instead of descriptions, or B<-S> to print tag values only, tab-delimited on a single line. The B<-t> option may also be used to add tag table information to the B<-X> option output. =item B<-T> (B<-table>) Output tag values in table form. Equivalent to B<-t -S -q -f>. =item B<-v>[I] (B<-verbose>) Print verbose messages. I specifies the level of verbosity in the range 0-5, with higher numbers being more verbose. If I is not given, then each B<-v> option increases the level of verbosity by 1. With any level greater than 0, most other options are ignored and normal console output is suppressed unless specific tags are extracted. Using B<-v0> causes the console output buffer to be flushed after each line (which may be useful to avoid delays when piping exiftool output), and prints the name of each processed file when writing. Also see the B<-progress> option. =item B<-w>[+|!] I or I (B<-textOut>) Write console output to files with names ending in I, one for each source file. The output file name is obtained by replacing the source file extension (including the '.') with the specified extension (and a '.' is added to the start of I if it doesn't already contain one). Alternatively, a I string may be used to give more control over the output file name and directory. In the format string, %d, %f and %e represent the directory, filename and extension of the source file, and %c represents a copy number which is automatically incremented if the file already exists. %d includes the trailing '/' if necessary, but %e does not include the leading '.'. For example: -w %d%f.txt # same effect as "-w txt" -w dir/%f_%e.out # write files to "dir" as "FILE_EXT.out" -w dir2/%d%f.txt # write to "dir2", keeping dir structure -w a%c.txt # write to "a.txt" or "a1.txt" or "a2.txt"... Existing files will not be overwritten unless an exclamation point is added to the option name (ie. B<-w!> or B<-textOut!>), or a plus sign to append to the existing file (ie. B<-w+> or B<-textOut+>). Both may be used (ie. B<-w+!> or B<-textOut+!>) to overwrite output files that didn't exist before the command was run, and append the output from multiple source files. For example, to write one output file for all source files in each directory: exiftool -filename -createdate -T -w+! %d/out.txt -r DIR Notes: 1) In a Windows BAT file the C<%> character is represented by C<%%>, so an argument like C<%d%f.txt> is written as C<%%d%%f.txt>. 2) If the argument for B<-w> does not contain a format code (%d, %f or %e), then it is interpreted as a file extension. Therefore it is not possible to specify a simple filename as an argument, so creating a single output file from multiple source files is typically done by shell redirection, ie) exiftool FILE1 FILE2 ... > out.txt But if necessary, an empty format code may be used to force the argument to be interpreted as a format string, and the same result may be obtained without the use of shell redirection: exiftool -w+! %0fout.txt FILE1 FILE2 ... Advanced features: A substring of the original file name, directory or extension may be taken by specifying a field width immediately following the '%' character. If the width is negative, the substring is taken from the end. The substring position (characters to ignore at the start or end of the string) may be given by a second optional value after a decimal point. For example: Input File Name Format Specifier Output File Name ---------------- ---------------- ---------------- Picture-123.jpg %7f.txt Picture.txt Picture-123.jpg %-.4f.out Picture.out Picture-123.jpg %7f.%-3f Picture.123 Picture-123a.jpg Meta%-3.1f.txt Meta123.txt For %d, the field width/position specifiers may be applied to the directory levels instead of substring position by using a colon instead of a decimal point in the format specifier. For example: Source Dir Format Result Notes ------------ ------ ---------- ------------------ pics/2012/02 %2:d pics/2012/ take top 2 levels pics/2012/02 %-:1d pics/2012/ up one directory level pics/2012/02 %:1d 2012/02/ ignore top level pics/2012/02 %1:1d 2012/ take 1 level after top /Users/phil %:2d phil/ ignore top 2 levels (Note that the root directory counts as one level when an absolute path is used as in the last example above.) For %c, these modifiers have a different effects. If a field width is given, the copy number is padded with zeros to the specified width. A leading '-' adds a dash before the copy number, and a '+' adds an underline. By default, a copy number of zero is omitted, but this can be changed by adding a decimal point to the modifier. For example: -w A%-cZ.txt # AZ.txt, A-1Z.txt, A-2Z.txt ... -w B%5c.txt # B.txt, B00001.txt, B00002.txt ... -w C%.c.txt # C0.txt, C1.txt, C2.txt ... -w D%-.c.txt # D-0.txt, D-1.txt, D-2.txt ... -w E%-.4c.txt # E-0000.txt, E-0001.txt, E-0002.txt ... -w F%-.4nc.txt # F-0001.txt, F-0002.txt, F-0003.txt ... -w G%+c.txt # G.txt, G_1.txt G_2.txt ... -w H%-lc.txt # H.txt, H-b.txt, H-c.txt ... A special feature allows the copy number to be incremented for each processed file by using %C (upper case) instead of %c. This allows a sequential number to be added to output file names, even if the names are different. For %C, a copy number of zero is not omitted as it is with %c. The number before the decimal place gives the starting index, the number after the decimal place gives the field width. The following examples show the output filenames when used with the command C: -w %C%f.txt # 0rose.txt, 1star.txt, 2jet.txt -w %f-%10C.txt # rose-10.txt, star-11.txt, jet-12.txt -w %.3C-%f.txt # 000-rose.txt, 001-star.txt, 002-jet.txt -w %57.4C%f.txt # 0057rose.txt, 0058star.txt, 0059jet.txt All format codes may be modified by 'l' or 'u' to specify lower or upper case respectively (ie. C<%le> for a lower case file extension). When used to modify %c or %C, the numbers are changed to an alphabetical base (see example H above). Also, %c may be modified by 'n' to count using natural numbers starting from 1, instead of 0 (see example F above). This same I syntax is used with the B<-o> and B<-tagsFromFile> options, although %c is only valid for output file names. =item B<-W>[!|+] I (B<-tagOut>) This enhanced version of the B<-w> option allows a separate output file to be created for each extracted tag. The differences between B<-W> and B<-w> are listed below: 1) With B<-W>, a new output file is created for each extracted tag. 2) B<-W> supports three additional format codes: %t, %g and %s represent the tag name, group name, and suggested extension for the output file (based on the format of the data). The %g code may be followed by a single digit to specify the group family number (ie. %g1), otherwise family 0 is assumed. The substring width/position/case specifiers may be used with these format codes in exactly the same way as with %f and %e. 3) The argument for B<-W> is interpreted as a file name if it contains no format codes. (For B<-w>, this would be a file extension.) This change allows a simple file name to be specified, which, when combined with the append feature, provides a method to write metadata from multiple source files to a single output file without the need for shell redirection. 4) Adding the B<-v> option to B<-W> generates a list of the tags and output file names instead of giving a verbose dump of the entire file. (Unless appending all output to one file for each source file by using B<-W+> and an output file I that does not contain %t, $g or %s.) 5) Individual list items are stored in separate files when B<-W> is combined with B<-b>, but note that for separate files to be created %c must be used in I to give the files unique names. =item B<-Wext> I, B<--Wext> I (B<-tagOutExt>) This option is used to specify the type of output file(s) written by the B<-W> option. An output file is written only if the suggested extension matches I. Multiple B<-Wext> options may be used to write more than one type of file. Use B<--Wext> to write all but the specified type(s). =item B<-X> (B<-xmlFormat>) Use ExifTool-specific RDF/XML formatting for console output. Implies the B<-a> option, so duplicate tags are extracted. The formatting options B<-b>, B<-D>, B<-H>, B<-l>, B<-s>, B<-sep>, B<-struct> and B<-t> may be used in combination with B<-X> to affect the output, but note that the tag ID (B<-D>, B<-H> and B<-t>), binary data (B<-b>) and structured output (B<-struct>) options are not effective for the short output (B<-s>). Another restriction of B<-s> is that only one tag with a given group and name may appear in the output. Note that the tag ID options (B<-D>, B<-H> and B<-t>) will produce non-standard RDF/XML unless the B<-l> option is also used. By default, list-type tags with multiple values are formatted as an RDF Bag, but they are combined into a single string when B<-s> or B<-sep> is used. Using B<-L> changes the XML encoding from "UTF-8" to "windows-1252". Other B<-charset> settings change the encoding only if there is a corresponding standard XML character set. The B<-b> option causes binary data values to be written, encoded in base64 if necessary. The B<-t> option adds tag table information to the output (table C, decimal tag C, and C for cases where multiple conditional tags exist with the same ID). Note: This output is NOT the same as XMP because it uses dynamically-generated property names corresponding to the ExifTool tag names, and not the standard XMP properties. To write XMP instead, use the B<-o> option with an XMP extension for the output file. =back =head3 Processing control =over 5 =item B<-a>, B<--a> (B<-duplicates>, B<--duplicates>) Allow (B<-a>) or suppress (B<--a>) duplicate tag names to be extracted. By default, duplicate tags are suppressed unless the B<-ee> or B<-X> options are used or the Duplicates option is enabled in the configuration file. =item B<-e> (B<--composite>) Extract existing tags only -- don't calculate composite tags. =item B<-ee> (B<-extractEmbedded>) Extract information from embedded documents in EPS files, embedded EPS information and JPEG and Jpeg2000 images in PDF files, embedded MPF images in JPEG and MPO files, streaming metadata in AVCHD videos, and the resource fork of Mac OS files. Implies the B<-a> option. Use B<-g3> or B<-G3> to identify the originating document for extracted information. Embedded documents containing sub-documents are indicated with dashes in the family 3 group name. (ie. C is the 3rd sub-document of the 2nd embedded document.) Note that this option may increase processing time substantially, especially for PDF files with many embedded images. =item B<-ext> I, B<--ext> I (B<-extension>) Process only files with (B<-ext>) or without (B<--ext>) a specified extension. There may be multiple B<-ext> and B<--ext> options. Extensions may begin with a leading '.', and case is not significant. For example: exiftool -ext .JPG DIR # process only JPG files exiftool --ext cr2 --ext dng DIR # supported files but CR2/DNG exiftool --ext . DIR # ignore if no extension exiftool -ext "*" DIR # process all files exiftool -ext "*" --ext xml DIR # process all but XML files The extension may be C<"*"> as in the last two examples above to force processing files with any extension (not just supported files). Using this option has two main advantages over specifying C<*.I> on the command line: 1) It applies to files in subdirectories when combined with the B<-r> option. 2) The B<-ext> option is case-insensitive, which is useful when processing files on case-sensitive filesystems. =item B<-F>[I] (B<-fixBase>) Fix the base for maker notes offsets. A common problem with some image editors is that offsets in the maker notes are not adjusted properly when the file is modified. This may cause the wrong values to be extracted for some maker note entries when reading the edited file. This option allows an integer I to be specified for adjusting the maker notes base offset. If no I is given, ExifTool takes its best guess at the correct base. Note that exiftool will automatically fix the offsets for images which store original offset information (ie. newer Canon models). Offsets are fixed permanently if B<-F> is used when writing EXIF to an image. ie) exiftool -F -exif:resolutionunit=inches image.jpg =item B<-fast>[I] Increase speed of extracting information from JPEG images. With this option, ExifTool will not scan to the end of a JPEG image to check for an AFCP or PreviewImage trailer, or past the first comment in GIF images or the audio/video data in WAV/AVI files to search for additional metadata. These speed benefits are small when reading images directly from disk, but can be substantial if piping images through a network connection. For more substantial speed benefits, B<-fast2> also causes exiftool to avoid extracting any EXIF MakerNote information. =item B<-fileOrder> [-]I Set file processing order according to the sorted value of the specified I. For example, to process files in order of date: exiftool -fileOrder DateTimeOriginal DIR Additional B<-fileOrder> options may be added for secondary sort keys. Floating point values are sorted numerically, and all other values are sorted alphabetically. The sort order may be reversed by prefixing the tag name with a C<-> (ie. C<-fileOrder -createdate>). A C<#> may be appended to the tag name to disable print conversion for the sorted values. Note that the B<-fileOrder> option has a large performance impact since it involves an additional processing pass of each file. =item B<-i> I (B<-ignore>) Ignore specified directory name. Use multiple B<-i> options to ignore more than one directory name. A special I value of C (case sensitive) may be specified to ignore symbolic links when the B<-r> option is used. =item B<-if> I Specify a condition to be evaluated before processing each I. I is a Perl-like expression containing tag names prefixed by C<$> symbols. It is evaluated with the tags from each I in turn, and the file is processed only if the expression returns true. Unlike Perl variable names, tag names are not case sensitive and may contain a hyphen. As well, tag names may have a leading group name separated by a colon, and/or a trailing C<#> character to disable print conversion. The expression C<$GROUP:all> evaluates to 1 if any tag exists in the specified C, or 0 otherwise (see note 2 below). When multiple B<-if> options are used, all conditions must be satisfied to process the file. Returns an exit status of 1 if all files fail the condition. Below are a few examples: # extract shutterspeed from all Canon images in a directory exiftool -shutterspeed -if '$make eq "Canon"' dir # add one hour to all images created on or after Apr. 2, 2006 exiftool -alldates+=1 -if '$CreateDate ge "2006:04:02"' dir # set EXIF ISO value if possible, unless it is set already exiftool '-exif:iso and B<-b> options also apply to tags used in I. 2) Some binary data blocks are not extracted unless specified explicitly. These tags are not available for use in the B<-if> condition unless they are also specified on the command line. The alternative is to use the C<$GROUP:all> syntax. (ie. Use C<$exif:all> instead of C<$exif> in I to test for the existence of EXIF tags.) 3) Tags in the string are interpolated the same way as with B<-p> before the expression is evaluated. In this interpolation, C<$/> is converted to a newline and C<$$> represents a single C<$> symbol (so Perl variables, if used, require a double C<$>). 4) The condition may only test tags from the file being processed. To process one file based on tags from another, two steps are required. For example, to process XMP sidecar files in directory C based on tags from the associated NEF: exiftool -if EXPR -p '$directory/$filename' -ext nef DIR > nef.txt exiftool -@ nef.txt -srcfile %d%f.xmp ... =item B<-m> (B<-ignoreMinorErrors>) Ignore minor errors and warnings. This enables writing to files with minor errors and disables some validation checks which could result in minor warnings. Generally, minor errors/warnings indicate a problem which usually won't result in loss of metadata if ignored. However, there are exceptions, so ExifTool leaves it up to you to make the final decision. Minor errors and warnings are indicated by "[minor]" at the start of the message. Warnings which affect processing when ignored are indicated by "[Minor]" (with a capital "M"). =item B<-o> I or I (B<-out>) Set the output file or directory name when writing information. Without this option, when any "real" tags are written the original file is renamed to C and output is written to I. When writing only FileName and/or Directory "pseudo" tags, B<-o> causes the file to be copied instead of moved, but directories specified for either of these tags take precedence over that specified by the B<-o> option. I may be C<-> to write to stdout. The output file name may also be specified using a I string in which %d, %f and %e represent the directory, file name and extension of I. Also, %c may be used to add a copy number. See the B<-w> option for I string examples. The output file is taken to be a directory name if it already exists as a directory or if the name ends with '/'. Output directories are created if necessary. Existing files will not be overwritten. Combining the B<-overwrite_original> option with B<-o> causes the original source file to be erased after the output file is successfully written. A special feature of this option allows the creation of certain types of files from scratch, or with the metadata from another type of file. The following file types may be created using this technique: XMP, ICC/ICM, MIE, VRD, EXIF The output file type is determined by the extension of I (specified as C<-.EXT> when writing to stdout). The output file is then created from a combination of information in I (as if the B<-tagsFromFile> option was used), and tag values assigned on the command line. If no I is specified, the output file may be created from scratch using only tags assigned on the command line. =item B<-overwrite_original> Overwrite the original I (instead of preserving it by adding C<_original> to the file name) when writing information to an image. Caution: This option should only be used if you already have separate backup copies of your image files. The overwrite is implemented by renaming a temporary file to replace the original. This deletes the original file and replaces it with the edited version in a single operation. When combined with B<-o>, this option causes the original file to be deleted if the output file was successfully written (ie. the file is moved instead of copied). =item B<-overwrite_original_in_place> Similar to B<-overwrite_original> except that an extra step is added to allow the original file attributes to be preserved. For example, on a Mac this causes the original file creation date, type, creator, label color, icon and hard links to the file to be preserved (but note that the Mac OS resource fork is always preserved unless specifically deleted with C<-rsrc:all=>). This is implemented by opening the original file in update mode and replacing its data with a copy of a temporary file before deleting the temporary. The extra step results in slower performance, so the B<-overwrite_original> option should be used instead unless necessary. =item B<-P> (B<-preserve>) Preserve the filesystem modification date/time of the original file (C) when writing. Note that some filesystems store a creation date which is not preserved by this option, with the exception of Windows systems where Win32API::File::Time is available. For other systems, the B<-overwrite_original_in_place> option may be used if necessary to preserve the creation date. =item B<-password> I Specify password to allow processing of password-protected PDF documents. If a password is required but not given, a warning is issued and the document is not processed. Ignored if a password is not required. =item B<-progress> Show file progress count in messages. The progress count appears in brackets after then name of each processed file, and gives the current file number and the total number of files to be processed. Implies the B<-v0> option, which prints the name of each processed file when writing. When combined with the [tt]-if[/tt] option, the total count includes all files before the condition is applied, but files that fail the condition will not have their names printed. =item B<-q> (B<-quiet>) Quiet processing. One B<-q> suppresses normal informational messages, and a second B<-q> suppresses warnings as well. Error messages can not be suppressed, although minor errors may be downgraded to warnings with the B<-m> option. =item B<-r> (B<-recurse>) Recursively process files in subdirectories. Only meaningful if I is a directory name. By default, exiftool will also follow symbolic links to directories if supported by the system, but this may be disabled with C<-i SYMLINKS> (see the B<-i> option for details). =item B<-scanForXMP> Scan all files (even unsupported formats) for XMP information unless found already. When combined with the B<-fast> option, only unsupported file types are scanned. Warning: It can be time consuming to scan large files. =item B<-u> (B<-unknown>) Extract values of unknown tags. Add another B<-u> to also extract unknown information from binary data blocks. This option applies to tags with numerical tag ID's, and causes tag names like "Exif_0xc5d9" to be generated for unknown information. It has no effect on information types which have human-readable tag ID's (such as XMP), since unknown tags are extracted automatically from these formats. =item B<-U> (B<-unknown2>) Extract values of unknown tags as well as unknown information from some binary data blocks. This is the same as two B<-u> options. =item B<-wm> I (B<-writeMode>) Set mode for writing/creating tags. I is a string of one or more characters from the list below. Write mode is C unless otherwise specified. w - Write existing tags c - Create new tags g - create new Groups as necessary The level of the group is the SubDirectory level in the metadata structure. For XMP or IPTC this is the full XMP/IPTC block (the family 0 group), but for EXIF this is the individual IFD (the family 1 group). =item B<-z> (B<-zip>) When reading, causes information to be extracted from .gz and .bz2 compressed images. (Only one image per archive. Requires gzip and bzip2 to be installed on the system.) When writing, causes compressed information to be written if supported by the image format. (ie. The PNG format supports compressed text.) This option also disables the recommended padding in embedded XMP, saving 2424 bytes when writing XMP in a file. =back =head3 Special features =over 5 =item B<-geotag> I Geotag images from the specified GPS track log file. Using the B<-geotag> option is equivalent to writing a value to the C tag. After the B<-geotag> option has been specified, the value of the C tag is written to define a date/time for the position interpolation. If C is not specified, the value is copied from C. For example, the following two commands are equivalent: exiftool -geotag track.log image.jpg exiftool -geotag "-Geotime value is converted to UTC, the local system timezone is assumed unless the date/time value contains a timezone. Writing C causes the following tags to be written (provided they can be calculated from the track log, and they are supported by the destination metadata format): GPSLatitude, GPSLatitudeRef, GPSLongitude, GPSLongitudeRef, GPSAltitude, GPSAltitudeRef, GPSDateStamp, GPSTimeStamp, GPSDateTime, GPSTrack, GPSTrackRef, GPSSpeed, GPSSpeedRef, GPSImgDirection, GPSImgDirectionRef, GPSPitch and GPSRoll. By default, tags are created in EXIF, and updated in XMP only if they already exist. However, C or C may be specified to write only EXIF or XMP tags respectively. Note that GPSPitch and GPSRoll are non-standard, and require user-defined tags in order to be written. The C tag may be used to specify a time correction which is applied to each C value for synchronization with GPS time. For example, the following command compensates for image times which are 1 minute and 20 seconds behind GPS: exiftool -geosync=+1:20 -geotag a.log DIR C must be set before C (if specified) to be effective. Advanced C features allow a linear time drift correction and synchronization from previously geotagged images. See "geotag.html" in the full ExifTool distribution for more information. Multiple B<-geotag> options may be used to concatinate GPS track log data. Also, a single B<-geotag> option may be used to load multiple track log files by using wildcards in the I name, but note that in this case I must be quoted on most systems (with the notable exception of Windows) to prevent filename expansion. For example: exiftool -geotag "TRACKDIR/*.log" IMAGEDIR Currently supported track file formats are GPX, NMEA RMC/GGA/GLL, KML, IGC, Garmin XML and TCX, Magellan PMGNTRK, Honeywell PTNTHPR, and Winplus Beacon text files. See L for examples. Also see "geotag.html" in the full ExifTool distribution and the L for more details and for information about geotag configuration options. =item B<-globalTimeShift> I Shift all formatted date/time values by the specified amount when reading. Does not apply to unformatted (B<-n>) output. I takes the same form as the date/time shift when writing (see L for details), with a negative shift being indicated with a minus sign (C<->) at the start of the I string. For example: # return all date/times, shifted back by 1 hour exiftool -globalTimeShift -1 -time:all a.jpg # set the file name from the shifted CreateDate (-1 day) for # all images in a directory exiftool "-filename I Add features from specified plug-in I. Currently, the MWG module is the only plug-in module distributed with exiftool. This module adds read/write support for tags as recommended by the Metadata Working Group. To save typing, C<-use MWG> is assumed if the C group is specified for any tag on the command line. See the L for more details. (Note that this option is not reversible, and remains in effect until the application terminates, even across the C<-execute> option.) =back =head3 Utilities =over 5 =item B<-restore_original> =item B<-delete_original>[!] These utility options automate the maintenance of the C<_original> files created by exiftool. They have no effect on files without an C<_original> copy. The B<-restore_original> option restores the specified files from their original copies by renaming the C<_original> files to replace the edited versions. For example, the following command restores the originals of all JPG images in directory C: exiftool -restore_original -ext jpg DIR The B<-delete_original> option deletes the C<_original> copies of all files specified on the command line. Without a trailing C this option prompts for confirmation before continuing. For example, the following command deletes C if it exists, after asking "Are you sure?": exiftool -delete_original a.jpg These options may not be used with other options to read or write tag values in the same command, but may be combined with options such B<-ext>, B<-if>, B<-r>, B<-q> and B<-v>. =back =head3 Other options =over 5 =item B<-@> I Read command-line arguments from the specified file. The file contains one argument per line (NOT one option per line -- some options require additional arguments, and all arguments must be placed on separate lines). Blank lines and lines beginning with C<#> and are ignored. Normal shell processing of arguments is not performed, which among other things means that arguments should not be quoted and spaces are treated as any other character. I may exist relative to either the current directory or the exiftool directory unless an absolute pathname is given. For example, the following I will set the value of Copyright to "Copyright YYYY, Phil Harvey", where "YYYY" is the year of CreateDate: -d %Y -copyright (B<-pause>) Pause with the message C<-- press any key --> or C<-- press RETURN --> (depending on your system) before terminating. This option is used to prevent the command window from closing when run as a Windows drag and drop application. =item B<-list>, B<-listw>, B<-listf>, B<-listr>, B<-listwf>, B<-listg>[I], B<-listd>, B<-listx> Print a list of all valid tag names (B<-list>), all writable tag names (B<-listw>), all supported file extensions (B<-listff>), all recognized file extensions (B<-listr>), all writable file extensions (B<-listwf>), all tag groups [in a specified family] (B<-listg>[I]), all deletable tag groups (B<-listd>), or an XML database of tag details including language translations (B<-listx>). The B<-list>, B<-listw> and B<-listx> options may be followed by an additional argument of the form C<-GROUP:All> to list only tags in a specific group, where C is one or more family 0-2 group names (excepting EXIF IFD groups) separated by colons. With B<-listg>, I may be given to specify the group family, otherwise family 0 is assumed. Here are some examples: -list # list all tag names -list -EXIF:All # list all EXIF tags -list -xmp:time:all # list all XMP tags relating to time -listw -XMP-dc:All # list all writable XMP-dc tags -listf # list all supported file extensions -listr # list all recognized file extensions -listwf # list all writable file extensions -listg1 # list all groups in family 1 -listd # list all deletable groups -listx -EXIF:All # list database of EXIF tags in XML format -listx -XMP:All -s # list short XML database of XMP tags When combined with B<-listx>, the B<-s> option shortens the output by omitting the descriptions and values (as in the last example above), and B<-f> adds a 'flags' attribute if applicable. The flags are formatted as a comma-separated list of the following possible values: Avoid, Binary, List, Mandatory, Permanent, Protected, Unknown and Unsafe (see the L). For XMP List tags, the list type (Alt, Bag or Seq) is added to the flags, and flattened structure tags are indicated by a Flattened flag. Note that none of the B<-list> options require an input I. =item B<-ver> Print exiftool version number. =back =head3 Advanced options Among other things, the advanced options allow complex processing to be performed from a single command without the need for additional scripting. This may be particularly useful for implementations such as Windows drag-and-drop applications. These options may also be used to improve performance in multi-pass processing by reducing the overhead required to load exiftool for each invocation. =over 5 =item B<-common_args> Specifies that all arguments following this option are common to all executed commands when B<-execute> is used. This and the B<-config> option are the only options that may not be used inside a B<-@> I. Note that by definition this option and its arguments MUST come after all other options on the command line. =item B<-config> I Load specified configuration file instead of the default ".ExifTool_config". If used, this option must come before all other arguments on the command line. The I name may contain a directory specification (otherwise the file must exist in the current directory), or may be set to an empty string ("") to disable loading of the config file. See the sample configuration file and "config.html" in the full ExifTool distribution for more information about the ExifTool configuration file. =item B<-echo>[I] I Echo text to stdout (B<-echo> or B<-echo1>) or stderr (B<-echo2>). Text is output as the command line is parsed, before the processing of any input files. I may also be 3 or 4 to output text (to stdout or stderr respectively) after processing is complete. =item B<-execute>[I] Execute command for all arguments up to this point on the command line. Allows multiple commands to be executed from a single command line. I is an optional number that is echoed in the "{ready}" message when using the B<-stay_open> feature. =item B<-srcfile> I Specify a different source file to be processed based on the name of the original I. This may be useful in some special situations for processing related preview images or sidecar files. See the B<-w> option for a description of the I syntax. Note that file name I strings for all options are based on the original I specified from the command line, not the name of the source file specified by B<-srcfile>. If than one B<-srcfile> option is specified, the files are tested in order and the first existing source file is processed. If none of the source files already exist, then exiftool uses the first B<-srcfile> specified. A I of C<@> may be used to represent the original I, which may be useful when specifying multiple B<-srcfile> options (ie. to fall back to processing the original I if no sidecar exists). =item B<-stay_open> I If I is C<1> or C, causes exiftool keep reading from the B<-@> I even after reaching the end of file. This feature allows calling applications to pre-load exiftool, thus avoiding the overhead of loading exiftool for each command. The procedure is as follows: 1) Execute C>, where I is the name of an existing (possibly empty) argument file or C<-> to pipe arguments from the standard input. 2) Write exiftool command-line arguments to I, one argument per line (see the B<-@> option for details). 3) Write C<-execute\n> to I, where C<\n> represents a newline sequence. (Note: You may need to flush your write buffers here if using buffered output.) Exiftool will then execute the command with the arguments received up to this point, send a "{ready}" message to stdout when done (unless the B<-q> or B<-T> option is used), and continue trying to read arguments for the next command from I. To aid in command/response synchronization, any number appended to the C<-execute> option is echoed in the "{ready}" message. For example, C<-execute613> results in "{ready613}". 4) Repeat steps 2 and 3 for each command. 5) Write C<-stay_open\nFalse\n> to I when done. This will cause exiftool to process any remaining command-line arguments then exit normally. The input I may be changed at any time before step 5 above by writing the following lines to the currently open I: -stay_open True -@ NEWARGFILE This causes I to be closed, and I to be kept open. (Without the B<-stay_open> here, exiftool would have returned to reading arguments from I after reaching the end of I.) Note: When writing arguments to a disk file there is a delay of up to 0.01 seconds after writing C<-execute\n> before exiftool starts processing the command. This delay may be avoided by sending a CONT signal to the exiftool process immediately after writing C<-execute\n>. (There is no associated delay when writing arguments via a pipe with C<-@ ->, so the signal is not necessary when using this technique.) =back =head1 READING EXAMPLES B: Beware when cutting and pasting these examples into your terminal! Some characters such as single and double quotes and hyphens may have been changed into similar-looking yet functionally-different characters by the text formatter used to display this documentation. Also note that Windows users must use double quotes instead of single quotes as below around arguments containing special characters. =over 5 =item exiftool -a -u -g1 a.jpg Print all meta information in an image, including duplicate and unknown tags, sorted by group (for family 1). =item exiftool -common dir Print common meta information for all images in C. =item exiftool -T -createdate -aperture -shutterspeed -iso dir > out.txt List specified meta information in tab-delimited column form for all images in C to an output text file named "out.txt". =item exiftool -s -ImageSize -ExposureTime b.jpg Print ImageSize and ExposureTime tag names and values. =item exiftool -l -canon c.jpg d.jpg Print standard Canon information from two image files. =item exiftool -r -w .txt -common pictures Recursively extract common meta information from files in C directory, writing text output to C<.txt> files with the same names. =item exiftool -b -ThumbnailImage image.jpg > thumbnail.jpg Save thumbnail image from C to a file called C. =item exiftool -b -JpgFromRaw -w _JFR.JPG -ext NEF -r . Recursively extract JPG image from all Nikon NEF files in the current directory, adding C<_JFR.JPG> for the name of the output JPG files. =item exiftool -d '%r %a, %B %e, %Y' -DateTimeOriginal -S -s -ext jpg . Print formatted date/time for all JPG files in the current directory. =item exiftool -IFD1:XResolution -IFD1:YResolution image.jpg Extract image resolution from EXIF IFD1 information (thumbnail image IFD). =item exiftool '-*resolution*' image.jpg Extract all tags with names containing the word "Resolution" from an image. =item exiftool -xmp:author:all -a image.jpg Extract all author-related XMP information from an image. =item exiftool -xmp -b a.jpg > out.xmp Extract complete XMP data record intact from C and write it to C using the special C tag (see the Extra tags in L<Image::ExifTool::TagNames|Image::ExifTool::TagNames>). =item exiftool -p '$filename has date $dateTimeOriginal' -q -f dir Print one line of output containing the file name and DateTimeOriginal for each image in directory C<dir>. =item exiftool -ee -p '$gpslatitude, $gpslongitude, $gpstimestamp' a.m2ts Extract all GPS positions from an AVCHD video. =item exiftool -icc_profile -b -w icc image.jpg Save complete ICC_Profile from an image to an output file with the same name and an extension of C<.icc>. =item exiftool -htmldump -w tmp/%f_%e.html t/images Generate HTML pages from a hex dump of EXIF information in all images from the C<t/images> directory. The output HTML files are written to the C<tmp> directory (which is created if it didn't exist), with names of the form 'FILENAME_EXT.html'. =item exiftool -a -b -ee -embeddedimage -W Image_%.3g3.%s file.pdf Extract embedded JPG and JP2 images from a PDF file. The output images will have file names like "Image_#.jpg" or "Image_#.jp2", where "#" is the ExifTool family 3 embedded document number for the image. =back =head1 WRITING EXAMPLES Note that quotes are necessary around arguments which contain certain special characters such as C<E<gt>>, C<E<lt>> or any white space. These quoting techniques are shell dependent, but the examples below will work for most Unix shells. With the Windows cmd shell however, double quotes should be used (ie. -Comment=E<34>This is a new commentE<34>). =over 5 =item exiftool -Comment='This is a new comment' dst.jpg Write new comment to a JPG image (replaces any existing comment). =item exiftool -comment= -o newdir -ext jpg . Remove comment from all JPG images in the current directory, writing the modified images to a new directory. =item exiftool -keywords=EXIF -keywords=editor dst.jpg Replace existing keyword list with two new keywords (C<EXIF> and C<editor>). =item exiftool -Keywords+=word -o newfile.jpg src.jpg Copy a source image to a new file, and add a keyword (C<word>) to the current list of keywords. =item exiftool -exposurecompensation+=-0.5 a.jpg Decrement the value of ExposureCompensation by 0.5 EV. Note that += with a negative value is used for decrementing because the -= operator is used for conditional deletion (see next example). =item exiftool -credit-=xxx dir Delete Credit information from all files in a directory where the Credit value was (C<xxx>). =item exiftool -xmp:description-de='k&uuml;hl' -E dst.jpg Write alternate language for XMP:Description, using HTML character escaping to input special characters. =item exiftool -all= dst.jpg Delete all meta information from an image. Note: You should NOT do this to RAW images (except DNG) since proprietary RAW image formats often contain information in the makernotes that is necessary for converting the image. =item exiftool -all= -comment='lonely' dst.jpg Delete all meta information from an image and add a comment back in. (Note that the order is important: C<-comment='lonely' -all=> would also delete the new comment.) =item exiftool -all= --jfif:all dst.jpg Delete all meta information except JFIF group from an image. =item exiftool -Photoshop:All= dst.jpg Delete Photoshop meta information from an image (note that the Photoshop information also includes IPTC). =item exiftool -r -XMP-crss:all= DIR Recursively delete all XMP-crss information from images in a directory. =item exiftool '-ThumbnailImageE<lt>=thumb.jpg' dst.jpg Set the thumbnail image from specified file (Note: The quotes are necessary to prevent shell redirection). =item exiftool '-JpgFromRawE<lt>=%d%f_JFR.JPG' -ext NEF -r . Recursively write JPEG images with filenames ending in C<_JFR.JPG> to the JpgFromRaw tag of like-named files with extension C<.NEF> in the current directory. (This is the inverse of the C<-JpgFromRaw> command of the L</READING EXAMPLES> section above.) =item exiftool -DateTimeOriginal-='0:0:0 1:30:0' dir Adjust original date/time of all images in directory C<dir> by subtracting one hour and 30 minutes. (This is equivalent to C<-DateTimeOriginal-=1.5>. See L<Image::ExifTool::Shift.pl|Image::ExifTool::Shift.pl> for details.) =item exiftool -createdate+=3 -modifydate+=3 a.jpg b.jpg Add 3 hours to the CreateDate and ModifyDate timestamps of two images. =item exiftool -AllDates+=1:30 -if '$make eq E<34>CanonE<34>' dir Shift the values of DateTimeOriginal, CreateDate and ModifyDate forward by 1 hour and 30 minutes for all Canon images in a directory. (The AllDates tag is provided as a shortcut for these three tags, allowing them to be accessed via a single tag.) =item exiftool -xmp:city=Kingston image1.jpg image2.nef Write a tag to the XMP group of two images. (Without the C<xmp:> this tag would get written to the IPTC group since C<City> exists in both, and IPTC is preferred by default.) =item exiftool -LightSource-='Unknown (0)' dst.tiff Delete C<LightSource> tag only if it is unknown with a value of 0. =item exiftool -whitebalance-=auto -WhiteBalance=tung dst.jpg Set C<WhiteBalance> to C<Tungsten> only if it was previously C<Auto>. =item exiftool -comment-= -comment='new comment' a.jpg Write a new comment only if the image doesn't have one already. =item exiftool -o %d%f.xmp dir Create XMP meta information data files for all images in C<dir>. =item exiftool -o test.xmp -owner=Phil -title='XMP File' Create an XMP data file only from tags defined on the command line. =item exiftool '-ICC_Profile<=%d%f.icc' image.jpg Write ICC_Profile to an image from a C<.icc> file of the same name. =item exiftool -hierarchicalkeywords='{keyword=one,children={keyword=B}}' Write structured XMP information. See L<http://owl.phy.queensu.ca/~phil/exiftool/struct.html> for more details. =item exiftool -trailer:all= image.jpg Delete any trailer found after the end of image (EOI) in a JPEG file. A number of digital cameras store a large PreviewImage after the JPEG EOI, and the file size may be reduced significantly by deleting this trailer. See the L<JPEG Tags documentation|Image::ExifTool::TagNames/JPEG Tags> for a list of recognized JPEG trailers. =back =head1 COPYING EXAMPLES These examples demonstrate the ability to copy tag values between files. =over 5 =item exiftool -tagsFromFile src.cr2 dst.jpg Copy the values of all writable tags from C<src.cr2> to C<dst.jpg>, writing the information to same-named tags in the preferred groups. =item exiftool -TagsFromFile src.jpg -all:all dst.jpg Copy the values of all writable tags from C<src.jpg> to C<dst.jpg>, preserving the original tag groups. =item exiftool -all= -tagsfromfile src.jpg -exif:all dst.jpg Erase all meta information from C<dst.jpg> image, then copy EXIF tags from C<src.jpg>. =item exiftool -exif:all= -tagsfromfile @ -all:all -unsafe bad.jpg Rebuild all EXIF meta information from scratch in an image. This technique can be used in JPEG images to repair corrupted EXIF information which otherwise could not be written due to errors. The C<Unsafe> tag is a shortcut for unsafe EXIF tags in JPEG images which are not normally copied. See the L<tag name documentation|Image::ExifTool::TagNames> for more details about unsafe tags. =item exiftool -Tagsfromfile a.jpg out.xmp Copy meta information from C<a.jpg> to an XMP data file. If the XMP data file C<out.xmp> already exists, it will be updated with the new information. Otherwise the XMP data file will be created. Only XMP, ICC and MIE files may be created like this (other file types may be edited but not created). See L</WRITING EXAMPLES> above for another technique to generate XMP files. =item exiftool -tagsFromFile a.jpg -XMP:All= -ThumbnailImage= -m b.jpg Copy all meta information from C<a.jpg> to C<b.jpg>, deleting all XMP information and the thumbnail image from the destination. =item exiftool -TagsFromFile src.jpg -title -author=Phil dst.jpg Copy title from one image to another and set a new author name. =item exiftool -TagsFromFile a.jpg -ISO -TagsFromFile b.jpg -comment dst.jpg Copy ISO from one image and Comment from another image to a destination image. =item exiftool -tagsfromfile src.jpg -exif:all --subifd:all dst.jpg Copy only the EXIF information from one image to another, excluding SubIFD tags. =item exiftool '-FileModifyDateE<lt>DateTimeOriginal' dir Use the original date from the meta information to set the same file's filesystem modification date for all images in a directory. (Note that C<-TagsFromFile @> is assumed if no other B<-TagsFromFile> is specified when redirecting information as in this example.) =item exiftool -TagsFromFile src.jpg '-xmp:allE<lt>all' dst.jpg Copy all possible information from C<src.jpg> and write in XMP format to C<dst.jpg>. =item exiftool -@ iptc2xmp.args -iptc:all= a.jpg Translate IPTC information to XMP with appropriate tag name conversions, and delete the original IPTC information from an image. This example uses iptc2xmp.args, which is a file included with the ExifTool distribution that contains the required arguments to convert IPTC information to XMP format. Also included with the distribution are xmp2iptc.args (which performs the inverse conversion) and a few more .args files for other conversions between EXIF, IPTC and XMP. =item exiftool -tagsfromfile %d%f.CR2 -r -ext JPG dir Recursively rewrite all C<JPG> images in C<dir> with information copied from the corresponding C<CR2> images in the same directories. =item exiftool '-keywords+E<lt>make' image.jpg Add camera make to list of keywords. =item exiftool '-commentE<lt>ISO=$exif:iso Exposure=${shutterspeed}' dir Set the Comment tag of all images in C<dir> from the values of the EXIF:ISO and ShutterSpeed tags. The resulting comment will be in the form "ISO=100 Exposure=1/60". =item exiftool -TagsFromFile src.jpg -icc_profile dst.jpg Copy ICC_Profile from one image to another. =item exiftool -TagsFromFile src.jpg -all:all dst.mie Copy all meta information in its original form from a JPEG image to a MIE file. The MIE file will be created if it doesn't exist. This technique can be used to store the metadata of an image so it can be inserted back into the image (with the inverse command) later in a workflow. =item exiftool -o dst.mie -all:all src.jpg This command performs exactly the same task as the command above, except that the B<-o> option will not write to an output file that already exists. =item exiftool -if '$jpgfromraw' -b -jpgfromraw -w %d%f_%ue.jpg -execute -if '$previewimage' -b -previewimage -w %d%f_%ue.jpg -execute -tagsfromfile @ -srcfile %d%f_%ue.jpg -overwrite_original -common_args --ext jpg DIR [Advanced] Extract JpgFromRaw or PreviewImage from all but JPG files in DIR, saving them with file names like C<image_EXT.jpg>, then add all meta information from the original files to the extracted images. Here, the command line is broken into three sections (separated by B<-execute> options), and each is executed as if it were a separate command. The B<-common_args> option causes the C<--ext jpg DIR> arguments to be applied to all three commands, and the B<-srcfile> option allows the extracted JPG image to be the source file for the third command (whereas the RAW files are the source files for the other two commands). =back =head1 RENAMING EXAMPLES By writing the C<FileName> and C<Directory> tags, files are renamed and/or moved to new directories. This can be particularly useful and powerful for organizing files by date when combined with the B<-d> option. New directories are created as necessary, but existing files will not be overwritten. The format codes %d, %f and %e may be used in the new file name to represent the directory, name and extension of the original file, and %c may be used to add a copy number if the file already exists (see the B<-w> option for details). Note that if used within a date format string, an extra '%' must be added to pass these codes through the date/time parser. (And further note that in a Windows batch file, all '%' characters must also be escaped, so in this extreme case '%%%%f' is necessary to pass a simple '%f' through the two levels of parsing.) See L<http://owl.phy.queensu.ca/~phil/exiftool/filename.html> for additional documentation and examples. =over 5 =item exiftool -filename=new.jpg dir/old.jpg Rename C<old.jpg> to C<new.jpg> in directory C<dir>. =item exiftool -directory=%e dir Move all files from directory C<dir> into directories named by the original file extensions. =item exiftool '-Directory<DateTimeOriginal' -d %Y/%m/%d dir Move all files in C<dir> into a directory hierarchy based on year, month and day of C<DateTimeOriginal>. ie) This command would move the file C<dir/image.jpg> with a C<DateTimeOriginal> of C<2005:10:12 16:05:56> to C<2005/10/12/image.jpg>. =item exiftool -o . '-Directory<DateTimeOriginal' -d %Y/%m/%d dir Same effect as above except files are copied instead of moved. =item exiftool '-filename<%f_${model;}.%e' dir Rename all files in C<dir> by adding the camera model name to the file name. The semicolon after the tag name inside the braces causes characters which are invalid in Windows file names to be deleted from the tag value (see the B<-p> option documentation for an explanation). =item exiftool '-FileName<CreateDate' -d %Y%m%d_%H%M%S%%-c.%%e dir Rename all images in C<dir> according to the C<CreateDate> date and time, adding a copy number with leading '-' if the file already exists (C<%-c>), and preserving the original file extension (C<%e>). Note the extra '%' necessary to escape the filename codes (C<%c> and C<%e>) in the date format string. =item exiftool -r '-FileName<CreateDate' -d %Y-%m-%d/%H%M_%%f.%%e dir Both the directory and the filename may be changed together via the C<FileName> tag if the new C<FileName> contains a '/'. The example above recursively renames all images in a directory by adding a C<CreateDate> timestamp to the start of the filename, then moves them into new directories named by date. =item exiftool '-FileName<${CreateDate}_$filenumber.jpg' -d %Y%m%d -ext jpg . Set the filename of all JPG images in the current directory from the CreateDate and FileNumber tags, in the form "20060507_118-1861.jpg". =back =head1 GEOTAGGING EXAMPLES ExifTool implements geotagging via 3 special tags: Geotag (which for convenience is also implemented as an exiftool option), Geosync and Geotime. The examples below highlight some geotagging features. See L<http://owl.phy.queensu.ca/~phil/exiftool/geotag.html> for additional documentation. =over 5 =item exiftool -geotag track.log a.jpg Geotag an image (C<a.jpg>) from position information in a GPS track log (C<track.log>). Since the C<Geotime> tag is not specified, the value of DateTimeOriginal is used for geotagging. Local system time is assumed unless DateTimeOriginal contains a timezone. =item exiftool -geotag t.log -geotime='2009:04:02 13:41:12-05:00' a.jpg Geotag an image with the GPS position for a specific time. (Note that the C<Geotag> tag must be assigned before C<Geotime> for the GPS data to be available when C<Geotime> is set.) =item exiftool -geotag log.gpx '-xmp:geotimeE<lt>createdate' dir Geotag all images in directory C<dir> with XMP tags instead of EXIF tags, based on the image CreateDate. (In this case, the order of the arguments doesn't matter because tags with values copied from other tags are always set after constant values.) =item exiftool -geotag a.log -geosync=-20 dir Geotag images in directory C<dir>, accounting for image timestamps which were 20 seconds ahead of GPS. =item exiftool -geotag a.log -geosync=1.jpg -geosync=2.jpg dir Geotag images using time synchronization from two previously geotagged images (1.jpg and 2.jpg), synchronizing the image and GPS times using a linear time drift correction. =item exiftool -geotag a.log '-geotimeE<lt>${createdate}+01:00' dir Geotag images in C<dir> using CreateDate with the specified timezone. If CreateDate already contained a timezone, then the timezone specified on the command line is ignored. =item exiftool -geotag= a.jpg Delete GPS tags which may have been added by the geotag feature. Note that this does not remove all GPS tags -- to do this instead use C<-gps:all=>. =item exiftool -xmp:geotag= a.jpg Delete XMP GPS tags which were added by the geotag feature. =item exiftool -xmp:geotag=track.log a.jpg Geotag an image with XMP tags, using the time from DateTimeOriginal. =item exiftool -geotag a.log -geotag b.log -r dir Combine multiple track logs and geotag an entire directory tree of images. =item exiftool -geotag 'tracks/*.log' -r dir Read all track logs from the C<tracks> directory. =item exiftool -p gpx.fmt -d %Y-%m-%dT%H:%M:%SZ dir > out.gpx Generate a GPX track log from all images in directory C<dir>. This example uses the C<gpx.fmt> file included in the full ExifTool distribution package and assumes that the images in C<dir> have all been previously geotagged. =back =head1 PIPING EXAMPLES =over 5 =item cat a.jpg | exiftool - Extract information from stdin. =item exiftool image.jpg -thumbnailimage -b | exiftool - Extract information from an embedded thumbnail image. =item cat a.jpg | exiftool -iptc:keywords+=fantastic - > b.jpg Add an IPTC keyword in a pipeline, saving output to a new file. =item wget -qO - http://a.domain.com/bigfile.jpg | exiftool -fast - Extract information from an image over the internet using the GNU wget utility. The B<-fast> option prevents exiftool from scanning for trailer information, so only the meta information header is transferred. =item exiftool a.jpg -thumbnailimage -b | exiftool -comment=wow - | exiftool a.jpg -thumbnailimage'<=-' Add a comment to an embedded thumbnail image. (Why anyone would want to do this I don't know, but I've included this as an example to illustrate the flexibility of ExifTool.) =back =head1 DIAGNOSTICS The exiftool application exits with a status of 0 on success, or 1 if an error occured or if all files failed the B<-if> condition. =head1 AUTHOR Copyright 2003-2013, Phil Harvey This is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L<Image::ExifTool(3pm)|Image::ExifTool>, L<Image::ExifTool::TagNames(3pm)|Image::ExifTool::TagNames>, L<Image::ExifTool::Shortcuts(3pm)|Image::ExifTool::Shortcuts>, L<Image::ExifTool::Shift.pl|Image::ExifTool::Shift.pl> =cut #------------------------------------------------------------------------------ # end </pre> <!-- ############################################################################################################### --> <!-- ############################################################################################################### --> <!-- ############################################################################################################### --> </td></tr></table></center> <!-- ### WEB content ABOVE ## --> <hr> <!-- ############################################################################################################### --> <!-- ############################################################################################################### --> <!-- ### WEB PAGE ABOVE - boiler plate below ########################### updated 14 Oct 2016 ##### --> <table border="1" width="98%"> <tr align="center" valign="center"> <td width="20%"> <!-- ebooks table start --><center> <table border="2" width="90%" valign="middle"><tr> <td align="center"><a href="https://www.smashwords.com/books/view/512015" target="_blank"> <em>ebook:</em><br> Full Size<br> Jeep Buyer's<br> Guide</a> </td> <td bgcolor="white" align="center"> <center> <a href="https://www.smashwords.com/books/view/508267" target="_new"> <em>ebook:</em><br> Practical Suggestions<br> for Microsoft <br> Windows </a> </center></td> <td align="center"> <a href="https://www.smashwords.com/books/view/505731" target="new"> <img src="http://johnmeister.com/pix/Linux-Tackles-Microsoft-small.jpg" width="98%" alt="Linux Tackles Microsoft"></a> </td></tr></table></center><!-- ebooks table end --> <a href="http://shop.oreilly.com/product/0636920050209.do" target="new"> 12 hour Video Course by john: <br> The Art of Linux System Administration <br> published by <b>O'Reilly Media</b><br> Study Guide for the LPIC-2 Certification Exams</a> </td> <!-- ###### divider --> <td border="5" width="10%" bgcolor="white" background="http://johnmeister.com/.pix/smallgrid.jpg"> <!-- SiteSearch Google --> <form method="get" action="http://www.google.com/custom" target="google_window"> <center> <a href="http://www.google.com/"> </a> <font color="blue" size="-1"><em>search for: </em></font> <input type="hidden" name="domains" value="johnmeister.com/jeep ; johnmeister.com/jeep"></input> <input type="text" name="q" size="15" maxlength="255" value=""></input> <br> <input type="submit" name="sa" value="Search"></input> <font size="-4"> </font> <input type="radio" name="sitesearch" value=""></input> <font size="-1" color="#000000">on the internet, or:</font> </center> <input type="radio" name="sitesearch" value="johnmeister.com" checked="checked"></input> <font size="-1" color="#000000"> <a href="http://johnmeister.com" target="_blank">JohnMeister.com</a>-fotos </font> <br> <input type="radio" name="sitesearch" value="johnmeister.com/linux"></input> <font size="-1" color="#000000"> <a href="http://johnmeister.com/linux" target="_blank">LinuxMeister</a>-Linux</font> <br> <!-- ###### divider --> <input type="radio" name="sitesearch" value="BibleTech"></input> <font size="-1" color="#000000"> <a href="http://BibleTech" target="_blank">BibleTech</a>- <br>Bible overview</font><br> <br> <a href="http://johnmeister.com/pix/Promises-n-Prophets.jpg" target="new"> An overview of Biblical history: <br> <b>"Promises and Prophets"</b></a> </td> <!-- ###### divider --> <td border="5" width="6%" bgcolor="white" background="http://johnmeister.com/.pix/smallgrid.jpg"> <font size="-1" color="#000000"><center><a href="http://johnmeister.com/jeep/sj" target="_blank"> <input type="radio" name="sitesearch" value="johnmeister.com/jeep"></input> <img src="http://johnmeister.com/jeep/pix/fsj-link.jpg" width="40%"><br> Wagoneers</a><br> FULL SIZE JEEPS</font></center> <hr width="50%"> <input type="radio" name="sitesearch" value="johnmeister.com/jeep" ></input> <font size="-1" color="#000000"> <a href="http://johnmeister.com/jeep" target="_blank">JeepMeister</a> <br>"Jeep is America's<br><center> only real sports car."<br> <em>-Enzo Ferrari</em> </font><br> <hr width="50%"> <input type="radio" name="sitesearch" value="johnmeister.com/tech"></input> <font size="-1" color="#000000"> <a href="http://johnmeister.com/tech" target="_blank">MeisterTech</a><br> Diesels +</font> <br> </center> </td> <!-- ###### divider --> <td width="6%"> <a href="http://johnmeister.com/linux/Overview/" target="new">One Page Overview of Linux Commands<br> </a><br> <a href="http://johnmeister.com/linux/Intro-to-Linux/the-5-basic-commands.jpg" target="new"> <em> click for an image of the 5 essential Linux commands</em></a><br><br> <a href="http://johnmeister.com/linux/Intro-to-Linux/" target="new">An Intro to Linux <br> </a> </td> <!-- ###### divider --> <td border="5" align="left" width="9%" bgcolor="white" background="http://johnmeister.com/.pix/smallgrid.jpg"> <font size="-1"> <ul> <li><a href="http://www.oreilly.com/pub/au/6963" target="new"> John's O'Reilly Media profile </a> <li><a href="http://johnmeister.com/fotomeister/" target="new"> fotomeister.us: select fotos</a> &nbsp; <li><a href="https://www.smashwords.com/books/search?query=John+Meister" target="_new"> e-books by john</a> <li><a href="http://forecast.weather.gov/MapClick.php?lat=47.910704281893054&lon=-122.25997924804688#.U2gNmfg24xA" target="new"> Everett weather </a> <li><a href="http://www.wsdot.com/traffic/seattle/default.aspx?cam=1525#cam" target="new">Seattle traffic </a> <li><a href="http://www.nwasthma.com/pollen/pollen-count" target="new">pollen count </a> <li><a href="http://www.persecution.com/" target="new"> global persecution </a> <li><a href="http://www.jihadwatch.org/" target="_blank"> Jihad news </a> <li><a href="http://www.hallindsey.com/news/" target="_new">News Links </a> <li><a href="http://www.bbc.com/news/world/middle_east/" target="new">BBC: Middle East</a> <li><a href="http://www.israelnationalnews.com/" target="_blank">Israel National News </a> <li><a href="http://www.spiegel.de/international/world/" target="_blank">Der Spiegel</a> <li><a href="http://www.biblegateway.com/passage/?search=Daniel%209&version=NASB;KJV;LBLA;LUTH1545" target="new">BIBLES: NASB/KJV/ES/D </a> <li><a href="http://johnmeister.com/bible/SEAM" target="new"> South East Asian Missions</a> &nbsp;&nbsp;&nbsp; </ul> </font> </td> <!-- #### divider --> <td width="10%"> <table border="3"> <tr> <td bgcolor="ivory"><center> <font size="-1"> <a href="http://www.amsoil.com/mygarage/vehiclelookup.aspx?zo=283461" target="new">AMSOIL product guide</a>, <br> <a href="http://www.amsoil.com/redirect.cgi?zo=283461"> or, AMSOIL web, or 1-800-956-5695, <br> use customer #283461 </a> <br>Amsoil dealer since 1983 </font></center></td></tr> <tr> <td> <center><a href="http://www.amsoil.com/redirect.cgi?zo=283461" target="new"> <img src="http://johnmeister.com/pix/usdealer.jpg" width="50%"></a></center></td> </tr></td> </tr></table> <font size="-1"><em> purchase <a href="http://www.amsoil.com/redirect.cgi?zo=283461" target="new">AMSOIL</a> <br> at Midway Auto on SR9 in Snohomish, <br>or at Northland Diesel in Bellingham, WA</em></font> </td> </tr> </table> <!-- bottom of main README.html #################################################################################### --> <hr width="80%"> <!-- ##################TOP OF bottom row ############################################################################### --> <center> <table border="3" align="center"><tr> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/sj" target="new"><img src="http://johnmeister.com/pix/SJ-J10.jpg" width="80%"><br><font size="-1"> SJ - 1962-1991</font></a></td> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/XJ" target="new"> <img src="http://johnmeister.com/pix/XJ.jpg" width="80%"><br><font size="-1"> XJ - 1984-2001 </font></a></td> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/WJ" target="new"><img src="http://johnmeister.com/pix/WJ.jpg" width="80%"><br><font size="-1"> WJ - 1999-2004</font></a></td> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/KJ" target="new"><img src="http://johnmeister.com/pix/KJ.jpg" width="80%"><br><font size="-1"> KJ - 2002-2007</font></a></td> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/WK" target="new"><img src="http://johnmeister.com/pix/WK.jpg" width="80%"> <br><font size="-1"> WK - 2005-2010 </font></a></td> <td align="center" valign="center"> <a href="http://www.amsoil.com/mygarage/vehiclelookup.aspx?zo=283461" target="new"> <img src="http://johnmeister.com/pix/usdealer.jpg" width="90%"> <br><font size="-2">Find the recommended <br>AMSOIL synthetics <br>for your Jeep</font></a> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/CJ-10A" target="new"><img src="http://johnmeister.com/pix/CJ-10A.jpg" width="80%"><br><font size="-1"> CJ-10A - 1984-1986</font></a></td> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/Jeepsters" target="new"><img src="http://johnmeister.com/pix/JEEPSTER.jpg" width="80%"><br><font size="-1"> Jeepsters</font></a></td> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/MJ" target="new"> <img src="http://johnmeister.com/pix/MJ.jpg" width="80%"><br><font size="-1"> MJ - 1984-1992 </font></a></td> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/Willys" target="new"><img src="http://johnmeister.com/pix/CJ-2A.jpg" width="80%"><br><font size="-1"> Willys - 1946-1965</font></a></td> <td align="center" valign="center"> <a href="http://johnmeister.com/jeep/Other-Jeeps" target="new"><img src="http://johnmeister.com/pix/FC170.jpg" width="80%"><br><font size="-1"> Other Jeeps (FC)</font></a></td> </td></tr></table> </center> <!-- ####################################### BOTTOM OF bottom row ################################################## --> <center> <hr width="75%"> </center> </body> </html>

#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#------------------------------------------------------------------------------
# File:         exiftool
#
# Description:  Read/write meta information
#
# Revisions:    Nov. 12/03 - P. Harvey Created
#               (See html/history.html for revision history)
#
# References:   ATV - Alexander Vonk, private communication
#------------------------------------------------------------------------------
use strict;
require 5.004;

my $version = '9.34';

# add our 'lib' directory to the include list BEFORE 'use Image::ExifTool'
my $exeDir;
BEGIN {
    # get exe directory
    $exeDir = ($0 =~ /(.*)[\\\/]/) ? $1 : '.';
    # add lib directory at start of include path
    unshift @INC, "$exeDir/lib";
    # load or disable config file if specified
    if (@ARGV and lc($ARGV[0]) eq '-config') {
        shift;
        $Image::ExifTool::configFile = shift;
    }
}
use Image::ExifTool qw{:Public Open};

# function prototypes
sub SigInt();
sub SigCont();
sub Cleanup();
sub GetImageInfo($$);
sub SetImageInfo($$$);
sub CleanXML($);
sub EncodeXML($);
sub FormatXML($$$);
sub EscapeJSON($;$);
sub FormatJSON($$$);
sub PrintCSV();
sub ConvertBinary($);
sub IsEqual($$);
sub Infile($;$);
sub AddSetTagsFile($;$);
sub DoSetFromFile($$$);
sub CleanFilename($);
sub ProcessFiles($;$);
sub ScanDir($$;$);
sub SetFileTime($@);
sub PreserveTime();
sub SuggestedExtension($$$);
sub LoadPrintFormat($);
sub FilenameSPrintf($;$@);
sub NextUnusedFilename($;$);
sub CreateDirectory($);
sub OpenOutputFile($;@);
sub AcceptFile($);
sub SlurpFile($$);
sub Rename($$);
sub ReadStayOpen($);
sub PrintTagList($@);
sub PrintErrors($$$);

$SIG{INT}  = 'SigInt';  # do cleanup on Ctrl-C
$SIG{CONT} = 'SigCont'; # (allows break-out of delays)
END {
    Cleanup();
}

# declare all static file-scope variables
my @commonArgs;     # arguments common to all commands
my @csvFiles;       # list of files when reading with CSV option
my @csvTags;        # order of tags for first file with CSV option (lower case)
my @delFiles;       # list of files to delete
my @dynamicFiles;   # list of -tagsFromFile files with dynamic names and -TAG<=FMT pairs
my @exclude;        # list of excluded tags
my (@echo3, @echo4);# stdout and stderr echo after processing is complete
my @files;          # list of files and directories to scan
my @moreArgs;       # more arguments to process after -stay_open -@
my @newValues;      # list of new tag values to set
my @srcFmt;         # source file name format strings
my @tags;           # list of tags to extract
my %appended;       # list of files appended to
my %created;        # list of files we created
my %csvTags;        # lookup for all found tags with CSV option (lower case keys)
my %database;       # lookup for database information based on file name
my %filterExt;      # lookup for filtered extensions
my %ignore;         # directory names to ignore
my %preserveTime;   # preserved timestamps for files
my %printFmt;       # the contents of the print format file
my %setTags;        # hash of list references for tags to set from files
my %setTagsList;    # list of other tag lists for multiple -tagsFromFile from the same file
my %warnedOnce;     # lookup for once-only warnings
my %wext;           # -W extensions to write
my $allGroup;       # show group name for all tags
my $argFormat;      # use exiftool argument-format output
my $binaryOutput;   # flag for binary output (undef or 1, or 0 for binary XML/PHP)
my $binaryStdout;   # flag set if we output binary to stdout
my $comma;          # flag set if we need a comma in JSON output
my $condition;      # conditional processing of files
my $count;          # count of files scanned
my $countBad;       # count of files with errors
my $countBadCr;     # count files not created due to errors
my $countBadWr;     # count write errors
my $countCopyWr;    # count of files copied without being changed
my $countDir;       # count of directories scanned
my $countFailed;    # count files that failed condition
my $countGoodCr;    # count files created OK
my $countGoodWr;    # count files written OK
my $countNewDir;    # count of directories created
my $countSameWr;    # count files written OK but not changed
my $critical;       # flag for critical operations (disable CTRL-C)
my $csv;            # flag for CSV option (set to "CSV", or maybe "JSON" when writing)
my $csvAdd;         # flag to add CSV information to existing lists
my $csvSaveCount;   # save counter for last CSV file loaded
my $deleteOrig;     # 0=restore original files, 1=delete originals, 2=delete w/o asking
my $disableOutput;  # flag to disable normal output
my $doSetFileName;  # flag set if FileName may be written
my $doUnzip;        # flag to extract info from .gz and .bz2 files
my $escapeHTML;     # flag to escape printed values for html
my $evalWarning;    # warning from eval
my $executeID;      # -execute ID number
my $fileHeader;     # header to print to output file (or console, once)
my $fileTrailer;    # trailer for output file
my $filtered;       # flag indicating file was filtered by name
my $filterFlag;     # file filter flag (0x01=deny extensions, 0x02=allow extensions)
my $fixLen;         # flag to fix description lengths when writing alternate languages
my $forcePrint;     # string to use for missing tag values (undef to not print them)
my $helped;         # flag to avoid printing help if no tags specified
my $html;           # flag for html-formatted output (2=html dump)
my $interrupted;    # flag set if CTRL-C is pressed during a critical process
my $isWriting;      # flag set if we are writing tags
my $joinLists;      # flag set to join list values into a single string
my $json;           # flag for JSON/PHP output format (1=JSON, 2=PHP)
my $listItem;       # item number for extracting single item from a list
my $listSep;        # list item separator (', ' by default)
my $mainTool;       # main ExifTool object
my $multiFile;      # non-zero if we are scanning multiple files
my $outFormat;      # -1=Canon format, 0=same-line, 1=tag names, 2=values only
my $outOpt;         # output file or directory name
my $overwriteOrig;  # flag to overwrite original file (1=overwrite, 2=in place)
my $pause;          # pause before returning
my $preserveTime;   # flag to preserve times of updated files
my $progress;       # progress count
my $progressMax;    # total number of files to process
my $progStr;        # progress message string
my $quiet;          # flag to disable printing of informational messages / warnings
my $rafStdin;       # File::RandomAccess for stdin (if necessary to rewind)
my $recurse;        # recurse into subdirectories
my $rtnVal;         # command return value (0=success)
my $saveCount;      # count the number of times we will/did call SaveNewValues()
my $scanWritable;   # flag to process only writable file types
my $seqFileNum;     # sequential file number used for %C
my $showGroup;      # number of group to show (may be zero or '')
my $showTagID;      # non-zero to show tag ID's
my $stayOpenBuff='';# buffer for -stay_open file
my $stayOpenFile;   # name of the current -stay_open argfile
my $structOpt;      # output structured XMP information (JSON and XML output only)
my $tabFormat;      # non-zero for tab output format
my $tagOut;         # flag for separate text output file for each tag
my $textOut;        # extension for text output file (or undef for no output)
my $textOverwrite;  # flag to overwrite existing text output file (2=append, 3=over+append)
my $tmpFile;        # temporary file to delete on exit
my $tmpText;        # temporary text file
my $utf8;           # flag set if we are using UTF-8 encoding
my $validFile;      # flag indicating we processed a valid file
my $verbose;        # verbose setting
my $vout;           # verbose output file reference (\*STDOUT or \*STDERR)
my $xml;            # flag for XML-formatted output

# flag to keep the input -@ argfile open:
# 0 = normal behaviour
# 1 = received "-stay_open true" and waiting for argfile to keep open
# 2 = currently reading from STAYOPEN argfile
# 3 = waiting for -@ to switch to a new STAYOPEN argfile
my $stayOpen = 0;

# lookup for O/S names which may use a backslash as a directory separator
# (ref File::Spec of PathTools-3.2701)
my %hasBackslash = ( MSWin32 => 1, os2 => 1, dos => 1, NetWare => 1, symbian => 1, cygwin => 1 );

# lookup for O/S names which use CR/LF newlines
my $isCRLF = { MSWin32 => 1, os2 => 1, dos => 1 }->{$^O};

# lookup for JSON characters that we escape specially
my %jsonChar = ( '"'=>'"', '\\'=>'\\', "\t"=>'t', "\n"=>'n', "\r"=>'r' );

# options requiring additional arguments
# (used only to skip over these arguments when reading -stay_open ARGFILE)
my %optArgs = (
    '-tagsfromfile' => 1, '-addtagsfromfile' => 1, '-alltagsfromfile' => 1,
    '-@' => 1,
    '-c' => 1, '-coordformat' => 1,
    '-charset' => 0, # (optional arg; OK because arg cannot begin with "-")
    '-config' => 1,
    '-d' => 1, '-dateformat' => 1,
    '-D' => 0, # nececessary to avoid matching lower-case equivalent
    '-echo' => 1, '-echo2' => 1,
    '-ext' => 1, '--ext' => 1, '-extension' => 1, '--extension' => 1,
    '-fileorder' => 1,
    '-geotag' => 1,
    '-i' => 1, '-ignore' => 1,
    '-if' => 1,
    '-lang' => 0, # (optional arg; cannot begin with "-")
    '-listitem' => 1,
    '-o' => 1, '-out' => 1,
    '-p' => 1, '-printformat' => 1,
    '-P' => 0,
    '-password' => 1,
    '-require' => 1,
    '-sep' => 1, '-separator' => 1,
    '-srcfile' => 1,
    '-stay_open' => 1,
    '-use' => 1,
    '-w' => 1, '-w!' => 1, '-textout' => 1, '-textout!' => 1,
    '-x' => 1, '-exclude' => 1,
    '-X' => 0,
);

# exit routine
sub Exit {
    if ($pause) {
        if (eval 'require Term::ReadKey') {
            print STDERR "-- press any key --";
            Term::ReadKey::ReadMode('cbreak');
            Term::ReadKey::ReadKey(0);
            Term::ReadKey::ReadMode(0);
            print STDERR "\b \b" x 20;
        } else {
            print STDERR "-- press RETURN --\n";
            ;
        }
    }
    exit shift;
}
# my warning and error routines (NEVER say "die"!)
sub Warn  { warn(@_) if $quiet < 2 or $_[0] =~ /^Error/; }
sub Error { Warn @_; $rtnVal = 1; }
sub WarnOnce($) {
    Warn(@_) and $warnedOnce{$_[0]} = 1 unless $warnedOnce{$_[0]};
}

# define signal handlers and cleanup routine
sub SigInt()  {
    $critical and $interrupted = 1, return;
    Cleanup();
    exit 1;
}
sub SigCont() { }
sub Cleanup() {
    unlink $tmpFile if defined $tmpFile;
    unlink $tmpText if defined $tmpText;
    undef $tmpFile;
    undef $tmpText;
    PreserveTime() if %preserveTime;
}

#------------------------------------------------------------------------------
# main script
#

# isolate arguments common to all commands
if (grep /^-common_args$/i, @ARGV) {
    my (@newArgs, $common);
    foreach (@ARGV) {
        if (/^-common_args$/i) {
            $common = 1;
        } elsif ($common) {
            push @commonArgs, $_;
        } else {
            push @newArgs, $_;
        }
    }
    @ARGV = @newArgs if $common;
}

#..............................................................................
# loop over sets of command-line arguments separated by "-execute"
Command: for (;;) {

@echo3 and print STDOUT join("\n", @echo3), "\n";
@echo4 and print STDERR join("\n", @echo4), "\n";

$rafStdin->Close() if $rafStdin;
undef $rafStdin;

# exit Command loop now if we are all done processing commands
last unless @ARGV or not defined $rtnVal or $stayOpen >= 2 or @commonArgs;

# attempt to restore text mode for STDOUT if necessary
if ($binaryStdout) {
    binmode(STDOUT,':crlf') if $] >= 5.006 and $isCRLF;
    $binaryStdout = 0;
}

# flush console and print "{ready}" message if -stay_open is in effect
if ($stayOpen >= 2) {
    if ($quiet) {
        # flush output if possible
        eval 'require IO::Handle' and STDERR->flush(), STDOUT->flush();
    } else {
        eval 'require IO::Handle' and STDERR->flush();
        my $id = defined $executeID ? $executeID : '';
        my $save = $|;
        $| = 1;     # turn on output autoflush for stdout
        print "{ready$id}\n";
        $| = $save; # restore original autoflush setting
    }
}

$rtnVal = 0 unless defined $rtnVal;

# initialize necessary static file-scope variables
# (not done: @commonArgs, @moreArgs, $critical, $binaryStdout, $helped,
#  $interrupted, $mainTool, $pause, $rtnVal, $stayOpen, $stayOpenBuff, $stayOpenFile)
undef @csvFiles;
undef @csvTags;
undef @delFiles;
undef @dynamicFiles;
undef @echo3;
undef @echo4;
undef @exclude;
undef @files;
undef @newValues;
undef @srcFmt;
undef @tags;
undef %appended;
undef %created;
undef %csvTags;
undef %database;
undef %filterExt;
undef %ignore;
undef %printFmt;
undef %preserveTime;
undef %setTags;
undef %setTagsList;
undef %warnedOnce;
undef %wext;
undef $allGroup;
undef $argFormat;
undef $binaryOutput;
undef $comma;
undef $condition;
undef $csv;
undef $csvAdd;
undef $deleteOrig;
undef $disableOutput;
undef $doSetFileName;
undef $doUnzip;
undef $escapeHTML;
undef $evalWarning;
undef $executeID;
undef $fileHeader;
undef $fileTrailer;
undef $filtered;
undef $fixLen;
undef $forcePrint;
undef $joinLists;
undef $listItem;
undef $multiFile;
undef $outOpt;
undef $preserveTime;
undef $progress;
undef $progressMax;
undef $recurse;
undef $scanWritable;
undef $showGroup;
undef $showTagID;
undef $structOpt;
undef $tagOut;
undef $textOut;
undef $textOverwrite;
undef $tmpFile;
undef $tmpText;
undef $validFile;
undef $verbose;

$count = 0;
$countBad = 0;
$countBadCr = 0;
$countBadWr = 0;
$countCopyWr = 0;
$countDir = 0;
$countFailed = 0;
$countGoodCr = 0;
$countGoodWr = 0;
$countNewDir = 0;
$countSameWr = 0;
$csvSaveCount = 0;
$filterFlag = 0;
$html = 0;
$isWriting = 0;
$json = 0;
$listSep = ', ';
$outFormat = 0;
$overwriteOrig = 0;
$progStr = '';
$quiet = 0;
$saveCount = 0;
$seqFileNum = 0;
$tabFormat = 0;
$utf8 = 1;
$vout = \*STDOUT;
$xml = 0;

# define local variables used only in this command loop
my @fileOrder;      # tags to use for ordering of input files
my %excludeGrp;     # hash of tags excluded by group
my $addGeotime;     # automatically added geotime argument
my $allInGroup;     # flag to show all tags in a group
my $doGlob;         # flag set to do filename wildcard expansion
my $escapeXML;      # flag to escape printed values for xml
my $setTagsFile;    # filename for last TagsFromFile option
my $sortOpt;        # sort option is used
my $srcStdin;       # one of the source files is STDIN
my $useMWG;         # flag set if we are using any MWG tag

my ($argsLeft, @nextPass, $badCmd);
my $pass = 0;

# for Windows, use globbing for wildcard expansion if available - MK/20061010
if ($^O eq 'MSWin32' and eval 'require File::Glob') {
    # override the core glob forcing case insensitivity
    import File::Glob qw(:globally :nocase);
    $doGlob = 1;
}

$mainTool = new Image::ExifTool;        # create ExifTool object

# don't extract duplicates by default unless set by UserDefined::Options
$mainTool->Options(Duplicates => 0) unless %Image::ExifTool::UserDefined::Options
    and defined $Image::ExifTool::UserDefined::Options{Duplicates};

# default is to join lists if the List option was set in the config file
$joinLists = 1 if defined $mainTool->Options('List') and not $mainTool->Options('List');

# parse command-line options in 2 passes...
# pass 1: set all of our ExifTool options
# pass 2: print all of our help and informational output (-list, -ver, etc)
for (;;) {

  # execute the command now if no more arguments or -execute is used
  if (not @ARGV or $ARGV[0] =~ /^-execute(\d*)$/i) {
    next Command if $badCmd;
    if (@ARGV) {
        $executeID = $1;        # save -execute number for "{ready}" response
        $helped = 1;            # don't show help if we used -execute
    } elsif ($stayOpen >= 2) {
        ReadStayOpen(\@ARGV);   # read more arguments from -stay_open file
        next;
    }
    if ($pass == 0) {
        # insert common arguments now if not done already
        if (@commonArgs and not defined $argsLeft) {
            # count the number of arguments remaining for subsequent commands
            $argsLeft = scalar(@ARGV) + scalar(@moreArgs);
            unshift @ARGV, @commonArgs;
            # all done with commonArgs if this is the end of the command
            undef @commonArgs unless $argsLeft;
            next;
        }
        # check if we have more arguments now than we did before we processed
        # the common arguments.  If so, then we have an infinite processing loop
        if (defined $argsLeft and $argsLeft < scalar(@ARGV) + scalar(@moreArgs)) {
            Warn "Ignoring -common_args from $ARGV[0] onwards to avoid infinite recursion\n";
            while ($argsLeft < scalar(@ARGV) + scalar(@moreArgs)) {
                @ARGV and shift(@ARGV), next;
                shift @moreArgs;
            }
        }
        # require MWG module if used in any argument
        # (note: this also covers the -p option because these tags were added to @tags)
        $useMWG = 1 if not $useMWG and grep /^mwg:/i, @tags;
        if ($useMWG) {
            require Image::ExifTool::MWG;
            Image::ExifTool::MWG::Load();
        }
    }
    if (@nextPass) {
        # process arguments which were deferred to the next pass
        unshift @ARGV, @nextPass;
        undef @nextPass;
        ++$pass;
        next;
    }
    @ARGV and shift;    # remove -execute from argument list
    last;               # process the command now
  }
  $_ = shift;
  next if $badCmd;      # flush remaining arguments if aborting this command

  if (s/^(-|\xe2\x88\x92)//) {  # allow funny dashes (nroff dash bug for cut-n-paste from pod)
    s/^\xe2\x88\x92/-/;         # translate double-dash too
    my $a = lc $_;
    if (/^list([wfrdx]|wf|g(\d*))?$/i) {
        $pass or push(@nextPass,"-$_");
        my $type = lc($1 || '');
        if (not $type or $type eq 'w' or $type eq 'x') {
            my $group;
            if ($ARGV[0] and $ARGV[0] =~ /^(-|\xe2\x88\x92)(.+):(all|\*)$/i) {
                if ($pass == 0) {
                    $useMWG = 1 if lc($2) eq 'mwg';
                    push(@nextPass, shift);
                    next;
                }
                $group = $2;
                shift;
                $group =~ /IFD/i and Warn("Can't list tags for specific IFD\n"), next;
                $group =~ /^(all|\*)$/ and undef $group;
            } else {
                $pass or next;
            }
            $helped = 1;
            if ($type eq 'x') {
                require Image::ExifTool::TagInfoXML;
                my %opts;
                $opts{Flags} = 1 if defined $forcePrint;
                $opts{NoDesc} = 1 if $outFormat > 0;
                Image::ExifTool::TagInfoXML::Write(undef, $group, %opts);
                next;
            }
            my $wr = ($type eq 'w');
            my $msg = ($wr ? 'Writable' : 'Available') . ($group ? " $group" : '') . ' tags';
            PrintTagList($msg, $wr ? GetWritableTags($group) : GetAllTags($group));
            # also print shortcuts if listing all tags
            next if $group or $wr;
            my @tagList = GetShortcuts();
            PrintTagList('Command-line shortcuts', @tagList) if @tagList;
            next;
        }
        $pass or next;
        $helped = 1;
        if ($type eq 'wf') {
            my @wf;
            CanWrite($_) and push @wf, $_ foreach GetFileType();
            PrintTagList('Writable file extensions', @wf);
        } elsif ($type eq 'f') {
            PrintTagList('Supported file extensions', GetFileType());
        } elsif ($type eq 'r') {
            PrintTagList('Recognized file extensions', GetFileType(undef, 0));
        } elsif ($type eq 'd') {
            PrintTagList('Deletable groups', GetDeleteGroups());
        } else { # 'g(\d*)'
            # list all groups in specified family
            my $family = $2 || 0;
            PrintTagList("Groups in family $family", GetAllGroups($family));
        }
        next;
    }
    if ($a eq 'ver') {
        $pass or push(@nextPass,'-ver'), next;
        my $libVer = $Image::ExifTool::VERSION;
        my $str = $libVer eq $version ? '' : " [Warning: Library version is $libVer]";
        print("$version$str$Image::ExifTool::RELEASE\n");
        $helped = 1;
        next;
    }
    if (/^(all|add)?tagsfromfile(=.*)?$/i) {
        $setTagsFile = $2 ? substr($2,1) : (@ARGV ? shift : '');
        if ($setTagsFile eq '') {
            Error("File must be specified for -tagsFromFile option\n");
            $badCmd = 1;
            next;
        }
        # create necessary lists, etc for this new -tagsFromFile file
        AddSetTagsFile($setTagsFile, { Replace => ($1 and lc($1) eq 'add') ? 0 : 1 } );
        next;
    }
    if ($a eq '@') {
        my $argFile = shift or Error("Expecting filename for -\@ option\n"), $badCmd=1, next;
        # switch to new ARGFILE if using chained -stay_open options
        if ($stayOpen == 1) {
            # defer remaining arguments until we close this argfile
            @moreArgs = @ARGV;
            undef @ARGV;
        } elsif ($stayOpen == 3) {
            if ($stayOpenFile and $stayOpenFile ne '-' and $argFile eq $stayOpenFile) {
                # don't allow user to switch to the same -stay_open argfile
                # because it will result in endless recursion
                $stayOpen = 2;
                Warn "Ignoring request to switch to the same -stay_open ARGFILE ($argFile)\n";
                next;
            }
            close STAYOPEN;
            $stayOpen = 1;  # switch to this -stay_open file
        }
        my $fp = ($stayOpen == 1 ? \*STAYOPEN : \*ARGFILE);
        unless (Open($fp, $argFile)) {
            unless ($argFile !~ /^\// and Open($fp, "$exeDir/$argFile")) {
                Error "Error opening arg file $argFile\n";
                $badCmd = 1;
                next
            }
        }
        if ($stayOpen == 1) {
            $stayOpenFile = $argFile;   # remember the name of the file we have open
            $stayOpenBuff = '';         # initialize buffer for reading this file
            $stayOpen = 2;
            $helped = 1;
            ReadStayOpen(\@ARGV);
            next;
        }
        my (@newArgs, $didBOM);
        foreach () {
            # filter Byte Order Mark if it exists from start of UTF-8 text file
            unless ($didBOM) {
                s/^\xef\xbb\xbf//;
                $didBOM = 1;
            }
            s/^\s+//; s/[\x0d\x0a]+$//s; # remove leading white space and trailing newline
            # remove white space before, and single space after '=', '+=', '-=' or '<='
            s/^(-[-:\w]+#?)\s*([-+<]?=) ?/$1$2/;
            push @newArgs, $_ unless $_ eq '' or /^#/;
        }
        close ARGFILE;
        unshift @ARGV, @newArgs;
        next;
    }
    /^(-?)(a|duplicates)$/i and $mainTool->Options(Duplicates => ($1 ? 0 : 1)), next;
    /^arg(s|format)$/i and $argFormat = 1, next;
    /^b(inary)?$/i and $mainTool->Options(Binary => 1), $binaryOutput = 1, next;
    if (/^c(oordFormat)?$/i) {
        my $fmt = shift;
        $fmt or Error("Expecting coordinate format for -c option\n"), $badCmd=1, next;
        $mainTool->Options('CoordFormat', $fmt);
        next;
    }
    if ($a eq 'charset') {
        my $charset = (@ARGV and $ARGV[0] !~ /^(-|\xe2\x88\x92)/) ? shift : undef;
        if (not $charset) {
            $pass or push(@nextPass, '-charset'), next;
            my %charsets;
            $charsets{$_} = 1 foreach values %Image::ExifTool::charsetName;
            PrintTagList('Available character sets', sort keys %charsets);
            $helped = 1;
        } elsif ($charset !~ s/^(\w+)=// or lc($1) eq 'exiftool') {
            $mainTool->Options(Charset => $charset);
            $utf8 = ($mainTool->Options('Charset') eq 'UTF8');
        } else {
            # set internal encoding of specified metadata type
            my $type = { id3 => 'ID3', iptc => 'IPTC', exif => 'EXIF',
                         photoshop => 'Photoshop', quicktime => 'QuickTime' }->{lc $1};
            $type or Warn("Unknown type for -charset option: $1\n"), next;
            $mainTool->Options("Charset$type" => $charset);
        }
        next;
    }
    /^config$/i and Warn("Ignored -config option (not first on command line)\n"), shift, next;
    if (/^csv(\+?=.*)?/i) {
        my $csvFile = $1;
        # must process on 2nd pass so -f option is available
        unless ($pass) {
            push(@nextPass,"-$_");
            if ($csvFile) {
                push @newValues, { SaveCount => ++$saveCount }; # marker to save new values now
                $csvSaveCount = $saveCount;
            }
            next;
        }
        if ($csvFile) {
            $csvFile =~ s/^(\+?=)//;
            $csvAdd = 2 if $1 eq '+=';
            $vout = \*STDERR if $srcStdin;
            $verbose and print $vout "Reading CSV file $csvFile\n";
            require Image::ExifTool::Import;
            my $msg = Image::ExifTool::Import::ReadCSV($csvFile, \%database, $forcePrint);
            $msg and Warn("$msg\n");
            $isWriting = 1;
        }
        $csv = 'CSV';
        next;
    }
    if (/^d$/ or $a eq 'dateformat') {
        my $fmt = shift;
        $fmt or Error("Expecting date format for -d option\n"), $badCmd=1, next;
        $mainTool->Options('DateFormat', $fmt);
        next;
    }
    (/^D$/ or $a eq 'decimal') and $showTagID = 'D', next;
    /^delete_original(!?)$/i and $deleteOrig = ($1 ? 2 : 1), next;
    (/^e$/ or $a eq '-composite') and $mainTool->Options(Composite => 0), next;
    (/^-e$/ or $a eq 'composite') and $mainTool->Options(Composite => 1), next;
    (/^E$/ or $a eq 'escapehtml') and require Image::ExifTool::HTML and $escapeHTML = 1, next;
    ($a eq 'ex' or $a eq 'escapexml') and $escapeXML = 1, next;
    if (/^echo(\d)?$/i) {
        my $n = $1 || 1;
        my $arg = shift;
        next unless defined $arg;
        $n > 4 and Warn("Invalid -echo number\n"), next;
        if ($n > 2) {
            $n == 3 ? push(@echo3, $arg) : push(@echo4, $arg);
        } else {
            print {$n==2 ? \*STDERR : \*STDOUT} $arg, "\n";
        }
        $helped = 1;
        next;
    }
    if (/^(ee|extractembedded)$/i) {
        $mainTool->Options(ExtractEmbedded => 1);
        $mainTool->Options(Duplicates => 1);
        next;
    }
    # (-execute handled at top of loop)
    if (/^-?ext(ension)?$/i) {
        my $ext = shift;
        defined $ext or Error("Expecting extension for -ext option\n"), $badCmd=1, next;
        $ext =~ s/^\.//;    # remove leading '.' if it exists
        my $flag = /^-/ ? 0 : 1;
        $filterFlag |= (0x01 << $flag);
        $filterExt{uc($ext)} = $flag;
        next;
    }
    if (/^f$/ or $a eq 'forceprint') {
        $forcePrint = $mainTool->Options('MissingTagValue');
        unless (defined $forcePrint) {
            $forcePrint = '-';
            $mainTool->Options(MissingTagValue => '-');
        }
        next;
    }
    if (/^F([-+]?\d*)$/ or /^fixbase([-+]?\d*)$/i) {
        $mainTool->Options(FixBase => $1);
        next;
    }
    if (/^fast(\d*)$/i) {
        $mainTool->Options(FastScan => (length $1 ? $1 : 1));
        next;
    }
    if ($a eq 'fileorder') {
        push @fileOrder, shift if @ARGV;
        next;
    }
    $a eq 'globaltimeshift' and $mainTool->Options(GlobalTimeShift => shift), next;
    if (/^(g)(roupHeadings|roupNames)?([\d:]*)$/i) {
        $showGroup = $3 || 0;
        $allGroup = ($2 ? lc($2) eq 'roupnames' : $1 eq 'G');
        $mainTool->Options(SavePath => 1) if $showGroup =~ /\b5\b/;
        next;
    }
    if ($a eq 'geotag') {
        my $trkfile = shift;
        $trkfile or Error("Expecting file name for -geotag option\n"), $badCmd=1, next;
        # allow wildcards in filename
        if ($trkfile =~ /[*?]/) {
            # CORE::glob() splits on white space, so use File::Glob if possible
            my @trks = eval('require File::Glob') ? File::Glob::bsd_glob($trkfile) : glob($trkfile);
            @trks or Error("No matching file found for -geotag option\n"), $badCmd=1, next;
            push @newValues, 'geotag='.shift(@trks) while @trks > 1;
            $trkfile = pop(@trks);
        }
        $_ = "geotag=$trkfile";
        # (fall through!)
    }
    if (/^h$/ or $a eq 'htmlformat') {
        require Image::ExifTool::HTML;
        $html = $escapeHTML = 1;
        $json = $xml = 0;
        next;
    }
    (/^H$/ or $a eq 'hex') and $showTagID = 'H', next;
    if (/^htmldump([-+]?\d+)?$/i) {
        $verbose = ($verbose || 0) + 1;
        $html = 2;
        $mainTool->Options(HtmlDumpBase => $1) if defined $1;
        next;
    }
    if (/^i(gnore)?$/i) {
        my $dir = shift;
        defined $dir or Error("Expecting directory name for -i option\n"), $badCmd=1, next;
        $ignore{$dir} = 1;
        next;
    }
    if ($a eq 'if') {
        my $cond = shift;
        defined $cond or Error("Expecting expression for -if option\n"), $badCmd=1, next;
        $useMWG = 1 if $cond =~ /\$\{?mwg:/i;
        if (defined $condition) {
            $condition .= " and ($cond)";
        } else {
            $condition = "($cond)";
        }
        next;
    }
    if (/^j(son)?(\+?=.*)?$/i) {
        if ($2) {
            # must process on 2nd pass because we need -f and -charset options
            unless ($pass) {
                push(@nextPass,"-$_");
                push @newValues, { SaveCount => ++$saveCount }; # marker to save new values now
                $csvSaveCount = $saveCount;
                next;
            }
            my $jsonFile = $2;
            $jsonFile =~ s/^(\+?=)//;
            $csvAdd = 2 if $1 eq '+=';
            $vout = \*STDERR if $srcStdin;
            $verbose and print $vout "Reading JSON file $jsonFile\n";
            my $chset = $mainTool->Options('Charset');
            require Image::ExifTool::Import;
            my $msg = Image::ExifTool::Import::ReadJSON($jsonFile, \%database, $forcePrint, $chset);
            $msg and Warn("$msg\n");
            $isWriting = 1;
            $csv = 'JSON';
        } else {
            $json = 1;
            $html = $xml = 0;
            $mainTool->Options(Duplicates => 1);
            require Image::ExifTool::XMP;   # for FixUTF8()
        }
        next;
    }
    /^(k|pause)$/i and $pause = 1, next;
    (/^l$/ or $a eq 'long') and --$outFormat, next;
    (/^L$/ or $a eq 'latin') and $utf8 = 0, $mainTool->Options(Charset => 'Latin'), next;
    if ($a eq 'lang') {
        my $lang = (@ARGV and $ARGV[0] !~ /^-/) ? shift : undef;
        if ($lang) {
            # make lower case and use underline as a separator (ie. 'en_ca')
            $lang =~ tr/-A-Z/_a-z/;
            $mainTool->Options(Lang => $lang);
            next if $lang eq $mainTool->Options('Lang');
        } else {
            $pass or push(@nextPass, '-lang'), next;
        }
        my $langs = "Available languages:\n";
        $langs .= "  $_ - $Image::ExifTool::langName{$_}\n" foreach @Image::ExifTool::langs;
        $langs =~ tr/_/-/;  # display dashes instead of underlines in language codes
        $langs = $mainTool->Decode($langs, 'UTF8');
        $langs = Image::ExifTool::HTML::EscapeHTML($langs) if $escapeHTML;
        $lang and Error("Invalid or unsupported language '$lang'.\n$langs"), $badCmd=1, next;
        print $langs;
        $helped = 1;
        next;
    }
    if ($a eq 'listitem') {
        $listItem = shift;
        defined $listItem or Warn("Expecting index for -listItem option\n");
        next;
    }
    /^(m|ignoreminorerrors)$/i and $mainTool->Options(IgnoreMinorErrors => 1), next;
    /^(n|-printconv)$/i and $mainTool->Options(PrintConv => 0), next;
    /^(-n|printconv)$/i and $mainTool->Options(PrintConv => 1), next;
    if (/^o(ut)?$/i) {
        $outOpt = shift;
        defined $outOpt or Error("Expected output file or directory name for -o option\n"), $badCmd=1, next;
        CleanFilename($outOpt);
        # verbose messages go to STDERR of output is to console
        $vout = \*STDERR if $vout =~ /^-(\.\w+)?$/;
        next;
    }
    /^overwrite_original$/i and $overwriteOrig = 1, next;
    /^overwrite_original_in_place$/i and $overwriteOrig = 2, next;
    (/^p$/ or $a eq 'printformat') and LoadPrintFormat(shift), next;
    (/^P$/ or $a eq 'preserve') and $preserveTime = 1, next;
    /^password$/i and $mainTool->Options(Password => shift), next;
    if ($a eq 'progress') {
        $progress = 0;
        $verbose = 0 unless defined $verbose;
        next;
    }
    /^q(uiet)?$/i and ++$quiet, next;
    /^r(ecurse)?$/i and $recurse = 1, next;
    if ($a eq 'require') { # undocumented, added in version 8.65
        my $ver = shift;
        unless (defined $ver and Image::ExifTool::IsFloat($ver)) {
            Error("Expecting version number for -require option\n");
            $badCmd = 1;
            next;
        }
        unless ($Image::ExifTool::VERSION >= $ver) {
            Error("Requires ExifTool version $ver or later\n");
            $badCmd = 1;
        }
        next;
    }
    /^restore_original$/i and $deleteOrig = 0, next;
    (/^S$/ or $a eq 'veryshort') and $outFormat+=2, next;
    /^s(hort)?(\d*)$/i and $outFormat = $2 eq '' ? $outFormat + 1 : $2, next;
    /^scanforxmp$/i and $mainTool->Options(ScanForXMP => 1), next;
    if (/^sep(arator)?$/i) {
        $listSep = shift;
        defined $listSep or Error("Expecting list item separator for -sep option\n"), $badCmd=1, next;
        $mainTool->Options(ListSep => $listSep);
        $joinLists = 1;
        # also split when writing values
        my $listSplit = quotemeta $listSep;
        # a space in the string matches zero or more whitespace characters
        $listSplit =~ s/(\\ )+/\\s\*/g;
        # but a single space alone matches one or more whitespace characters
        $listSplit = '\\s+' if $listSplit eq '\\s*';
        $mainTool->Options(ListSplit => $listSplit);
        next;
    }
    /^sort$/i and $sortOpt = 1, next;
    if ($a eq 'srcfile') {
        @ARGV or Warn("Expecting FMT for -srcfile option\n"), next;
        push @srcFmt, shift;
        next;
    }
    if ($a eq 'stay_open') {
        my $arg = shift;
        defined $arg or Warn("Expecting argument for -stay_open option\n"), next;
        if ($arg =~ /^(1|true)$/i) {
            if (not $stayOpen) {
                $stayOpen = 1;
            } elsif ($stayOpen == 2) {
                $stayOpen = 3;  # chained -stay_open options
            } else {
                Warn "-stay_open already active\n";
            }
        } elsif ($arg =~ /^(0|false)$/i) {
            if ($stayOpen >= 2) {
                # close -stay_open argfile and process arguments up to this point
                close STAYOPEN;
                push @ARGV, @moreArgs;
                undef @moreArgs;
            } elsif (not $stayOpen) {
                Warn("-stay_open wasn't active\n");
            }
            $stayOpen = 0;
        } else {
            Warn "Invalid argument for -stay_open\n";
        }
        next;
    }
    if (/^(-)?struct$/i) {
        $structOpt = $1 ? 0 : 1;
        $mainTool->Options(Struct => $structOpt);
        # require XMPStruct in case we need to serialize a structure
        require 'Image/ExifTool/XMPStruct.pl' if $structOpt;
        next;
    }
    /^t(ab)?$/  and $tabFormat = 1, next;
    if (/^T$/ or $a eq 'table') {
        $tabFormat = 1; $outFormat+=2; ++$quiet;
        $forcePrint = $mainTool->Options('MissingTagValue');
        unless (defined $forcePrint) {
            $forcePrint = '-';
            $mainTool->Options(MissingTagValue => '-');
        }
        next;
    }
    if (/^(u)(nknown(2)?)?$/i) {
        my $inc = ($3 or (not $2 and $1 eq 'U')) ? 2 : 1;
        $mainTool->Options(Unknown => $mainTool->Options('Unknown') + $inc);
        next;
    }
    if ($a eq 'use') {
        my $module = shift;
        $module or Error("Expecting module name for -use option\n"), $badCmd=1, next;
        lc $module eq 'mwg' and $useMWG = 1, next;
        local $SIG{'__WARN__'} = sub { $evalWarning = $_[0] };
        unless (eval "require Image::ExifTool::$module" or
                eval "require $module" or
                eval "require '$module'")
        {
            delete $SIG{'__WARN__'};
            Error("Error using module $module\n");
            $badCmd = 1;
        }
        next;
    }
    if (/^v(erbose)?(\d*)$/i) {
        $verbose = ($2 eq '') ? ($verbose || 0) + 1 : $2;
        next;
    }
    if (/^(w|textout|tagout)([!+]*)$/i) {
        $textOut = shift || Warn("Expecting output extension for -$_ option\n");
        my ($t1, $t2) = ($1, $2);
        $textOverwrite = 0;
        $textOverwrite += 1 if $t2 =~ /!/;  # overwrite
        $textOverwrite += 2 if $t2 =~ /\+/; # append
        if ($t1 ne 'W' and lc($t1) ne 'tagout') {
            undef $tagOut;
        } elsif ($textOverwrite >= 2 and $textOut !~ /%[-+]?\d*[.:]?\d*[lu]?[tgs]/) {
            $tagOut = 0; # append tags to one file
        } else {
            $tagOut = 1; # separate file for each tag
        }
        next;
    }
    if (/^(-?)(wext|tagoutext)$/i) {
        my $ext = shift;
        defined $ext or Error("Expecting extension for -wext option\n"), $badCmd=1, next;
        my $flag = 1;
        $1 and $wext{'*'} = 1, $flag = -1;
        $ext =~ s/^\.//;
        $wext{lc $ext} = $flag;
        next;
    }
    if ($a eq 'wm' or $a eq 'writemode') {
        my $wm = shift;
        defined $wm or Error("Expecting argument for -$_ option\n"), $badCmd=1, next;
        $wm =~ /^[wcg]*$/i or Error("Invalid argument for -$_ option\n"), $badCmd=1, next;
        $mainTool->Options(WriteMode => $wm);
        next;
    }
    if (/^x$/ or $a eq 'exclude') {
        my $tag = shift;
        defined $tag or Error("Expecting tag name for -x option\n"), $badCmd=1, next;
        $tag =~ s/\ball\b/\*/ig;    # replace 'all' with '*' in tag names
        if ($setTagsFile) {
            push @{$setTags{$setTagsFile}}, "-$tag";
        } else {
            push @exclude, $tag;
        }
        next;
    }
    (/^X$/ or $a eq 'xmlformat') and $xml = 1, $html = $json = 0, $mainTool->Options(Duplicates => 1), next;
    if (/^php$/i) {
        $json = 2;
        $html = $xml = 0;
        $mainTool->Options(Duplicates=>1);
        next;
    }
    /^z(ip)?$/i and $doUnzip = 1, $mainTool->Options(Compress => 1, Compact => 1), next;
    $_ eq '' and push(@files, '-'), $srcStdin = 1, next;   # read STDIN
    length $_ eq 1 and $_ ne '*' and Error("Unknown option -$_\n"), $badCmd=1, next;
    if (/^[^<]+( ++$saveCount };
        }
        push @newValues, $_;
        if (/^mwg:/i) {
            $useMWG = 1;
        } elsif (/^([-\w]+:)*(filename|directory)\b/i) {
            $doSetFileName = 1;
        } elsif (/^([-\w]+:)*(geotag|geotime)\b/i) {
            if (lc $2 eq 'geotag') {
                if ((not defined $addGeotime or $addGeotime) and length $val) {
                    $addGeotime = ($1 || '') . 'Geotime)/;
        if ($setTagsFile) {
            push @{$setTags{$setTagsFile}}, $_;
            if (/>/) {
                $useMWG = 1 if /^(.*>\s*)?mwg:/si;
                if (/\b(filename|directory)#?$/i) {
                    $doSetFileName = 1;
                } elsif (/\bgeotime#?$/i) {
                    $addGeotime = '';
                }
            } else {
                $useMWG = 1 if /^([^<]+<\s*(.*\$\{?)?)?mwg:/si;
                if (/^([-\w]+:)*(filename|directory)\b/i) {
                    $doSetFileName = 1;
                } elsif (/^([-\w]+:)*geotime\b/i) {
                    $addGeotime = '';
                }
            }
        } elsif (/^-(.*)/) {
            push @exclude, $1;
        } else {
            push @tags, $_;
        }
    }
  } elsif ($doGlob and /[*?]/) {
    # glob each filespec if necessary - MK/20061010
    push @files, File::Glob::bsd_glob($_);
    $doGlob = 2;
  } else {
    push @files, $_;
    $srcStdin = 1 if $_ eq '-';
  }
}

# set verbose output to STDERR if output could be to console
$vout = \*STDERR if $srcStdin and ($isWriting or @newValues);
$mainTool->Options(TextOut => $vout) if $vout eq \*STDERR;

# change default EXIF string encoding if MWG used
if ($useMWG and not defined $mainTool->Options('CharsetEXIF')) {
    $mainTool->Options(CharsetEXIF => 'UTF8');
}

# print help
unless ((@tags and not $outOpt) or @files or @newValues) {
    if ($doGlob and $doGlob == 2) {
        Warn "No matching files\n";
        $rtnVal = 1;
        next;
    }
    if ($outOpt) {
        Warn "Nothing to write\n";
        $rtnVal = 1;
        next;
    }
    unless ($helped) {
        # catch warnings if we have problems running perldoc
        local $SIG{'__WARN__'} = sub { $evalWarning = $_[0] };
        my $dummy = \*SAVEERR;  # avoid "used only once" warning
        unless ($^O eq 'os2') {
            open SAVEERR, ">&STDERR";
            open STDERR, '>/dev/null';
        }
        if (system('perldoc',$0)) {
            print "Syntax:  exiftool [OPTIONS] FILE\n\n";
            print "Consult the exiftool documentation for a full list of options.\n";
        }
        unless ($^O eq 'os2') {
            close STDERR;
            open STDERR, '>&SAVEERR';
        }
    }
    next;
}

# do sanity check on -delete_original and -restore_original
if (defined $deleteOrig and (@newValues or @tags)) {
    if (not @newValues) {
        my $verb = $deleteOrig ? 'deleting' : 'restoring from';
        Warn "Can't specify tags when $verb originals\n";
    } elsif ($deleteOrig) {
        Warn "Can't use -delete_original when writing.\n";
        Warn "Maybe you meant -overwrite_original ?\n";
    } else {
        Warn "It makes no sense to use -restore_original when writing\n";
    }
    $rtnVal = 1;
    next;
}

if ($overwriteOrig > 1 and $outOpt) {
    Warn "Can't overwrite in place when -o option is used\n";
    $rtnVal = 1;
    next;
}

if ($tagOut and ($csv or %printFmt or $tabFormat or $xml or ($verbose and $html))) {
    Warn "Sorry, -W may not be combined with -csv, -htmlDump, -j, -p, -t or -X\n";
    $rtnVal = 1;
    next;
}

if ($textOut and $csv and $csv eq 'CSV' and not $isWriting) {
    Warn "Sorry, -w may not be combined with -csv\n";
    $rtnVal = 1;
    next;
}

if ($escapeHTML or $json) {
    # must be UTF8 for HTML conversion and JSON output
    $mainTool->Options(Charset => 'UTF8');
    # use Escape option to do our HTML escaping unless XML output
    $mainTool->Options(Escape => 'HTML') if $escapeHTML and not $xml;
} elsif ($escapeXML and not $xml) {
    $mainTool->Options(Escape => 'XML');
}

# set sort option
if ($sortOpt) {
    # (note that -csv sorts alphabetically by default anyway if more than 1 file)
    my $sort = ($outFormat > 0 or $xml or $json or $csv) ? 'Tag' : 'Descr';
    $mainTool->Options(Sort => $sort, Sort2 => $sort);
}

# set up for RDF/XML, JSON and PHP output formats
if ($xml) {
    require Image::ExifTool::XMP;   # for EscapeXML()
    my $charset = $mainTool->Options('Charset');
    # standard XML encoding names for supported Charset settings
    # (ref http://www.iana.org/assignments/character-sets)
    my %encoding = (
        UTF8     => 'UTF-8',
        Latin    => 'windows-1252',
        Latin2   => 'windows-1250',
        Cyrillic => 'windows-1251',
        Greek    => 'windows-1253',
        Turkish  => 'windows-1254',
        Hebrew   => 'windows-1255',
        Arabic   => 'windows-1256',
        Baltic   => 'windows-1257',
        Vietnam  => 'windows-1258',
        MacRoman => 'macintosh',
    );
    # switch to UTF-8 if we don't have a standard encoding name
    unless ($encoding{$charset}) {
        $charset = 'UTF8';
        $mainTool->Options(Charset => $charset);
    }
    # set file header/trailer for XML output
    $fileHeader = "\n" .
                  "\n";
    $fileTrailer = "\n";
    # extract as a list unless short output format
    $joinLists = 1 if $outFormat > 0;
    $mainTool->Options(List => 1) unless $joinLists;
    $showGroup = $allGroup = 1;         # always show group 1
    # set binaryOutput flag to 0 or undef (0 = output encoded binary in XML)
    $binaryOutput = ($outFormat > 0 ? undef : 0) if $binaryOutput;
    $showTagID = 'D' if $tabFormat and not $showTagID;
} elsif ($json) {
    if ($json == 1) { # JSON
        $fileHeader = '[';
        $fileTrailer = "]\n";
        undef $binaryOutput; # can't currently use -b with -json
    } else { # PHP
        $fileHeader = 'Array(';
        $fileTrailer = ");\n";
        # allow binary output in a text-mode file when -php and -b used together
        # (this works because PHP strings are simple arrays of bytes, and CR/LF
        #  won't be messed up in the text mode output because they are converted
        #  to escape sequences in the strings)
        $binaryOutput = 0 if $binaryOutput;
    }
    $mainTool->Options(List => 1) unless $joinLists;
    $mainTool->Options(Duplicates => 0) unless defined $showGroup;
} elsif ($structOpt) {
    $mainTool->Options(List => 1);
} else {
    $joinLists = 1;     # join lists for all other unstructured output formats
}

if ($argFormat) {
    $outFormat = 3;
    $allGroup = 1 if defined $showGroup;
}

# change to forward slashes if necessary in all filenames (like CleanFilename)
if ($hasBackslash{$^O}) {
    tr/\\/\// foreach @files;
}

# can't do anything if no file specified
unless (@files) {
    unless ($outOpt) {
        Warn "No file specified\n";
        $rtnVal = 1;
        next;
    }
    push @files, '';    # create file from nothing
}

# set Verbose and HtmlDump options
if ($verbose) {
    $disableOutput = 1 unless @tags or @exclude or $tagOut;
    undef $binaryOutput unless $tagOut;    # disable conflicting option
    if ($html) {
        $html = 2;    # flag for html dump
        $mainTool->Options(HtmlDump => $verbose);
    } else {
        $mainTool->Options(Verbose => $verbose) unless $tagOut;
    }
} elsif (defined $verbose) {
    # auto-flush output when -v0 is used
    require FileHandle;
    STDOUT->autoflush(1);
    STDERR->autoflush(1);
}

# validate all tags we're writing
my $needSave = 1;
if (@newValues) {
    # assume -geotime value if -geotag specified without -geotime
    if ($addGeotime) {
        AddSetTagsFile($setTagsFile = '@') unless $setTagsFile and $setTagsFile eq '@';
        push @{$setTags{$setTagsFile}}, $addGeotime;
        $verbose and print $vout qq{Argument "-$addGeotime" is assumed\n};
    }
    my %setTagsIndex;
    # add/delete option lookup
    my %addDelOpt = ( '+' => 'AddValue', '-' => 'DelValue', "\xe2\x88\x92" => 'DelValue' );
    $saveCount = 0;
    foreach (@newValues) {
        if (ref $_ eq 'HASH') {
            # save new values now if we stored a "SaveCount" marker
            if ($$_{SaveCount}) {
                $saveCount = $mainTool->SaveNewValues();
                $needSave = 0;
                # insert marker to load values from CSV file now if this was the CSV file
                push @dynamicFiles, \$csv if $$_{SaveCount} == $csvSaveCount;
            }
            next;
        }
        /(.*?)=(.*)/s or next;
        my ($tag, $newVal) = ($1, $2);
        $tag =~ s/\ball\b/\*/ig;    # replace 'all' with '*' in tag names
        $newVal eq '' and undef $newVal;    # undefined to delete tag
        if ($tag =~ /^(All)?TagsFromFile$/i) {
            defined $newVal or Error("Need file name for -tagsFromFile\n"), next Command;
            ++$isWriting;
            if ($newVal eq '@' or not defined FilenameSPrintf($newVal)) {
                push @dynamicFiles, $newVal;
                next;   # set tags from dynamic file later
            }
            unless (-e $newVal or $newVal eq '-') {
                Warn "File '$newVal' does not exist for -tagsFromFile option\n";
                $rtnVal = 1;
                next Command;
            }
            my $setTags = $setTags{$newVal};
            # do we have multiple -tagsFromFile options with this file?
            if ($setTagsList{$newVal}) {
                # use the tags set in the i-th occurrence
                my $i = $setTagsIndex{$newVal} || 0;
                $setTagsIndex{$newVal} = $i + 1;
                $setTags = $setTagsList{$newVal}[$i] if $setTagsList{$newVal}[$i];
            }
            # set specified tags from this file
            unless (DoSetFromFile($mainTool, $newVal, $setTags)) {
                $rtnVal = 1;
                next Command;
            }
            $needSave = 1;
            next;
        }
        my %opts = ( Shift => 0 );  # shift values if possible instead of adding/deleting
        # allow writing of 'unsafe' tags unless specified by wildcard
        $opts{Protected} = 1 unless $tag =~ /[?*]/;

        if ($tag =~ s/SetNewValue($tag, $newVal, %opts);
        $needSave = 1;
        ++$isWriting if $rtn;
        $wrn and Warn "Warning: $wrn\n";
    }
    # exclude specified tags
    foreach (@exclude) {
        $mainTool->SetNewValue($_, undef, Replace => 2);
        $needSave = 1;
    }
    unless ($isWriting or $outOpt or @tags) {
        Warn "Nothing to do.\n";
        $rtnVal = 1;
        next;
    }
} elsif (grep /^(\*:)?\*$/, @exclude) {
    Warn "All tags excluded -- nothing to do.\n";
    $rtnVal = 1;
    next;
}
if ($isWriting and @tags and not $outOpt) {
    my ($tg, $s) = @tags > 1 ? ("$tags[0] ...", 's') : ($tags[0], '');
    Warn "Ignored superfluous tag name$s or invalid option$s: -$tg\n";
}
# save current state of new values if setting values from target file
# or if we may be translating to a different format
$mainTool->SaveNewValues() if $outOpt or (@dynamicFiles and $needSave);

$multiFile = 1 if @files > 1;
@exclude and $mainTool->Options(Exclude => \@exclude);

undef $binaryOutput if $html;

if ($binaryOutput) {
    $outFormat = 99;    # shortest possible output format
    $mainTool->Options(PrintConv => 0);
    unless ($textOut or $binaryStdout) {
        binmode(STDOUT);
        $binaryStdout = 1;
        $mainTool->Options(TextOut => ($vout = \*STDERR));
    }
    # disable conflicting options
    undef $showGroup;
}

# sort by groups to look nicer depending on options
if (defined $showGroup and not (@tags and $allGroup)) {
    $mainTool->Options(Sort => "Group$showGroup");
}

if (defined $textOut) {
    CleanFilename($textOut);  # make all forward slashes
    # add '.' before output extension if necessary
    $textOut = ".$textOut" unless $textOut =~ /[.%]/ or defined $tagOut;
}

# determine if we should scan for only writable files
if ($outOpt) {
    my $type = GetFileType($outOpt);
    if ($type) {
        unless (CanWrite($type)) {
            Warn "Can't write $type files\n";
            $rtnVal = 1;
            next;
        }
        $scanWritable = $type unless CanCreate($type);
    } else {
        $scanWritable = 1;
    }
    $isWriting = 1;     # set writing flag
} elsif ($isWriting or defined $deleteOrig) {
    $scanWritable = 1;
}

# set flag to fix description lengths if necessary
$fixLen = ($utf8 and $mainTool->Options('Lang') ne 'en' and eval 'require Encode');

# sort input files if specified
if (@fileOrder) {
    my @allFiles;
    ProcessFiles(undef, \@allFiles);
    my $sortTool = new Image::ExifTool;
    $sortTool->Options(PrintConv => $mainTool->Options('PrintConv'));
    $sortTool->Options(Duplicates => 0);
    my (%sortBy, %isFloat, @rev, $file);
    # save reverse sort flags
    push @rev, (s/^-// ? 1 : 0) foreach @fileOrder;
    foreach $file (@allFiles) {
        my @tags;
        my $info = $sortTool->ImageInfo(Infile($file,1), @fileOrder, \@tags);
        # get values of all tags (or '~' to sort last if not defined)
        foreach (@tags) {
            $_ = $$info{$_};
            defined $_ or $_ = '~', next;
            $isFloat{$_} = 1 if /^[+-]?(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
        }
        $sortBy{$file} = \@tags;    # save tag values for each file
    }
    # sort in specified order
    @files = sort {
        my ($i, $cmp);
        for ($i=0; $i<@rev; ++$i) {
            my $u = $sortBy{$a}[$i];
            my $v = $sortBy{$b}[$i];
            if (not $isFloat{$u} and not $isFloat{$v}) {
                $cmp = $u cmp $v;               # alphabetically
            } elsif ($isFloat{$u} and $isFloat{$v}) {
                $cmp = $u <=> $v;               # numerically
            } else {
                $cmp = $isFloat{$u} ? -1 : 1;   # numbers first
            }
            return $rev[$i] ? -$cmp : $cmp if $cmp;
        }
        return $a cmp $b;   # default to sort by name
    } @allFiles;
} elsif (defined $progress) {
    # expand FILE argument to count the number of files to process
    my @allFiles;
    ProcessFiles(undef, \@allFiles);
    @files = @allFiles;
}
# set file count for progress message
$progressMax = scalar @files if defined $progress;

# store duplicate database information under canonical filenames
my @dbKeys = keys %database;
if (@dbKeys and require Cwd) {
    foreach (@dbKeys) {
        my $canonFile = Cwd::abs_path($_);
        if (defined $canonFile) {
            $database{$canonFile} = $database{$_} unless $database{$canonFile};
        } else {
            # (may happen on Mac if the filename encoding is incorrect in the database)
            Warn "Error generating canonical filename for $_\n";
        }
    }
}

# process all specified files
ProcessFiles($mainTool);

if ($filtered and not $validFile) {
    Warn "No file with specified extension\n";
    $rtnVal = 1;
}

# print CSV information if necessary
PrintCSV() if $csv and not $isWriting;

# print file trailer if necessary
print $fileTrailer if $fileTrailer and not $textOut and not $fileHeader;

if (defined $deleteOrig) {

    # print summary and delete requested files
    unless ($quiet) {
        printf "%5d directories scanned\n", $countDir if $countDir;
        printf "%5d directories created\n", $countNewDir if $countNewDir;
        printf "%5d files failed condition\n", $countFailed if $countFailed;
        printf "%5d image files found\n", $count;
    }
    if (@delFiles) {
        # verify deletion unless "-delete_original!" was specified
        if ($deleteOrig == 1) {
            printf '%5d originals will be deleted!  Are you sure [y/n]? ', scalar(@delFiles);
            my $response = ;
            unless ($response =~ /^(y|yes)\s*$/i) {
                Warn "Originals not deleted.\n";
                next;
            }
        }
        $countGoodWr = unlink @delFiles;
        $countBad = scalar(@delFiles) - $countGoodWr;
    }
    if ($quiet) {
        # no more messages
    } elsif ($count and not $countGoodWr and not $countBad) {
        printf "%5d original files found\n", $countGoodWr;
    } elsif ($deleteOrig) {
        printf "%5d original files deleted\n", $countGoodWr if $count;
        printf "%5d originals not deleted due to errors\n", $countBad if $countBad;
    } else {
        printf "%5d image files restored from original\n", $countGoodWr if $count;
        printf "%5d files not restored due to errors\n", $countBad if $countBad;
    }

} elsif ((not $binaryStdout or $verbose) and not $quiet) {

    # print summary
    my $tot = $count + $countBad;
    my $totWr = $countGoodWr + $countBadWr + $countSameWr + $countCopyWr +
                $countGoodCr + $countBadCr;
    if ($countDir or $totWr or $countFailed or $tot > 1 or $textOut) {
        my $o = (($html or $json or $xml or %printFmt or $csv) and not $textOut) ? \*STDERR : $vout;
        printf($o "%5d directories scanned\n", $countDir) if $countDir;
        printf($o "%5d directories created\n", $countNewDir) if $countNewDir;
        printf($o "%5d files failed condition\n", $countFailed) if $countFailed;
        printf($o "%5d image files created\n", $countGoodCr) if $countGoodCr;
        printf($o "%5d image files updated\n", $countGoodWr) if $totWr - $countGoodCr - $countBadCr - $countCopyWr;
        printf($o "%5d image files unchanged\n", $countSameWr) if $countSameWr;
        printf($o "%5d image files %s\n", $countCopyWr, $overwriteOrig ? 'moved' : 'copied') if $countCopyWr;
        printf($o "%5d files weren't updated due to errors\n", $countBadWr) if $countBadWr;
        printf($o "%5d files weren't created due to errors\n", $countBadCr) if $countBadCr;
        printf($o "%5d image files read\n", $count) if $tot>1 or ($countDir and not $totWr);
        printf($o "%5d files could not be read\n", $countBad) if $countBad;
        printf($o "%5d output files created\n", scalar(keys %created)) if $textOut;
        printf($o "%5d output files appended\n", scalar(keys %appended)) if %appended;
    }
}

# set error status if we had any errors or if all files failed the "-if" condition
$rtnVal = 1 if $countBadWr or $countBadCr or $countBad or ($countFailed and not $count);

# last ditch effort to preserve filemodifydate
PreserveTime() if %preserveTime;

} # end "Command" loop ........................................................

close STAYOPEN if $stayOpen >= 2;

Exit $rtnVal;   # all done


#------------------------------------------------------------------------------
# Get image information from EXIF data in file (or write file if writing)
# Inputs: 0) ExifTool object reference, 1) file name
sub GetImageInfo($$)
{
    my ($exifTool, $orig) = @_;
    my (@foundTags, $info, $file, $ind);

    # determine the name of the source file based on the original input file name
    if (@srcFmt) {
        my ($fmt, $first);
        foreach $fmt (@srcFmt) {
            $file = $fmt eq '@' ? $orig : FilenameSPrintf($fmt, $orig);
            # use this file if it exists
            -e $file and undef($first), last;
            $verbose and print $vout "Source file $file does not exist\n";
            $first = $file unless defined $first;
        }
        $file = $first if defined $first;
    } else {
        $file = $orig;
    }

    my $pipe = $file;
    if ($doUnzip) {
        # pipe through gzip or bzip2 if necessary
        if ($file =~ /\.gz$/i) {
            $pipe = qq{gzip -dc "$file" |};
        } elsif ($file =~ /\.bz2$/i) {
            $pipe = qq{bzip2 -dc "$file" |};
        }
    }
    # evaluate -if expression for conditional processing
    if (defined $condition) {
        unless ($file eq '-' or -e $file) {
            Warn "File not found: $file\n";
            ++$countBad;
            return;
        }
        # catch run time errors as well as compile errors
        undef $evalWarning;
        local $SIG{'__WARN__'} = sub { $evalWarning = $_[0] };

        my %info;
        # extract information and build expression for evaluation
        my $opts = { Duplicates => 1, Verbose => 0, HtmlDump => 0 };
        # return all tags but explicitly mention tags on command line so
        # requested images will generate the appropriate warnings
        @foundTags = ('*', @tags) if @tags;
        $info = $exifTool->ImageInfo(Infile($pipe,$isWriting), \@foundTags, $opts);
        my $cond = $exifTool->InsertTagValues(\@foundTags, $condition, \%info);

        #### eval "-if" condition (%info)
        my $result = eval $cond;

        $@ and $evalWarning = $@;
        if ($evalWarning) {
            # fail condition if warning is issued
            undef $result;
            if ($verbose) {
                chomp $evalWarning;
                $evalWarning =~ s/ at \(eval .*//s;
                delete $SIG{'__WARN__'};
                Warn "Condition: $evalWarning - $file\n";
            }
        }
        unless ($result) {
            $verbose and print $vout "-------- $file (failed condition)$progStr\n";
            ++$countFailed;
            return;
        }
        # can't make use of $info if verbose because we must reprocess
        # the file anyway to generate the verbose output
        undef $info if $verbose;
    }
    if (defined $deleteOrig) {
        print $vout "======== $file$progStr\n" if defined $verbose;
        ++$count;
        my $original = "${file}_original";
        -e $original or return;
        if ($deleteOrig) {
            $verbose and print $vout "Scheduled for deletion: $original\n";
            push @delFiles, $original;
        } elsif (rename $original, $file) {
            $verbose and print $vout "Restored from $original\n";
            ++$countGoodWr;
        } else {
            Warn "Error renaming $original\n";
            ++$countBad;
        }
        return;
    }
    my $lineCount = 0;
    my ($fp, $outfile, $append);
    if ($textOut and $verbose and not $tagOut) {
        ($fp, $outfile, $append) = OpenOutputFile($orig);
        $fp or ++$countBad, return;
        # delete file if we exit prematurely (unless appending)
        $tmpText = $outfile unless $append;
        $exifTool->Options(TextOut => $fp);
    }

    if ($isWriting) {
        print $vout "======== $file$progStr\n" if defined $verbose;
        SetImageInfo($exifTool, $file, $orig);
        $info = $exifTool->GetInfo('Warning', 'Error');
        PrintErrors($exifTool, $info, $file);
        # close output text file if necessary
        if ($outfile) {
            undef $tmpText;
            close($fp);
            $exifTool->Options(TextOut => $vout);
            if ($info->{Error}) {
                unlink $outfile;    # erase bad file
            } elsif ($append) {
                $appended{$outfile} = 1 unless $created{$outfile};
            } else {
                $created{$outfile} = 1;
            }
        }
        return;
    }

    # extract information from this file
    unless ($file eq '-' or -e $file) {
        Warn "File not found: $file\n";
        $outfile and close($fp), undef($tmpText), unlink($outfile);
        ++$countBad;
        return;
    }
    # print file/progress message
    my $o;
    unless ($binaryOutput or $textOut or %printFmt or $html > 1 or $csv) {
        if ($html) {
            require Image::ExifTool::HTML;
            my $f = Image::ExifTool::HTML::EscapeHTML($file);
            print "\n";
        } elsif (not ($json or $xml)) {
            $o = \*STDOUT if ($multiFile and not $quiet) or $progress;
        }
    }
    $o = \*STDERR if $progress and not $o;
    $o and print $o "======== $file$progStr\n";
    if ($info) {
        # get the information we wanted
        if (@tags and not %printFmt) {
            @foundTags = @tags;
            $info = $exifTool->GetInfo(\@foundTags);
        }
    } else {
        # request specified tags unless using print format option
        my $oldDups = $exifTool->Options('Duplicates');
        if (%printFmt) {
            $exifTool->Options(Duplicates => 1);
            $exifTool->Options(RequestAll => 1);
        } else {
            @foundTags = @tags;
        }
        # extract the information
        $info = $exifTool->ImageInfo(Infile($pipe), \@foundTags);
        $exifTool->Options(Duplicates => $oldDups);
    }
    # all done now if we already wrote output text file (ie. verbose option)
    if ($fp) {
        if ($outfile) {
            $exifTool->Options(TextOut => \*STDOUT);
            undef $tmpText;
            if ($info->{Error}) {
                close($fp);
                unlink $outfile;    # erase bad file
            } else {
                ++$lineCount;       # output text file (likely) is not empty
            }
        }
        if ($info->{Error}) {
            Warn "Error: $info->{Error} - $file\n";
            ++$countBad;
            return;
        }
    }

    # print warnings to stderr if using binary output
    # (because we are likely ignoring them and piping stdout to file)
    # or if there is none of the requested information available
    if ($binaryOutput or not %$info) {
        my $errs = $exifTool->GetInfo('Warning', 'Error');
        PrintErrors($exifTool, $errs, $file) and $rtnVal = 1;
    } elsif ($exifTool->GetValue('Error')) {
        $rtnVal = 1;
    }

    # open output file (or stdout if no output file) if not done already
    unless ($outfile or $tagOut) {
        ($fp, $outfile, $append) = OpenOutputFile($orig);
        $fp or ++$countBad, return;
        $tmpText = $outfile unless $append;
    }

    # print the results for this file
    if (%printFmt) {
        # output using print format file (-p) option
        my ($type, $doc, $grp);
        undef $fileTrailer;
        # repeat for each embedded document if necessary
        my $lastDoc = $exifTool->Options('ExtractEmbedded') ? $exifTool->{DOC_COUNT} : 0;
        foreach $type ('HEAD', 'BODY', 'TAIL') {
            my $prf = $printFmt{$type} or next;
            for ($doc=0; $doc<=$lastDoc; ++$doc) {
                if ($lastDoc) {
                    if ($doc) {
                        last unless $type eq 'BODY'; # only repeat BODY lines
                        $grp = "Doc$doc:";
                    } else {
                        $grp = 'Main:';
                    }
                    # change tag groups to print next document by adding "Main:" or "Doc#:"
                    # to all tags which don't already start with a family 3 group name
                    $prf = [ @{$printFmt{$type}} ];
                    s/((^|[^\$])(\$\$)*\$\{?)((?!(Main|Doc\d+):)[\w])/$1$grp$4/ig foreach @$prf;
                }
                my @lines;
                foreach (@$prf) {
                    my $line = $exifTool->InsertTagValues(\@foundTags, $_, 'Warn');
                    push @lines, $line if defined $line;
                }
                $lineCount += scalar @lines;
                if ($type eq 'TAIL') {
                    $fileTrailer = '' unless defined $fileTrailer;
                    $fileTrailer .= join '', @lines;
                } elsif (@lines) {
                    print $fp @lines;
                }
            }
        }
        delete $printFmt{HEAD} unless $outfile; # print header only once per output file
        my $errs = $exifTool->GetInfo('Warning', 'Error');
        PrintErrors($exifTool, $errs, $file);
    } elsif (not $disableOutput) {
        my ($tag, $line, %noDups, %csvInfo, $bra, $ket, $sep);
        if ($fp) {
            # print file header (only once)
            if ($fileHeader) {
                print $fp $fileHeader;
                undef $fileHeader unless $textOut;
            }
            if ($html) {
                print $fp "\n";
            } elsif ($xml) {
                my $f = $file;
                CleanXML(\$f);
                print $fp "\nGetGroup($tag);
                    unless ($grp1) {
                        next unless defined $forcePrint;
                        $grp = $grp1 = 'Unknown';
                    }
                    next if $groups{$grp1};
                    $groups{$grp1} = 1;
                    # include family 0 and 1 groups in URI except for internal tags
                    # (this will put internal tags in the "XML" group on readback)
                    $grp .= "/$grp1" unless $grp eq $grp1 and
                                            $grp =~ /^(ExifTool|File|Composite|Unknown)$/;
                    print $fp "\n  xmlns:$grp1='http://ns.exiftool.ca/$grp/1.0/'";
                }
                print $fp '>' if $outFormat < 1; # finish rdf:Description token unless short format
                $ind = $outFormat >= 0 ? ' ' : '   ';
            } elsif ($json) {
                # set delimiters for JSON or PHP output
                ($bra, $ket, $sep) = $json == 1 ? ('{','}',':') : ('Array(',')',' =>');
                print $fp ",\n" if $comma;
                print $fp qq($bra\n  "SourceFile"$sep ), EscapeJSON($file, 1);
                $comma = 1;
                $ind = (defined $showGroup and not $allGroup) ? '    ' : '  ';
            } elsif ($csv) {
                $database{$file} = \%csvInfo;
                push @csvFiles, $file;
            }
        }
        # suppress duplicates manually in JSON and short XML output
        my $noDups = ($json or ($xml and $outFormat > 0));
        my $printConv = $mainTool->Options('PrintConv');
        my $lastGroup = '';
TAG:    foreach $tag (@foundTags) {
            my $tagName = GetTagName($tag);
            my ($group, $valList);
            # make sure this tag has a value
            my $val = $$info{$tag};
            if (ref $val) {
                if (defined $binaryOutput and not $binaryOutput) { # happens with -X -b or -php -b
                    # avoid extracting Protected binary tags (ie. data blocks) [insider information]
                    next if $exifTool->{TAG_INFO}{$tag}{Protected};
                }
                $val = ConvertBinary($val); # convert SCALAR references
                if ($structOpt) {
                    # serialize structure if necessary
                    $val = Image::ExifTool::XMP::SerializeStruct($val) unless $xml or $json;
                } elsif (ref $val eq 'ARRAY') {
                    if (defined $listItem) {
                        # take only the specified item
                        $val = $$val[$listItem];
                    # join arrays of simple values (with newlines for binary output)
                    } elsif ($binaryOutput) {
                        if ($tagOut) {
                            $valList = $val;
                            $val = shift @$valList;
                        } else {
                            $val = join "\n", @$val;
                        }
                    } elsif ($joinLists) {
                        $val = join $listSep, @$val;
                    }
                }
            }
            if (not defined $val) {
                # ignore tags that weren't found unless necessary
                next if $binaryOutput;
                if (defined $forcePrint) {
                    $val = $forcePrint; # forced to print all tag values
                } elsif (not $csv) {
                    next;
                }
            }
            if (defined $showGroup) {
                $group = $exifTool->GetGroup($tag, $showGroup);
                # look ahead to see if this tag may suppress a priority tag in
                # the same group, and if so suppress this tag instead
                next if $noDups and $tag =~ /^(.*?) ?\(/ and defined $$info{$1} and
                        $group eq $exifTool->GetGroup($1, $showGroup);
                $group = 'Unknown' if not $group and ($xml or $json);
                if ($fp and not ($allGroup or $csv)) {
                    if ($lastGroup ne $group) {
                        if ($html) {
                            my $cols = 1;
                            ++$cols if $outFormat==0 or $outFormat==1;
                            ++$cols if $showTagID;
                            print $fp "\n";
                        } elsif ($json) {
                            print $fp "\n  $ket" if $lastGroup;
                            print $fp ',' if $lastGroup or $comma;
                            print $fp qq(\n  "$group"$sep $bra);
                            undef $comma;
                            undef %noDups;  # allow duplicate names in different groups
                        } else {
                            print $fp "---- $group ----\n";
                        }
                        $lastGroup = $group;
                    }
                    undef $group;   # undefine so we don't print it below
                }
            }

            ++$lineCount;           # we are printing something meaningful

            # loop through list values when -b -W used
            for (;;) {
                if ($tagOut) {
                    # determine suggested extension for output file
                    my $ext = SuggestedExtension($exifTool, \$val, $tagName);
                    if (%wext and ($wext{$ext} || $wext{'*'} || -1) < 0) {
                        if ($verbose and $verbose > 1) {
                            print $vout "Not writing $ext output file for $tagName\n";
                        }
                        next TAG;
                    }
                    my @groups = $mainTool->GetGroup($tag);
                    $outfile and close($fp), undef($tmpText); # (shouldn't happen)
                    ($fp, $outfile, $append) = OpenOutputFile($orig, $tagName, \@groups, $ext);
                    $fp or ++$countBad, next TAG;
                    $tmpText = $outfile unless $append;
                }
                # write binary output
                if ($binaryOutput) {
                    print $fp $val;
                    if ($tagOut) {
                        if ($append) {
                            $appended{$outfile} = 1 unless $created{$outfile};
                        } else {
                            $created{$outfile} = 1;
                        }
                        close($fp);
                        undef $tmpText;
                        $verbose and print $vout "Wrote $tagName to $outfile\n";
                        undef $outfile;
                        undef $fp;
                        next TAG unless $valList and @$valList;
                        $val = shift @$valList;
                        next; # loop over values of List tag
                    }
                    next TAG;
                }
                last;
            }
            # save information for CSV output
            if ($csv) {
                my $t = $group ? "$group:$tagName" : $tagName;
                $t .= '#' if $tag =~ /#/;   # add ValueConv "#" suffix if used
                # (tag-name case may be different if some tags don't exist
                # in a file, so all logic must use lower-case tag names)
                my $lcTag = lc $t;
                # override existing entry only if top priority
                next if defined $csvInfo{$lcTag} and $tag =~ /\(/;
                $csvInfo{$lcTag} = $val;
                if (defined $csvTags{$lcTag}) {
                    # overwrite with actual extracted tag name
                    # (note: can't check "if defined $val" here because -f may be used)
                    $csvTags{$lcTag} = $t if defined $$info{$tag};
                } else {
                    # (don't save unextracted tag name unless -f was used)
                    $csvTags{$lcTag} = defined($val) ? $t : '';
                    if (@csvFiles == 1) {
                        push @csvTags, $lcTag; # save order of tags for first file
                    } elsif (@csvTags) {
                        undef @csvTags;
                    }
                }
                next;
            }

            # get description if we need it (use tag name if $outFormat > 0)
            my $desc = $outFormat > 0 ? $tagName : $exifTool->GetDescription($tag);

            if ($xml) {
                # RDF/XML output format
                my $tok = "$group:$tagName";
                # manually un-do CR/LF conversion in Windows because output
                # is in text mode, which will re-convert newlines to CR/LF
                $isCRLF and $val =~ s/\x0d\x0a/\x0a/g;
                if ($outFormat > 0) {
                    if ($structOpt and ref $val) {
                        $val = Image::ExifTool::XMP::SerializeStruct($val);
                    }
                    if ($escapeHTML) {
                        $val =~ tr/\0-\x08\x0b\x0c\x0e-\x1f/./;
                        Image::ExifTool::XMP::FixUTF8(\$val) if $utf8;
                        $val = Image::ExifTool::HTML::EscapeHTML($val);
                    } else {
                        CleanXML(\$val);
                    }
                    unless ($noDups{$tok}) {
                        print $fp "\n $tok='$val'";
                        # XML does not allow duplicate attributes
                        $noDups{$tok} = 1;
                    }
                    next;
                }
                my ($xtra, $valNum, $descClose);
                if ($showTagID) {
                    my ($id, $lang) = $exifTool->GetTagID($tag);
                    if ($id =~ /^\d+$/) {
                        $id = sprintf("0x%.4x", $id) if $showTagID eq 'H';
                    } else {
                        $id = Image::ExifTool::XMP::FullEscapeXML($id);
                    }
                    $xtra = " et:id='$id'";
                    $xtra .= " xml:lang='$lang'" if $lang;
                } else {
                    $xtra = '';
                }
                if ($tabFormat) {
                    my $table = $exifTool->GetTableName($tag);
                    my $index = $exifTool->GetTagIndex($tag);
                    $xtra .= " et:table='$table'";
                    $xtra .= " et:index='$index'" if defined $index;
                }
                my $lastVal = $val;
                for ($valNum=0; $valNum<2; ++$valNum) {
                    $val = FormatXML($val, $ind, $group);
                    if ($outFormat >= 0) {
                        # normal output format (note: this will give
                        # non-standard RDF/XML if there are any attributes)
                        print $fp "\n <$tok$xtra$val";
                        last;
                    } elsif ($valNum == 0) {
                        CleanXML(\$desc);
                        if ($xtra) {
                            print $fp "\n <$tok>";
                            print $fp "\n  ";
                            $descClose = "\n  ";
                        } else {
                            print $fp "\n <$tok rdf:parseType='Resource'>";
                            $descClose = '';
                        }
                        # print tag Description
                        print $fp "\n   $desc";
                        if ($printConv) {
                            # print PrintConv value
                            print $fp "\n   ";
                            $val = $exifTool->GetValue($tag, 'ValueConv');
                            $val = '' unless defined $val;
                            # go back to print ValueConv value only if different
                            next unless IsEqual($val, $lastVal);
                            print $fp "$descClose\n ";
                            last;
                        }
                    }
                    # print ValueConv value
                    print $fp "\n   ";
                    print $fp "$descClose\n ";
                    last;
                }
                next;
            } elsif ($json) {
                # JSON or PHP output format
                my $tok = $allGroup ? "$group:$tagName" : $tagName;
                # (removed due to backward incompatibility)
                # $tok .= '#' if $tag =~ /#/; # add back '#' suffix if used
                next if $noDups{$tok};
                $noDups{$tok} = 1;
                print $fp ',' if $comma;
                print $fp qq(\n$ind"$tok"$sep );
                if ($showTagID or $outFormat < 0) {
                    $val = { val => $val };
                    if ($showTagID) {
                        my $id = $exifTool->GetTagID($tag);
                        $id = sprintf('0x%.4x', $id) if $showTagID eq 'H' and $id =~ /^\d+$/;
                        $$val{id} = $id;
                    }
                    if ($outFormat < 0) {
                        $$val{desc} = $desc;
                        if ($printConv) {
                            my $num = $exifTool->GetValue($tag, 'ValueConv');
                            $$val{num} = $num unless IsEqual($num, $$val{val});
                        }
                    }
                }
                FormatJSON($fp, $val, $ind);
                $comma = 1;
                next;
            }
            my $id;
            if ($showTagID) {
                $id = $exifTool->GetTagID($tag);
                if ($id =~ /^(\d+)(\.\d+)?$/) { # only print numeric ID's
                    $id = sprintf("0x%.4x", $1) if $showTagID eq 'H';
                } else {
                    $id = '-';
                }
            }

            # translate unprintable chars in value and remove trailing spaces
            $val =~ tr/\x01-\x1f\x7f/./;
            $val =~ s/\x00//g;
            $val =~ s/\s+$//;

            if ($html) {
                print $fp "";
                print $fp "" if defined $group;
                print $fp "" if $showTagID;
                print $fp "" if $outFormat <= 1;
                print $fp "\n";
            } else {
                my $buff = '';
                if ($tabFormat) {
                    $buff = "$group\t" if defined $group;
                    $buff .= "$id\t" if $showTagID;
                    if ($outFormat <= 1) {
                        $buff .= "$desc\t$val\n";
                    } elsif (defined $line) {
                        $line .= "\t$val";
                    } else {
                        $line = $val;
                    }
                } elsif ($outFormat < 0) {    # long format
                    $buff = "[$group] " if defined $group;
                    $buff .= "$id " if $showTagID;
                    $buff .= "$desc\n      $val\n";
                } elsif ($outFormat == 0 or $outFormat == 1) {
                    my $wid;
                    my $len = 0;
                    if (defined $group) {
                        $buff = sprintf("%-15s ", "[$group]");
                        $len = 16;
                    }
                    if ($showTagID) {
                        $wid = ($showTagID eq 'D') ? 5 : 6;
                        $len += $wid + 1;
                        ($wid = $len - length($buff) - 1) < 1 and $wid = 1;
                        $buff .= sprintf "%${wid}s ", $id;
                    }
                    $wid = 32 - (length($buff) - $len);
                    # pad description to a constant length
                    # (get actual character length when using alternate languages
                    # because these descriptions may contain UTF8-encoded characters)
                    my $padLen = $wid - length($fixLen ? Encode::decode_utf8($desc) : $desc);
                    $padLen = 0 if $padLen < 0;
                    $buff .= $desc . (' ' x $padLen) . ": $val\n";
                } elsif ($outFormat == 2) {
                    $buff = "[$group] " if defined $group;
                    $buff .= "$id " if $showTagID;
                    $buff .= "$tagName: $val\n";
                } elsif ($argFormat) {
                    $buff = '-';
                    $buff .= "$group:" if defined $group;
                    $tagName .= '#' if $tag =~ /#/; # add '#' suffix if used
                    $buff .= "$tagName=$val\n";
                } else {
                    $buff = "$group " if defined $group;
                    $buff .= "$id " if $showTagID;
                    $buff .= "$val\n";
                }
                print $fp $buff;
            }
            if ($tagOut) {
                if ($append) {
                    $appended{$outfile} = 1 unless $created{$outfile};
                } else {
                    $created{$outfile} = 1;
                }
                close($fp);
                undef $tmpText;
                $verbose and print $vout "Wrote $tagName to $outfile\n";
                undef $outfile;
                undef $fp;
            }
        }
        if ($fp) {
            if ($html) {
                print $fp "
$group
$group$id$desc$val
\n"; } elsif ($xml) { # close rdf:Description element print $fp $outFormat < 1 ? "\n\n" : "/>\n"; } elsif ($json) { print $fp "\n $ket" if $lastGroup; print $fp "\n$ket"; $comma = 1; } elsif ($tabFormat and $outFormat > 1) { print $fp "$line\n" if defined $line; } } } if ($outfile) { print $fp $fileTrailer if $fileTrailer; # write file trailer close($fp); undef $tmpText; if ($lineCount) { if ($append) { $appended{$outfile} = 1 unless $created{$outfile}; } else { $created{$outfile} = 1; } } else { unlink $outfile unless $append; # don't keep empty output files } undef $comma; } ++$count; } #------------------------------------------------------------------------------ # Set information in file # Inputs: 0) ExifTool object reference, 1) source file name # 2) original source file name ('' to create from scratch) # Returns: true on success sub SetImageInfo($$$) { my ($exifTool, $file, $orig) = @_; my ($outfile, $restored, $isTemporary, $isStdout, $outType, $tagsFromSrc); my $infile = $file; # save infile in case we change it again undef $tmpFile; # make sure this isn't defined # clear any existing errors or warnings since we check these on return delete $exifTool->{VALUE}->{Error}; delete $exifTool->{VALUE}->{Warning}; # first, try to determine our output file name so we can return quickly # if it already exists (note: this test must be delayed until after we # set tags from dynamic files if writing FileName or Directory) if (defined $outOpt) { if ($outOpt =~ /^-(\.\w+)?$/) { # allow output file type to be specified with "-o -.EXT" $outType = GetFileType($outOpt) if $1; $outfile = '-'; $isStdout = 1; } else { $outfile = FilenameSPrintf($outOpt, $orig); if ($outfile eq '') { Warn "Can't create file with zero-length name from $orig\n"; ++$countBadCr; return 0; } } if (not $isStdout and (-d $outfile or $outfile =~ /\/$/)) { $outfile .= '/' unless $outfile =~ /\/$/; my $name = $file; $name =~ s/^.*\///s; # remove directory name $outfile .= $name; } else { my $srcType = GetFileType($file) || ''; $outType or $outType = GetFileType($outfile); if ($outType and ($srcType ne $outType or $outType eq 'ICC')) { unless (CanCreate($outType)) { my $what = $srcType ? 'other types' : 'scratch'; WarnOnce "Error: Can't create $outType files from $what\n"; ++$countBadCr; return 0; } if ($file ne '') { # restore previous new values unless done already $exifTool->RestoreNewValues() unless $restored; $restored = 1; # translate to this type by setting specified tags from file my @setTags = @tags; foreach (@exclude) { push @setTags, "-$_"; } # force some tags to be copied for certain file types my %forceCopy = ( ICC => 'ICC_Profile', VRD => 'CanonVRD', ); push @setTags, $forceCopy{$outType} if $forceCopy{$outType}; # assume "-tagsFromFile @" unless -tagsFromFile already specified # (%setTags won't be empty if -tagsFromFile used) if (not %setTags or (@setTags and not $setTags{'@'})) { return 0 unless DoSetFromFile($exifTool, $file, \@setTags); } elsif (@setTags) { # add orphaned tags to existing "-tagsFromFile @" for this file only push @setTags, @{$setTags{'@'}}; $tagsFromSrc = \@setTags; } # all done with source file -- create from meta information alone $file = ''; } } } unless ($isStdout) { $outfile = NextUnusedFilename($outfile); if (-e $outfile and not $doSetFileName) { Warn "Error: '$outfile' already exists - $infile\n"; ++$countBadWr; return 0; } } } elsif ($file eq '-') { $isStdout = 1; } # set tags from destination file if required if (@dynamicFiles) { # restore previous values if necessary $exifTool->RestoreNewValues() unless $restored; my ($dyFile, %setTagsIndex); foreach $dyFile (@dynamicFiles) { if (not ref $dyFile) { my ($fromFile, $setTags); if ($dyFile eq '@') { $fromFile = $orig; $setTags = $tagsFromSrc || $setTags{$dyFile}; } else { $fromFile = FilenameSPrintf($dyFile, $orig); ++$countBadWr, return 0 unless defined $fromFile; $setTags = $setTags{$dyFile}; } # do we have multiple -tagsFromFile options with this file? if ($setTagsList{$dyFile}) { # use the tags set in the i-th occurrence my $i = $setTagsIndex{$dyFile} || 0; $setTagsIndex{$dyFile} = $i + 1; $setTags = $setTagsList{$dyFile}[$i] if $setTagsList{$dyFile}[$i]; } # set new values values from file return 0 unless DoSetFromFile($exifTool, $fromFile, $setTags); } elsif (ref $dyFile eq 'ARRAY') { # a dynamic file containing a simple tag value my $fname = FilenameSPrintf($$dyFile[1], $orig); my ($buff, $rtn, $wrn); my $opts = $$dyFile[2]; if (defined $fname and SlurpFile($fname, \$buff)) { $verbose and print $vout "Reading $$dyFile[0] from $fname\n"; ($rtn, $wrn) = $mainTool->SetNewValue($$dyFile[0], $buff, %$opts); $wrn and Warn "$wrn\n"; } # remove this tag if we couldn't set it properly $rtn or $mainTool->SetNewValue($$dyFile[0], undef, Replace => 2, ProtectSaved => $$opts{ProtectSaved}); next; } elsif (ref $dyFile eq 'SCALAR') { # set new values from CSV database my ($f, $found, $tag); # read tags for SourceFile '*' plus the specific file foreach $f ('*', $file) { my $csvInfo = $database{$f}; unless ($csvInfo) { next if $f eq '*' or not require Cwd; # check canonical file name $csvInfo = $database{Cwd::abs_path($f)} or next; } $found = 1; $verbose and print $vout "Setting new values from $csv database\n"; foreach $tag (sort keys %$csvInfo) { next if $tag =~ /\b(SourceFile|Directory|FileName)$/i; # don't write these my ($rtn, $wrn) = $mainTool->SetNewValue($tag, $$csvInfo{$tag}, Protected => 1, AddValue => $csvAdd, ProtectSaved => $csvSaveCount); $wrn and Warn "$wrn\n" if $verbose; } } unless ($found) { Warn("No SourceFile '$file' in imported $csv database\n"); if (require Cwd) { my $cFile = Cwd::abs_path($file); Warn("(canonical path: '$cFile')\n"); } return 0; } } } } if ($isStdout) { # write to STDOUT $outfile = \*STDOUT; unless ($binaryStdout) { binmode(STDOUT); $binaryStdout = 1; } } else { # determine what our output file name should be my $newFileName = $exifTool->GetNewValues('FileName'); my $newDir = $exifTool->GetNewValues('Directory'); if (defined $newFileName or defined $newDir or ($doSetFileName and defined $outfile)) { if ($newFileName) { $newFileName = FilenameSPrintf($newFileName, $orig); if (defined $outfile) { $outfile = Image::ExifTool::GetNewFileName($orig, $outfile) if $orig ne ''; $outfile = Image::ExifTool::GetNewFileName($outfile, $newFileName); } elsif ($orig ne '') { $outfile = Image::ExifTool::GetNewFileName($orig, $newFileName); } } if ($newDir) { $newDir = FilenameSPrintf($newDir, $orig); $outfile = Image::ExifTool::GetNewFileName(defined $outfile ? $outfile : $orig, $newDir); } $outfile = NextUnusedFilename($outfile, $infile); if (-e $outfile) { if ($infile ne $outfile) { Warn "Error: '$outfile' already exists - $infile\n"; ++$countBadWr; return 0; } undef $outfile; # not changing the file name after all } } if (defined $outfile) { $verbose and print $vout "'$infile' --> '$outfile'\n"; # create output directory if necessary CreateDirectory($outfile); # set temporary file (automatically erased on abnormal exit) $tmpFile = $outfile if defined $outOpt; } unless (defined $tmpFile) { # count the number of tags and pseudo-tags we are writing my ($numSet, $numPseudo) = $exifTool->CountNewValues(); if (-e $file) { unless ($numSet) { # no need to write if no tags set print $vout "Nothing changed in $file\n" if defined $verbose; ++$countSameWr; return 1; } } elsif (CanCreate($file)) { if ($numSet == $numPseudo) { # no need to write if no real tags Warn("Error: Nothing to write - $file\n"); ++$countBadWr; return 0; } unless (defined $outfile) { # create file from scratch $outfile = $file; $file = ''; } } else { # file doesn't exist, and we can't create it Warn("Error: File not found - $file\n"); ++$countBadWr; return 0; } # quickly rename file and/or set file date if this is all we are doing if ($numSet == $numPseudo) { my $r1 = $exifTool->SetFileModifyDate($file,undef,'FileCreateDate'); my $r2 = $exifTool->SetFileModifyDate($file); my $r3 = 0; $r3 = $exifTool->SetFileName($file, $outfile) if defined $outfile; if ($r1 > 0 or $r2 > 0 or $r3 > 0) { ++$countGoodWr; } elsif ($r1 < 0 or $r2 < 0 or $r3 < 0) { ++$countBadWr; return 0; } else { ++$countSameWr; } return 1; } unless (defined $outfile) { # write to a truly temporary file $outfile = "${file}_exiftool_tmp"; if (-e $outfile) { Warn("Error: Temporary file already exists: $outfile\n"); ++$countBadWr; return 0; } $isTemporary = 1; } # new output file is temporary until we know it has been written properly $tmpFile = $outfile; } } # rewrite the file my $success = $exifTool->WriteInfo(Infile($file), $outfile, $outType); # get file time if preserving it my ($aTime, $mTime, $cTime); if ($preserveTime and $success and -e $file) { $aTime = $^T - (-A _) * (24 * 3600); $mTime = $^T - (-M _) * (24 * 3600); $cTime = $^T - (-C _) * (24 * 3600); } if ($success == 1) { # preserve the original file times if (defined $tmpFile) { if (-e $file) { SetFileTime($tmpFile, $aTime, $mTime, $cTime) if $preserveTime; if ($isTemporary) { # preserve original file attributes if possible Image::ExifTool::CopyFileAttrs($file, $outfile); # move original out of the way my $original = "${file}_original"; if (not $overwriteOrig and not -e $original) { # rename the file and check again to be sure the file doesn't exist # (in case, say, the filesystem truncated the file extension) if (not Rename($file, $original) or -e $file) { Error "Error renaming $file\n"; return 0; } } if ($overwriteOrig > 1) { # copy temporary file over top of original to preserve attributes my ($err, $buff); my $newFile = $tmpFile; Open(\*NEW_FILE, $newFile) or Error("Error opening $newFile\n"), return 0; binmode(NEW_FILE); #.......................................................... # temporarily disable CTRL-C during this critical operation $critical = 1; undef $tmpFile; # handle deletion of temporary file ourself if (Open(\*ORIG_FILE, $file, '>')) { binmode(ORIG_FILE); while (read(NEW_FILE, $buff, 65536)) { print ORIG_FILE $buff or $err = 1; } close(NEW_FILE); close(ORIG_FILE) or $err = 1; if ($err) { Warn "Couldn't overwrite in place - $file\n"; unless (rename($newFile, $file) or (unlink $file and Rename($newFile, $file))) { Error("Error renaming $newFile to $file\n"), return 0; } } else { unlink $newFile; if ($preserveTime) { SetFileTime($file, $aTime, $mTime, $cTime); # save time to set it later again to patch OS X 10.6 bug $preserveTime{$file} = [ $aTime, $mTime, $cTime ]; } } ++$countGoodWr; } else { close(NEW_FILE); Warn "Error opening $file for writing\n"; unlink $newFile; ++$countBadWr; } undef $critical; # end critical section SigInt() if $interrupted; # issue delayed SIGINT if necessary #.......................................................... # simply rename temporary file to replace original # (if we didn't already rename it to add "_original") } elsif (Rename($tmpFile, $file)) { ++$countGoodWr; } else { my $newFile = $tmpFile; undef $tmpFile; # (avoid deleting file if we get interrupted) # unlink may fail if already renamed or no permission if (not unlink($file)) { Warn "Error renaming temporary file to $file\n"; unlink $newFile; ++$countBadWr; # try renaming again now that the target has been deleted } elsif (not Rename($newFile, $file)) { Warn "Error renaming temporary file to $file\n"; # (don't delete tmp file now because it is all we have left) ++$countBadWr; } else { ++$countGoodWr; } } } elsif ($overwriteOrig) { # erase original file unlink $file or Warn "Error erasing original $file\n"; ++$countGoodWr; } else { ++$countGoodCr; } } else { # this file was created from scratch, not edited ++$countGoodCr; } } else { ++$countGoodWr; } } elsif ($success) { if ($isTemporary) { # just erase the temporary file since no changes were made unlink $tmpFile; ++$countSameWr; } else { SetFileTime($outfile, $aTime, $mTime, $cTime) if defined $aTime; if ($overwriteOrig) { unlink $file or Warn "Error erasing original $file\n"; } ++$countCopyWr; } print $vout "Nothing changed in $file\n" if defined $verbose; } else { unlink $tmpFile if defined $tmpFile; ++$countBadWr; } undef $tmpFile; return $success; } #------------------------------------------------------------------------------ # Clean string for XML (also removes invalid control chars and malformed UTF-8) # Inputs: 0) string ref # Returns: nothing, but input string is escaped sub CleanXML($) { my $strPt = shift; # translate control characters that are invalid in XML $$strPt =~ tr/\0-\x08\x0b\x0c\x0e-\x1f/./; # fix malformed UTF-8 characters Image::ExifTool::XMP::FixUTF8($strPt) if $utf8; # escape necessary characters for XML $$strPt = Image::ExifTool::XMP::EscapeXML($$strPt); } #------------------------------------------------------------------------------ # Encode string for XML # Inputs: 0) string ref # Returns: encoding used (and input string is translated) sub EncodeXML($) { my $strPt = shift; if ($$strPt =~ /[\0-\x08\x0b\x0c\x0e-\x1f]/ or ($utf8 and Image::ExifTool::XMP::IsUTF8($strPt) < 0)) { # encode binary data and non-UTF8 with special characters as base64 $$strPt = Image::ExifTool::XMP::EncodeBase64($$strPt); return 'http://www.w3.org/2001/XMLSchema#base64Binary'; #ATV } elsif ($escapeHTML) { $$strPt = Image::ExifTool::HTML::EscapeHTML($$strPt); } else { $$strPt = Image::ExifTool::XMP::EscapeXML($$strPt); } return ''; # not encoded } #------------------------------------------------------------------------------ # Format value for XML output # Inputs: 0) value, 1) indentation, 2) group # Returns: formatted value sub FormatXML($$$) { local $_; my ($val, $ind, $grp) = @_; my $gt = '>'; if (ref $val eq 'ARRAY') { # convert ARRAY into an rdf:Bag my $val2 = "\n$ind "; foreach (@$val) { $val2 .= "\n$ind "; } $val = "$val2\n$ind \n$ind"; } elsif (ref $val eq 'HASH') { $gt = " rdf:parseType='Resource'>"; my $val2 = ''; foreach (sort keys %$val) { my $tok = $grp . ':' . $_; $val2 .= "\n$ind <$tok" . FormatXML($$val{$_}, "$ind ", $grp) . ""; } $val = "$val2\n$ind"; } else { # (note: SCALAR reference should have already been converted) my $enc = EncodeXML(\$val); $gt = " rdf:datatype='$enc'>\n" if $enc; #ATV } return $gt . $val; } #------------------------------------------------------------------------------ # Escape string for JSON or PHP # Inputs: 0) string, 1) flag to force numbers to be quoted too # Returns: Escaped string (quoted if necessary) sub EscapeJSON($;$) { my ($str, $quote) = @_; unless ($quote) { # JSON boolean (true or false) return lc($str) if $str =~ /^(true|false)$/i and $json < 2; # JSON/PHP number (see json.org for numerical format) # return $str if $str =~ /^-?(\d|[1-9]\d+)(\.\d+)?(e[-+]?\d+)?$/i; # (these big numbers caused problems for some JSON parsers, so be more conservative) return $str if $str =~ /^-?(\d|[1-9]\d{1,14})(\.\d{1,16})?(e[-+]?\d{1,3})?$/i; } # escape special characters $str =~ s/(["\t\n\r\\])/\\$jsonChar{$1}/sg; if ($json < 2) { # JSON # escape other control characters with \u $str =~ s/([\0-\x1f])/sprintf("\\u%.4X",ord $1)/sge; # JSON strings must be valid UTF8 Image::ExifTool::XMP::FixUTF8(\$str) if $utf8; } else { # PHP # must escape "$" too for PHP $str =~ s/\$/\\\$/sg; # escape other control characters with \x $str =~ s/([\0-\x1f])/sprintf("\\x%.2X",ord $1)/sge; } return '"' . $str . '"'; # return the quoted string } #------------------------------------------------------------------------------ # Print JSON or PHP value # Inputs: 0) file reference, 1) value, 2) indentation sub FormatJSON($$$) { local $_; my ($fp, $val, $ind) = @_; my $comma; if (not ref $val) { print $fp EscapeJSON($val); } elsif (ref $val eq 'ARRAY') { if ($joinLists and not ref $$val[0]) { print $fp EscapeJSON(join $listSep, @$val); } else { my ($bra, $ket) = $json == 1 ? ('[',']') : ('Array(',')'); print $fp $bra; foreach (@$val) { print $fp ',' if $comma; FormatJSON($fp, $_, $ind); $comma = 1, } print $fp $ket, } } elsif (ref $val eq 'HASH') { my ($bra, $ket, $sep) = $json == 1 ? ('{','}',':') : ('Array(',')',' =>'); print $fp $bra; foreach (sort keys %$val) { print $fp ',' if $comma; print $fp qq(\n$ind "$_"$sep ); # hack to force decimal id's to be printed as strings with -H if ($showTagID and $_ eq 'id' and $showTagID eq 'H' and $$val{$_} =~ /^\d+\.\d+$/) { print $fp qq{"$$val{$_}"}; } else { FormatJSON($fp, $$val{$_}, "$ind "); } $comma = 1, } print $fp "\n$ind$ket", } else { # (note: SCALAR reference should have already been converted) print $fp '""'; } } #------------------------------------------------------------------------------ # Format value for CSV file # Inputs: value # Returns: value quoted if necessary sub FormatCSV($) { my $val = shift; $val = qq{"$val"} if $val =~ s/"/""/g or $val =~ /(^\s+|\s+$)/ or $val =~ /[,\n\r]/; return $val; } #------------------------------------------------------------------------------ # Print accumulated CSV information sub PrintCSV() { my ($file, $lcTag, @tags); @csvTags or @csvTags = sort keys %csvTags; # make a list of tags actually found foreach $lcTag (@csvTags) { push @tags, $csvTags{$lcTag} if $csvTags{$lcTag}; } print join(',', 'SourceFile', @tags), "\n"; my $empty = defined($forcePrint) ? $forcePrint : ''; foreach $file (@csvFiles) { my @vals = (FormatCSV($file)); # start with full file name my $csvInfo = $database{$file}; foreach $lcTag (@csvTags) { next unless $csvTags{$lcTag}; my $val = $$csvInfo{$lcTag}; defined $val or push(@vals,$empty), next; push @vals, FormatCSV($val); } print join(',', @vals), "\n"; } } #------------------------------------------------------------------------------ # Convert binary data (SCALAR references) for printing # Inputs: 0) object reference # Returns: converted object sub ConvertBinary($) { my $obj = shift; my ($key, $val); if (ref $obj eq 'HASH') { foreach $key (keys %$obj) { $$obj{$key} = ConvertBinary($$obj{$key}) if ref $$obj{$key}; } } elsif (ref $obj eq 'ARRAY') { foreach $val (@$obj) { $val = ConvertBinary($val) if ref $val; } } elsif (ref $obj eq 'SCALAR') { # (binaryOutput flag is set to 0 for binary mode of XML/PHP output formats) if (defined $binaryOutput) { $obj = $$obj; } else { # (-b is not valid for JSON or HTML output) my $bOpt = ($json==1 or $html) ? '' : ', use -b option to extract'; if ($$obj =~ /^Binary data/) { $obj = "($$obj$bOpt)"; } else { $obj = '(Binary data ' . length($$obj) . " bytes$bOpt)"; } } } return $obj; } #------------------------------------------------------------------------------ # Compare two tag values to see if they are equal # Inputs: 0) value1, 1) value2 # Returns: true if they are equal sub IsEqual($$) { return 1 if ref $_[0] eq 'SCALAR' or $_[0] eq $_[1]; return 0 if ref $_[0] ne 'ARRAY' or ref $_[1] ne 'ARRAY' or @{$_[0]} ne @{$_[1]}; # test all elements of an array my $i = 0; for ($i=0; $i ++$saveCount }, "TagsFromFile=$setFile"; # add option to protect the tags which are assigned after this # (this is the mechanism by which the command-line order-of-operations is preserved) $opts or $opts = { }; $$opts{ProtectSaved} = $saveCount; push @{$setTags{$setFile}}, $opts; } #------------------------------------------------------------------------------ # Get input file name or reference for calls to the ExifTool API # Inputs: 0) file name ('-' for STDIN), 1) flag to buffer STDIN # Returns: file name, or RAF reference for buffering STDIN sub Infile($;$) { my ($file, $bufferStdin) = @_; if ($file eq '-' and ($bufferStdin or $rafStdin)) { if ($rafStdin) { $rafStdin->Seek(0); # rewind } elsif (open RAF_STDIN, '-') { $rafStdin = new File::RandomAccess(\*RAF_STDIN); $rafStdin->BinMode(); } return $rafStdin if $rafStdin; } return $file; } #------------------------------------------------------------------------------ # Set new values from file # Inputs: 0) exiftool ref, 1) filename, 2) reference to list of values to set # Returns: 0 on error (and increments $countBadWr) sub DoSetFromFile($$$) { local $_; my ($exifTool, $file, $setTags) = @_; $verbose and print $vout "Setting new values from $file\n"; my $info = $exifTool->SetNewValuesFromFile(Infile($file,1), @$setTags); my $numSet = scalar(keys %$info); if ($$info{Error}) { # delete all error and warning tags my @warns = grep /^(Error|Warning)\b/, keys %$info; $numSet -= scalar(@warns); # issue a warning for the main error only if we were able to set some tags if (keys(%$info) > @warns) { my $err = $$info{Error}; delete $$info{$_} foreach @warns; $$info{Warning} = $err; } } elsif ($$info{Warning}) { my $warns = 1; ++$warns while $$info{"Warning ($warns)"}; $numSet -= $warns; } PrintErrors($exifTool, $info, $file) and ++$countBadWr, return 0; Warn "Warning: No writable tags set from $file\n" unless $numSet; return 1; } #------------------------------------------------------------------------------ # Translate backslashes to forward slashes in filename if necessary # Inputs: 0) Filename # Returns: nothing, but changes filename if necessary sub CleanFilename($) { $_[0] =~ tr/\\/\// if $hasBackslash{$^O}; } #------------------------------------------------------------------------------ # process files in our @files list # Inputs: 0) ExifTool ref, 1) list ref to just return full file names # Notes: arg 0 is not used if arg 1 is defined sub ProcessFiles($;$) { my ($exifTool, $list) = @_; my $file; foreach $file (@files) { if (defined $progressMax) { ++$progress; $progStr = " [$progress/$progressMax]"; } if (-d $file) { $multiFile = $validFile = 1; ScanDir($mainTool, $file, $list); } elsif ($filterFlag and not AcceptFile($file)) { if (-e $file) { $filtered = 1; $verbose and print $vout "-------- $file (wrong extension)$progStr\n"; } else { Warn "File not found: $file\n"; $rtnVal = 1; } } else { $validFile = 1; $list and push(@$list, $file), next; GetImageInfo($exifTool, $file); } } } #------------------------------------------------------------------------------ # Scan directory for image files # Inputs: 0) ExifTool ref, 1) directory name, 2) list ref to return file names sub ScanDir($$;$) { my ($exifTool, $dir, $list) = @_; opendir(DIR_HANDLE, $dir) or Warn("Error opening directory $dir\n"), return; my @fileList = readdir(DIR_HANDLE); closedir(DIR_HANDLE); my $file; $dir =~ /\/$/ or $dir .= '/'; foreach $file (@fileList) { my $path = "$dir$file"; if (-d $path) { next unless $recurse and $file !~ /^\./; # ignore dirs starting with "." next if $ignore{$file} or ($ignore{SYMLINKS} and -l $path); ScanDir($exifTool, $path, $list); next; } # apply rules from -ext options my $accepted; if ($filterFlag) { $accepted = AcceptFile($file) or next; # must be specifically accepted to bypass selection logic $accepted &= 0x01; } unless ($accepted) { # read/write this file if it is a supported type if ($scanWritable) { if ($scanWritable eq '1') { next unless CanWrite($file); } else { my $type = GetFileType($file); next unless defined $type and $type eq $scanWritable; } } elsif (not GetFileType($file)) { next unless $doUnzip; next unless $file =~ /\.(gz|bz2)$/i; } } $list and push(@$list, $path), next; GetImageInfo($exifTool, $path); } ++$countDir; } #------------------------------------------------------------------------------ # Set file system times # Inputs: 0) file name, 1) access time, 2) modification time, 3) creation time # Notes: issues warning if modification/access times can't be set sub SetFileTime($@) { my ($file, $aTime, $mTime, $cTime) = @_; unless (utime($aTime, $mTime, $file)) { Warn "Error setting file time - $file\n"; } # set creation time for Windows systems only (if Win32API::File::Time is available) if ($^O eq 'MSWin32' and eval 'require Win32API::File::Time') { Win32API::File::Time::SetFileTime($file,undef,undef,$cTime); } } #------------------------------------------------------------------------------ # patch for OS X 10.6 to preserve file modify date # (this probably isn't a 100% fix, but it may solve a majority of the cases) sub PreserveTime() { local $_; SetFileTime($_, @{$preserveTime{$_}}) foreach keys %preserveTime; undef %preserveTime; } #------------------------------------------------------------------------------ # Add print format entry # Inputs: 0) expression string sub AddPrintFormat($) { my $expr = shift; my $type; if ($expr =~ /^#/) { $expr =~ s/^#\[(HEAD|BODY|TAIL)\]// or return; # ignore comments $type = $1; } else { $type = 'BODY'; } $printFmt{$type} or $printFmt{$type} = [ ]; push @{$printFmt{$type}}, $expr; # add to list of requested tags push @tags, $expr =~ /\$((?:[-\w]+:)*[-\w]+#?)/g; # tags contained in braces too... push @tags, $expr =~ /\$\{((?:[-\w]+:)*[-\w]+#?)\}/g; } #------------------------------------------------------------------------------ # Get suggested file extension based on tag value for binary output # Inputs: 0) data ref # Returns: file extension (lower case), or 'dat' if unknown sub SuggestedExtension($$$) { my ($exifTool, $valPt, $tag) = @_; my $ext; if (not $binaryOutput) { $ext = 'txt'; } elsif ($$valPt =~ /^\xff\xd8\xff/) { $ext = 'jpg'; } elsif ($$valPt =~ /^(\0\0\0\x0cjP( |\x1a\x1a)\x0d\x0a\x87\x0a|\xff\x4f\xff\x51\0)/) { $ext = 'jp2'; } elsif ($$valPt =~ /^(\x89P|\x8aM|\x8bJ)NG\r\n\x1a\n/) { $ext = 'png'; } elsif ($$valPt =~ /^GIF8[79]a/) { $ext = 'gif'; } elsif ($$valPt =~ /^<\?xpacket/ or $tag eq 'XMP') { $ext = 'xmp'; } elsif ($$valPt =~ /^<\?xml/ or $tag eq 'XML') { $ext = 'xml'; } elsif ($$valPt =~ /^RIFF....WAVE/s) { $ext = 'wav'; } elsif ($tag eq 'OriginalRawFileData' and defined($ext = $exifTool->GetValue('OriginalRawFileName'))) { $ext =~ s/^.*\.//s; $ext = $ext ? lc($ext) : 'raw'; } elsif ($tag eq 'EXIF') { $ext = 'exif'; } elsif ($$valPt =~ /^(MM\0\x2a|II\x2a\0)/) { $ext = 'tiff'; } elsif ($$valPt !~ /^.{0,4096}\0/s) { $ext = 'txt'; } elsif ($$valPt =~ /^BM/) { $ext = 'bmp'; } else { $ext = 'dat'; } return $ext; } #------------------------------------------------------------------------------ # Load print format file # Inputs: 0) file name # - saves lines of file to %printFmt list # - adds tag names to @tags list sub LoadPrintFormat($) { my $arg = shift; if (not defined $arg) { Error "Must specify file or expression for -p option\n"; } elsif ($arg !~ /\n/ and -f $arg and Open(\*FMT_FILE, $arg)) { foreach () { AddPrintFormat($_); } close(FMT_FILE); } else { AddPrintFormat($arg . "\n"); } } #------------------------------------------------------------------------------ # A sort of sprintf for filenames # Inputs: 0) format string (%d=dir, %f=file name, %e=ext), # 1) source filename or undef to test format string # 2-4) [%t %g %s only] tag name, ref to array of group names, suggested extension # Returns: new filename or undef on error (or if no file and fmt contains token) sub FilenameSPrintf($;$@) { my ($fmt, $file, @extra) = @_; local $_; # return format string straight away if no tokens return $fmt unless $fmt =~ /%[-+]?\d*[.:]?\d*[lu]?[dfetgs]/; return undef unless defined $file; CleanFilename($file); # make sure we are using forward slashes # split filename into directory, file, extension my %part; @part{qw(d f e)} = ($file =~ /^(.*?)([^\/]*?)(\.[^.\/]*)?$/); defined $part{f} or Warn("Error: Bad pattern match for file $file\n"), return undef; $part{e} = $part{e} ? substr($part{e}, 1) : ''; @part{qw(t g s)} = @extra; my ($filename, $pos) = ('', 0); while ($fmt =~ /(%([-+]?)(\d*)([.:]?)(\d*)([lu]?)([dfetgs]))/g) { $filename .= substr($fmt, $pos, pos($fmt) - $pos - length($1)); $pos = pos($fmt); my ($sign, $wid, $dot, $skip, $mod, $code) = ($2, $3, $4, $5 || 0, $6, $7); my (@path, $part, $len, $groups); if ($code eq 'd' and $dot and $dot eq ':') { # field width applies to directory levels instead of characters @path = split '/', $part{d}; $len = scalar @path; } else { if ($code eq 'g') { $groups = $part{g} || [ ] unless defined $groups; $fmt =~ /\G(\d?)/g; # look for %g1, %g2, etc $part{g} = $$groups[$1 || 0]; $pos = pos($fmt); } $part{$code} = '' unless defined $part{$code}; $len = length $part{$code}; } next unless $skip < $len; $wid = $len - $skip if $wid eq '' or $wid + $skip > $len; $skip = $len - $wid - $skip if $sign eq '-'; if (@path) { $part = join('/', @path[$skip..($skip+$wid-1)]) . '/'; } else { $part = substr($part{$code}, $skip, $wid); } $part = ($mod eq 'u') ? uc($part) : lc($part) if $mod; $filename .= $part; } $filename .= substr($fmt, $pos); # add rest of file name # remove double slashes (except at beginning to allow Windows UNC paths) $filename =~ s{(?!^)//}{/}g; return $filename; } #------------------------------------------------------------------------------ # Convert number to alphabetical index: a, b, c, ... z, aa, ab ... # Inputs: 0) number # Returns: alphabetical index string sub Num2Alpha($) { my $num = shift; my $alpha = chr(97 + ($num % 26)); while ($num >= 26) { $num = int($num / 26) - 1; $alpha = chr(97 + ($num % 26)) . $alpha; } return $alpha; } #------------------------------------------------------------------------------ # Expand '%c' and '%C' codes if filename to get next unused file name # Inputs: 0) file name format string, 1) filename ok to use even if it exists # Returns: new file name sub NextUnusedFilename($;$) { my ($fmt, $okfile) = @_; return $fmt unless $fmt =~ /%[-+]?\d*\.?\d*[lun]?[cC]/; my %sep = ( '-' => '-', '+' => '_' ); my ($copy, $alpha) = (0, 'a'); for (;;) { my ($filename, $pos) = ('', 0); while ($fmt =~ /(%([-+]?)(\d*)(\.?)(\d*)([lun]?)([cC]))/g) { $filename .= substr($fmt, $pos, pos($fmt) - $pos - length($1)); $pos = pos($fmt); my ($sign, $wid, $dec, $wid2, $mod, $tok) = ($2, $3 || 0, $4, $5 || 0, $6, $7); if ($tok eq 'C') { $seqFileNum = $wid if $wid and not $seqFileNum; $wid = $wid2; } else { next unless $dec or $copy; $wid = $wid2 if $wid < $wid2; } # add dash or underline separator if '-' or '+' specified $filename .= $sep{$sign} if $sign; if ($mod and $mod ne 'n') { my $a = $tok eq 'C' ? Num2Alpha($seqFileNum++) : $alpha; my $str = ($wid and $wid > length $a) ? 'a' x ($wid - length($a)) : ''; $str .= $a; $str = uc $str if $mod eq 'u'; $filename .= $str; } else { my $c = $tok eq 'C' ? $seqFileNum++ : $copy; my $num = $c + ($mod ? 1 : 0); $filename .= $wid ? sprintf("%.${wid}d",$num) : $num; } } $filename .= substr($fmt, $pos); # add rest of file name # return now with filename unless file exists return $filename unless -e $filename; return $filename if defined $okfile and $filename eq $okfile; ++$copy; ++$alpha; } } #------------------------------------------------------------------------------ # Create directory for specified file # Inputs: 0) complete file name including path # Returns: true if a directory was created sub CreateDirectory($) { my $file = shift; my ($dir, $created); ($dir = $file) =~ s/[^\/]*$//; # remove filename from path specification if ($dir and not -d $dir) { my @parts = split /\//, $dir; $dir = ''; foreach (@parts) { $dir .= $_; if (length $dir and not -d $dir and # don't try to create a network drive root directory not ($hasBackslash{$^O} and $dir =~ m{^//[^/]*$})) { # create directory since it doesn't exist mkdir($dir, 0777) or Error("Error creating directory $dir\n"), return 0; $verbose and print $vout "Created directory $dir\n"; $created = 1; } $dir .= '/'; } } ++$countNewDir if $created; return $created; } #------------------------------------------------------------------------------ # Open output text file # Inputs: 0) file name format string, 1-N) extra arguments for FilenameSPrintf # Returns: 0) file reference (or undef on error), 1) file name if opened, 2) append flag # Notes: returns reference to STDOUT and no file name if no textOut file needed sub OpenOutputFile($;@) { my ($file, @args) = @_; my ($fp, $outfile, $append); if ($textOut) { $outfile = $file; CleanFilename($outfile); if ($textOut =~ /%[-+]?\d*[.:]?\d*[lun]?[dfetgscC]/ or defined $tagOut) { # make filename from printf-like $textOut $outfile = FilenameSPrintf($textOut, $file, @args); return () unless defined $outfile; $outfile = NextUnusedFilename($outfile); CreateDirectory($outfile); # create directory if necessary } else { $outfile =~ s/\.[^.\/]*$//; # remove extension if it exists $outfile .= $textOut; } my $mode = '>'; if (-e $outfile) { unless ($textOverwrite) { Warn "Output file $outfile already exists for $file\n"; return (); } if ($textOverwrite == 2 or ($textOverwrite == 3 and $created{$outfile})) { $mode = '>>'; $append = 1; } } unless (Open(\*OUTFILE, $outfile, $mode)) { my $what = $mode eq '>' ? 'creating' : 'appending to'; Error("Error $what $outfile\n"); return (); } binmode(OUTFILE) if $binaryOutput; $fp = \*OUTFILE; } else { $fp = \*STDOUT; } return($fp, $outfile, $append); } #------------------------------------------------------------------------------ # Filter files based on extension # Inputs: 0) file name # Returns: 0 = rejected, 1 = specifically accepted, 2 = accepted by default # Notes: This routine should only be called if $filterFlag is set sub AcceptFile($) { my $file = shift; my $ext = ($file =~ /^.*\.(.+)$/s) ? uc($1) : ''; return $filterExt{$ext} if defined $filterExt{$ext}; return $filterExt{'*'} if defined $filterExt{'*'}; return 0 if $filterFlag & 0x02; # reject if accepting specific extensions return 2; # accept by default } #------------------------------------------------------------------------------ # Rename a file (patch for Windows denial-of-service susceptibility) # Inputs: 0) old name, 1) new name # Returns: true on success sub Rename($$) { my ($old, $new) = @_; my $result = rename($old, $new); if (not $result and $^O eq 'MSWin32') { my $try = 2; for (;;) { select(undef,undef,undef,0.01); # sleep for 0.01 sec $result = rename($old, $new); last if $result or ++$try > 50; # retry for 0.5 seconds } Warn("It took $try tries to rename $new\n") if $result; } return $result; } #------------------------------------------------------------------------------ # Slurp file into buffer # Inputs: 0) file name, 1) buffer reference # Returns: 1 on success sub SlurpFile($$) { my ($file, $buffPt) = @_; Open(\*INFILE, $file) or Warn("Error opening file $file\n"), return 0; binmode(INFILE); my $bsize = 1024 * 1024; my $num = read(INFILE, $$buffPt, $bsize); unless (defined $num) { close(INFILE); Warn("Error reading $file\n"); return 0; } my $bmax = 64 * $bsize; while ($num == $bsize) { $bsize *= 2 if $bsize < $bmax; my $buff; $num = read(INFILE, $buff, $bsize); last unless $num; $$buffPt .= $buff; } close(INFILE); return 1; } #------------------------------------------------------------------------------ # Read arguments from -stay_open argfile # Inputs: 0) argument list ref # Notes: blocks until -execute, -stay_open or -@ option is available # (or until there was an error reading from the file) sub ReadStayOpen($) { my $args = shift; my (@newArgs, $processArgs, $result, $optArgs); my $lastOpt = ''; my $unparsed = length $stayOpenBuff; for (;;) { if ($unparsed) { # parse data already read from argfile $result = $unparsed; undef $unparsed; } else { # read more data from argfile # - this read may block (which is good) if reading from a pipe $result = sysread(STAYOPEN, $stayOpenBuff, 65536, length($stayOpenBuff)); } if ($result) { my $pos = 0; while ($stayOpenBuff =~ /\n/g) { my $len = pos($stayOpenBuff) - $pos; my $arg = substr($stayOpenBuff, $pos, $len); $pos += $len; $arg =~ s/^\s+//; # remove leading white space $arg =~ s/[\x0d\x0a]+$//s; # remove trailing newline # remove white space before, and single space after '=', '+=', '-=' or '<=' $arg =~ s/^(-[-:\w]+#?)\s*([-+<]?=) ?/$1$2/; next if $arg eq '' or $arg =~ /^#/; # ignore empty/comment lines push @newArgs, $arg; if ($optArgs) { # this is an argument for the last option undef $optArgs; next unless $lastOpt eq '-stay_open' or $lastOpt eq '-@'; } else { $optArgs = $optArgs{$arg}; $lastOpt = lc $arg; $optArgs = $optArgs{$lastOpt} unless defined $optArgs; next unless $lastOpt =~ /^-execute\d*$/; } $processArgs = 1; last; # process arguments up to this point } next unless $pos; # nothing to do if we didn't read any arguments # keep unprocessed data in buffer $stayOpenBuff = substr($stayOpenBuff, $pos); if ($processArgs) { # process new arguments after -execute or -stay_open option unshift @$args, @newArgs; last; } } elsif ($result == 0) { # sysread() didn't block (ie. when reading from a file), # so wait for a short time (1/100 sec) then try again # Note: may break out of this early if SIGCONT is received select(undef,undef,undef,0.01); } else { Warn "Error reading from ARGFILE\n"; close STAYOPEN; $stayOpen = 0; last; } } } #------------------------------------------------------------------------------ # Print list of tags # Inputs: 0) message, 1-N) list of tag names sub PrintTagList($@) { my $msg = shift; print $msg, ":\n" unless $quiet; print ' '; my $len = 1; my $tag; foreach $tag (@_) { my $taglen = length($tag); if ($len + $taglen > 78) { print "\n "; $len = 1; } print " $tag"; $len += $taglen + 1; } @_ or print ' [empty list]'; print "\n"; } #------------------------------------------------------------------------------ # Print warnings and errors from info hash # Inputs: 0) ExifTool object ref, 1) info hash, 2) file name # Returns: true if there was an Error sub PrintErrors($$$) { my ($exifTool, $info, $file) = @_; my ($tag, $key); foreach $tag (qw(Warning Error)) { next unless $$info{$tag}; my @keys = ( $tag ); push @keys, sort(grep /^$tag /, keys %$info) if $exifTool->Options('Duplicates'); foreach $key (@keys) { Warn "$tag: $info->{$key} - $file\n"; } } return $$info{Error}; } __END__ =head1 NAME exiftool - Read and write meta information in files =head1 SYNOPSIS =over 4 =item B [I] [-I...] [--I...] I... =item B [I] -I[+-E]=[I]... I... =item B [I] B<-tagsFromFile> I [-I[EI]...] I... =item B [ B<-ver> | B<-list>[B|B|B|B|B[I]|B|B] ] =back For specific examples, see the L sections below. This documentation is displayed if exiftool is run without an input I when one is expected. =head1 DESCRIPTION A command-line interface to L, used for reading and writing meta information in a variety of file types. I is one or more source file names, directory names, or C<-> for the standard input. Information is read from source files and printed in readable form to the console (or written to output text files with B<-w>). To write or delete information, tag values are assigned using the -I=[I] syntax, or the B<-geotag> option. To copy or move information, the B<-tagsFromFile> feature is used. By default the original files are preserved with C<_original> appended to their names -- be sure to verify that the new files are OK before erasing the originals. Once in write mode, exiftool will ignore any read-specific options. Note: If I is a directory name then only supported file types in the directory are processed (in write mode only writable types are processed). However, files may be specified by name, or the B<-ext> option may be used to force processing of files with any extension. Below is a list of file types and meta information formats currently supported by ExifTool (r = read, w = write, c = create): File Types ------------+-------------+-------------+-------------+------------ 3FR r | EIP r | LA r | ORF r/w | RSRC r 3G2 r | EPS r/w | LNK r | OTF r | RTF r 3GP r | ERF r/w | M2TS r | PAC r | RW2 r/w ACR r | EXE r | M4A/V r | PAGES r | RWL r/w AFM r | EXIF r/w/c | MEF r/w | PBM r/w | RWZ r AI r/w | EXR r | MIE r/w/c | PCD r | RM r AIFF r | F4A/V r | MIFF r | PDF r/w | SO r APE r | FFF r/w | MKA r | PEF r/w | SR2 r/w ARW r/w | FLA r | MKS r | PFA r | SRF r ASF r | FLAC r | MKV r | PFB r | SRW r/w AVI r | FLV r | MNG r/w | PFM r | SVG r BMP r | FPF r | MODD r | PGF r | SWF r BTF r | FPX r | MOS r/w | PGM r/w | THM r/w CHM r | GIF r/w | MOV r | PLIST r | TIFF r/w COS r | GZ r | MP3 r | PICT r | TTC r CR2 r/w | HDP r/w | MP4 r | PMP r | TTF r CRW r/w | HDR r | MPC r | PNG r/w | VRD r/w/c CS1 r/w | HTML r | MPG r | PPM r/w | VSD r DCM r | ICC r/w/c | MPO r/w | PPT r | WAV r DCP r/w | IDML r | MQV r | PPTX r | WDP r/w DCR r | IIQ r/w | MRW r/w | PS r/w | WEBP r DFONT r | IND r/w | MXF r | PSB r/w | WEBM r DIVX r | INX r | NEF r/w | PSD r/w | WMA r DJVU r | ITC r | NRW r/w | PSP r | WMV r DLL r | J2C r | NUMBERS r | QTIF r | WV r DNG r/w | JNG r/w | ODP r | RA r | X3F r/w DOC r | JP2 r/w | ODS r | RAF r/w | XCF r DOCX r | JPEG r/w | ODT r | RAM r | XLS r DV r | K25 r | OFR r | RAR r | XLSX r DVB r | KDC r | OGG r | RAW r/w | XMP r/w/c DYLIB r | KEY r | OGV r | RIFF r | ZIP r Meta Information ----------------------+----------------------+--------------------- EXIF r/w/c | CIFF r/w | Ricoh RMETA r GPS r/w/c | AFCP r/w | Picture Info r IPTC r/w/c | Kodak Meta r/w | Adobe APP14 r XMP r/w/c | FotoStation r/w | MPF r MakerNotes r/w/c | PhotoMechanic r/w | Stim r Photoshop IRB r/w/c | JPEG 2000 r | APE r ICC Profile r/w/c | DICOM r | Vorbis r MIE r/w/c | Flash r | SPIFF r JFIF r/w/c | FlashPix r | DjVu r Ducky APP12 r/w/c | QuickTime r | M2TS r PDF r/w/c | Matroska r | PE/COFF r PNG r/w/c | GeoTIFF r | AVCHD r Canon VRD r/w/c | PrintIM r | ZIP r Nikon Capture r/w/c | ID3 r | (and more) =head1 OPTIONS Case is not significant for any command-line option (including tag and group names), except for single-character options when the corresponding upper-case option exists. Many single-character options have equivalent long-name versions (shown in brackets), and some options have inverses which are invoked with a leading double-dash. Note that multiple single-character options may NOT be combined into one argument because this would be interpreted as a tag name. Contrary to standard practice, options may appear after source file names on the exiftool command line. =head2 Option Summary L -TAG or --TAG Extract or exclude specified tag -TAG[+-]=[VALUE] Write new value for tag -TAG[+-]<=DATFILE Write tag value from contents of file -TAG[+-] -args (-argFormat) Format metadata as exiftool arguments -b (-binary) Output metadata in binary format -c FMT (-coordFormat) Set format for GPS coordinates -charset [[TYPE=]CHARSET] Specify encoding for special characters -csv[=CSVFILE] Export/import tags in CSV format -d FMT (-dateFormat) Set format for date/time values -D (-decimal) Show tag ID numbers in decimal -E, -ex (-escape(HTML|XML)) Escape values for HTML (-E) or XML (-ex) -f (-forcePrint) Force printing of all specified tags -g[NUM...] (-groupHeadings) Organize output by tag group -G[NUM...] (-groupNames) Print group name for each tag -h (-htmlFormat) Use HMTL formatting for output -H (-hex) Show tag ID number in hexadecimal -htmlDump[OFFSET] Generate HTML-format binary dump -j[=JSONFILE] (-json) Export/import tags in JSON format -l (-long) Use long 2-line output format -L (-latin) Use Windows Latin1 encoding -lang [LANG] Set current language -listItem INDEX Extract specific item from a list -n (--printConv) Read/write numerical tag values -p FMTFILE (-printFormat) Print output in specified format -php Export tags as a PHP Array -s[NUM] (-short) Short output format -S (-veryShort) Very short output format -sep STR (-separator) Set separator string for list items -sort Sort output alphabetically -struct Enable output of structured information -t (-tab) Output in tab-delimited list format -T (-table) Output in tabular format -v[NUM] (-verbose) Print verbose messages -w[+|!] EXT (-textOut) Write (or overwrite!) output text files -W[+|!] FMT (-tagOut) Write output text file for each tag -Wext EXT (-tagOutExt) Write only specified file types with -W -X (-xmlFormat) Use RDF/XML output format L -a (-duplicates) Allow duplicate tags to be extracted -e (--composite) Do not calculate composite tags -ee (-extractEmbedded) Extract information from embedded files -ext EXT (-extension) Process files with specified extension -F[OFFSET] (-fixBase) Fix the base for maker notes offsets -fast[NUM] Increase speed for slow devices -fileOrder [-]TAG Set file processing order -i DIR (-ignore) Ignore specified directory name -if EXPR Conditionally process files -m (-ignoreMinorErrors) Ignore minor errors and warnings -o OUTFILE (-out) Set output file or directory name -overwrite_original Overwrite original by renaming tmp file -overwrite_original_in_place Overwrite original by copying tmp file -P (-preserve) Preserve date/time of original file -password PASSWD Password for processing protected files -progress Show file progress count -q (-quiet) Quiet processing -r (-recurse) Recursively process subdirectories -scanForXMP Brute force XMP scan -u (-unknown) Extract unknown tags -U (-unknown2) Extract unknown binary tags too -wm MODE (-writeMode) Set tag write/create mode -z (-zip) Read/write compressed information L -geotag TRKFILE Geotag images from specified GPS log -globalTimeShift SHIFT Shift all formatted date/time values -use MODULE Add features from plug-in module L -delete_original[!] Delete "_original" backups -restore_original Restore from "_original" backups L -@ ARGFILE Read command-line arguments from file -k (-pause) Pause before terminating -list[w|f|wf|g[NUM]|d|x] List various exiftool capabilities -ver Print exiftool version number L -common_args Define common arguments -config CFGFILE Specify configuration file name -echo[NUM] TEXT Echo text to stdout or stderr -execute[NUM] Execute multiple commands on one line -srcfile FMT Set different source file name -stay_open FLAG Keep reading -@ argfile even after EOF =head2 Option Details =head3 Tag operations =over 5 =item B<->I Extract information for the specified tag (ie. C<-CreateDate>). Multiple tags may be specified in a single command. A tag name is the handle by which a piece of information is referenced. See L for documentation on available tag names. A tag name may include leading group names separated by colons (ie. C<-EXIF:CreateDate>, or C<-Doc1:XMP:Creator>), and each group name may be prefixed by a digit to specify family number (ie. C<-1IPTC:City>). Use the B<-listg> option to list available group names by family. A special tag name of C may be used to indicate all meta information. This is particularly useful when a group name is specified to extract all information in a group (but beware that unless the B<-a> option is also used, some tags in the group may be suppressed by same-named tags in other groups). The wildcard characters C and C<*> may be used in a tag name to match any single character and zero or more characters respectively. These may not be used in a group name, with the exception that a group name of C<*> (or C) may be used to extract all instances of a tag (as if B<-a> was used). Note that arguments containing wildcards must be quoted on the command line of most systems to prevent shell globbing. A C<#> may be appended to the tag name to disable the print conversion on a per-tag basis (see the B<-n> option). This may also be used when writing or copying tags. If no tags are specified, all available information is extracted (as if C<-All> had been specified). Note: Descriptions, not tag names, are shown by default when extracting information. Use the B<-s> option to see the tag names instead. =item B<-->I Exclude specified tag from extracted information. Same as the B<-x> option. Once excluded from the output, a tag may not be re-included by a subsequent option. May also be used following a B<-tagsFromFile> option to exclude tags from being copied, or to exclude groups from being deleted when deleting all information (ie. C<-all= --exif:all> deletes all but EXIF information). But note that this will not exclude individual tags from a group delete. Instead, individual tags may be recovered using the B<-tagsFromFile> option (ie. C<-all= -tagsfromfile @ -artist>). Wildcards are permitted as described above for B<-TAG>. =item B<->I[+-]B<=>[I] Write a new value for the specified tag (ie. C<-comment=wow>), or delete the tag if no I is given (ie. C<-comment=>). C<+=> and C<-=> are used to add or remove existing entries from a list, or to shift date/time values (see L for details). C<+=> may also be used to increment numerical values, and C<-=> may be used to conditionally delete or replace a tag (see L for examples). I may contain a leading family 0 or 1 group name separated by a colon. If no group name is specified, the tag is created in the preferred group, and updated in any other location where a same-named tag already exists. The preferred group is the first group in the following list where I is valid: 1) EXIF, 2) IPTC, 3) XMP. The wildcards C<*> and C may be used in tag names to assign the same value to multiple tags. When specified with wildcards, "unsafe" tags are not written. A tag name of C is eqivalent to C<*> (except that it doesn't require quoting, while arguments with wildcards do on systems with shell globbing), and is often used when deleting all metadata (ie. C<-All=>) or an entire group (ie. C<-GROUP:All=>). Note that not all groups are deletable, and that the JPEG APP14 "Adobe" group is not removed by default with C<-All=> because it may affect the appearance of the image. Use the B<-listd> option for a complete list of deletable groups, and see note 3 below regarding the "APP" groups. Also, within an image some groups may be contained within others, and these groups are removed if the containing group is deleted: JPEG Image: - Deleting EXIF or IFD0 also deletes ExifIFD, GlobParamIFD, GPS, IFD1, InteropIFD, MakerNotes, PrintIM and SubIFD. - Deleting ExifIFD also deletes InteropIFD and MakerNotes. - Deleting Photoshop also deletes IPTC. TIFF Image: - Deleting EXIF only removes ExifIFD which also deletes InteropIFD and MakerNotes. Notes: 1) Many tag values may be assigned in a single command. If two assignments affect the same tag, the latter takes precedence (except for list-type tags, where both values are written). 2) MakerNotes tags may be edited, but not created or deleted individually. This avoids many potential problems including the inevitable compatibility problems with OEM software which may be very inflexible about the information it expects to find in the maker notes. 3) Changes to PDF files are reversible because the original information is never actually deleted from the file. So ExifTool alone may not be used to securely edit metadata in PDF files. 4) The "APP" group names ("APP0" through "APP15") are used to delete JPEG application segments which are not associated with another deletable group. For example, specifying C<-APP14:All=> will NOT delete the APP14 "Adobe" segment because this is accomplished with C<-Adobe:All>. Special feature: Integer values may be specified in hexadecimal with a leading C<0x>, and simple rational values may be specified as fractions. =item B<->IE=I or B<->IE=I Set the value of a tag from the contents of file I. The file name may also be given by a I string where %d, %f and %e represent the directory, file name and extension of the original I (see the B<-w> option for more details). Note that quotes are required around this argument to prevent shell redirection since it contains a C> symbol. C<+E=> or C<-E=> may also be used to add or delete specific list entries, or to shift date/time values. =item B<-tagsFromFile> I or I Copy tag values from I to I. Tag names on the command line after this option specify the tags to be copied, or excluded from the copy. Wildcards are permitted in these tag names. If no tags are specified, then all possible tags (see note 1 below) from the source file are copied to same-named tags in the preferred location of the output file (the same as specifying C<-all>). More than one B<-tagsFromFile> option may be used to copy tags from multiple files. By default, this option will commute information between same-named tags in different groups and write each tag to the preferred group. This allows some information to be automatically translated when copying between images of different formats. However, if a group name is specified for a tag then the information is written to this group (unless redirected to another group, see below). If C is used as a group name, then the information is written to the same family 1 group in the destination file. In this way, C<-All:All> is used to copy all information while preserving the family 1 group (ie. the specific location in the metadata) of each tag. I may be the same as I to move information around within a single file. In this case, C<@> may be used to represent the source file (ie. C<-tagsFromFile @>), permitting this feature to be used for batch processing multiple files. Specified tags are then copied from each file in turn as it is rewritten. For advanced batch use, the source file name may also be specified using a I string in which %d, %f and %e represent the directory, file name and extension of I. See B<-w> option for I string examples. A powerful redirection feature allows a destination tag to be specified for each copied tag. With this feature, information may be written to a tag with a different name or group. This is done using E'-IEI'E or E'-IEI'E on the command line after B<-tagsFromFile>, and causes the value of I to be copied from I and written to I in I. Note that this argument must be quoted to prevent shell redirection, and there is no C<=> sign as when assigning new values. Source and/or destination tags may be prefixed by a group name and/or suffixed by C<#>. Wildcards are allowed in both the source and destination tag names. A destination group and/or tag name of C or C<*> writes to the same family 1 group and/or tag name as the source. If no destination group is specified, the information is written to the preferred group. Whitespace around the C> or C> is ignored. As a convenience, C<-tagsFromFile @> is assumed for any redirected tags which are specified without a prior B<-tagsFromFile> option. Copied tags may also be added or deleted from a list with arguments of the form E'-I+EI'E or E'-I-EI'E. An extension of the redirection feature allows strings involving tag names to be used on the right hand side of the C> symbol with the syntax E'-IEI'E, where tag names in I are prefixed with a C<$> symbol. See the B<-p> option for more details about this syntax. Strings starting with a C<=> sign must insert a single space after the C> to avoid confusion with the C=> operator which sets the tag value from the contents of a file. A single space at the start of the string is removed if it exists, but all other whitespace in the string is preserved. See L for examples using B<-tagsFromFile>. Notes: 1) Some tags (generally tags which may affect the appearance of the image) are considered "unsafe" to write, and are only copied if specified explicitly (ie. no wildcards). See the L for more details about "unsafe" tags. 2) Be aware of the difference between excluding a tag from being copied (--I), and deleting a tag (-I=). Excluding a tag prevents it from being copied to the destination image, but deleting will remove a pre-existing tag from the image. 3) The maker note information is copied as a block, so it isn't affected like other information by subsequent tag assignments on the command line. Also, since the PreviewImage referenced from the maker notes may be rather large, it is not copied, and must be transferred separately if desired. 4) The order of operations is to copy all specified tags at the point of the B<-tagsFromFile> option in the command line. Any tag assignment to the right of the B<-tagsFromFile> option is made after all tags are copied. For example, new tag values are set in the order One, Two, Three then Four with this command: exiftool -One=1 -tagsFromFile s.jpg -Two -Four=4 -Three d.jpg This is significant in the case where an overlap exists between the copied and assigned tags because later operations may override earlier ones. 5) The normal behaviour of copied tags differs subtly from that of assigned tags for list-type tags. When copying to a list, each copied tag overrides any previous operations on the list. While this avoids duplicate list items when copying groups of tags from a file containing redundant information, it also prevents values of different tags from being copied into the same list when this is the intent. So a B<-addTagsFromFile> option is provided which allows copying of multiple tags into the same list. ie) exiftool -addtagsfromfile @ '-subject and B<-addTagsFromFile> options are equivalent. 6) The B<-a> option (allow duplicate tags) is always in effect when reading tags from I. 7) Structured tags are copied by default when copying tags. See the B<-struct> option for details. =item B<-x> I (B<-exclude>) Exclude the specified tag. There may be multiple B<-x> options. This has the same effect as --I on the command line. May also be used following a B<-tagsFromFile> option to exclude tags from being copied. =back =head3 Input-output text formatting Note that trailing spaces are removed from extracted values for most output text formats. The exceptions are C<-b>, C<-csv>, C<-j> and C<-X>. =over 5 =item B<-args> (B<-argFormat>) Output information in the form of exiftool arguments, suitable for use with the B<-@> option when writing. May be combined with the B<-G> option to include group names. This feature may be used to effectively copy tags between images, but allows the metadata to be altered by editing the intermediate file (C in this example): exiftool -args -G1 --filename --directory src.jpg > out.args exiftool -@ out.args dst.jpg Note: Be careful when copying information with this technique since it is easy to write tags which are normally considered "unsafe". For instance, the FileName and Directory tags are excluded in the example above to avoid renaming and moving the destination file. Also note that the second command above will produce warning messages for any tags which are not writable. =item B<-b> (B<-binary>) Output requested metadata in binary format without tag names or descriptions. This option is mainly used for extracting embedded images or other binary data, but it may also be useful for some text strings since control characters (such as newlines) are not replaced by '.' as they are in the default output. List items are separated by a newline when extracted with the B<-b> option. May be combined with C<-php> or C<-X> to extract binary data in PHP or XML format. =item B<-c> I (B<-coordFormat>) Set the print format for GPS coordinates. I uses the same syntax as the C format string. The specifiers correspond to degrees, minutes and seconds in that order, but minutes and seconds are optional. For example, the following table gives the output for the same coordinate using various formats: FMT Output ------------------- ------------------ "%d deg %d' %.2f"\" 54 deg 59' 22.80" (default for reading) "%d %d %.8f" 54 59 22.80000000 (default for copying) "%d deg %.4f min" 54 deg 59.3800 min "%.6f degrees" 54.989667 degrees Notes: 1) To avoid loss of precision, the default coordinate format is different when copying tags using the B<-tagsFromFile> option. 2) If the hemisphere is known, a reference direction (N, S, E or W) is appended to each printed coordinate, but adding a C<+> to the format specifier (ie. C<%+.6f>) prints a signed coordinate instead. 3) This print formatting may be disabled with the B<-n> option to extract coordinates as signed decimal degrees. =item B<-charset> [[I=]I] If I is C or not specified, this option sets the ExifTool character encoding for output tag values when reading and input values when writing. The default ExifTool encoding is C. If no I is given, a list of available character sets is returned. Valid I values are: CHARSET Alias(es) Description ---------- --------------- ---------------------------------- UTF8 cp65001, UTF-8 UTF-8 characters (default) Latin cp1252, Latin1 Windows Latin1 (West European) Latin2 cp1250 Windows Latin2 (Central European) Cyrillic cp1251, Russian Windows Cyrillic Greek cp1253 Windows Greek Turkish cp1254 Windows Turkish Hebrew cp1255 Windows Hebrew Arabic cp1256 Windows Arabic Baltic cp1257 Windows Baltic Vietnam cp1258 Windows Vietnamese Thai cp874 Windows Thai MacRoman cp10000, Roman Macintosh Roman MacLatin2 cp10029 Macintosh Latin2 (Central Europe) MacCyrillic cp10007 Macintosh Cyrillic MacGreek cp10006 Macintosh Greek MacTurkish cp10081 Macintosh Turkish MacRomanian cp10010 Macintosh Romanian MacIceland cp10079 Macintosh Icelandic MacCroatian cp10082 Macintosh Croatian Other values of I listed below are used to specify the internal encoding of various meta information formats. TYPE Description Default --------- ------------------------------------------- ------- EXIF Internal encoding of EXIF "ASCII" strings (none) ID3 Internal encoding of ID3v1 information Latin IPTC Internal IPTC encoding to assume when Latin IPTC:CodedCharacterSet is not defined Photoshop Internal encoding of Photoshop IRB strings Latin QuickTime Internal encoding of QuickTime strings MacRoman See L for more information about coded character sets. =item B<-csv>[=I] Export information in as a CSV file, or import information if I is specified. When importing, the CSV file must be in exactly the same format as the exported file. The first row of the I must be the ExifTool tag names (with optional group names) for each column of the file, and values must be separated by commas. A special "SourceFile" column specifies the files associated with each row of information (a SourceFile of "*" may be used to apply the information to all target images). The following examples demonstrate basic use of this option: # generate CSV file with common tags from all images in a directory exiftool -common -csv dir > out.csv # update metadata for all images in a directory from CSV file exiftool -csv=a.csv dir Empty values are ignored when importing. To force a tag to be deleted, use the B<-f> option and set the value to "-" in the CSV file (or to the MissingTagValue if this API option was used). May be combined with the B<-g> or B<-G> option to add group names to the tags. If the B<-a> option is used to allow duplicate tag names, the duplicate tags are only included in the CSV output if the column headings are unique. Adding the B<-G4> option ensures a unique column heading for each tag. List-type tags are stored as simple strings in a CSV file, but the B<-sep> option may be used to split them back into separate items when importing. Special feature: B<-csv>+=I may be used to add items to existing lists. This affects only list-type tags. Also applies to the B<-j> option. Note that this option is fundamentally different than all other output format options because it requires information from all input files to be buffered in memory before the output is written. This may result in excessive memory usage when processing a very large number of files with a single command. Also, it makes this option incompatible with the B<-w> option. =item B<-d> I (B<-dateFormat>) Set the format for date/time tag values. The specifics of the I syntax are system dependent -- consult the C man page on your system for details. The default format is equivalent to "%Y:%m:%d %H:%M:%S". This option has no effect on date-only or time-only tags and ignores timezone information if present. Only one B<-d> option may be used per command. The inverse operation (ie. un-formatting a date/time value) is currently not applied when writing a date/time tag. =item B<-D> (B<-decimal>) Show tag ID number in decimal when extracting information. =item B<-E>, B<-ex> (B<-escapeHTML>, B<-escapeXML>) Escape characters in output values for HTML (B<-E>) or XML (B<-ex>). For HTML, all characters with Unicode code points above U+007F are escaped as well as the following 5 characters: & (&) E<39> (') E (") E (>) and E (<). For XML, only these 5 characters are escaped. The B<-E> option is implied with B<-h>, and B<-ex> is implied with B<-X>. The inverse conversion is applied when writing tags. =item B<-f> (B<-forcePrint>) Force printing of tags even if their values are not found. This option only applies when tag names are specified. With this option, a dash (C<->) is printed for the value of any missing tag (but this may be configured via the API MissingTagValue option). May also be used to add a 'flags' attribute to the B<-listx> output, or to allow tags to be deleted when writing with the B<-csv>=I feature. =item B<-g>[I][:I...] (B<-groupHeadings>) Organize output by tag group. I specifies a group family number, and may be 0 (general location), 1 (specific location), 2 (category), 3 (document number) or 4 (instance number). Multiple families may be specified by separating them with colons. By default the resulting group name is simplified by removing any leading C and collapsing adjacent identical group names, but this can be avoided by placing a colon before the first family number (ie. B<-g:3:1>). If I is not specified, B<-g0> is assumed. Use the B<-listg> option to list group names for a specified family. =item B<-G>[I][:I...] (B<-groupNames>) Same as B<-g> but print group name for each tag. =item B<-h> (B<-htmlFormat>) Use HTML table formatting for output. Implies the B<-E> option. The formatting options B<-D>, B<-H>, B<-g>, B<-G>, B<-l> and B<-s> may be used in combination with B<-h> to influence the HTML format. =item B<-H> (B<-hex>) Show tag ID number in hexadecimal when extracting information. =item B<-htmlDump>[I] Generate a dynamic web page containing a hex dump of the EXIF information. This can be a very powerful tool for low-level analysis of EXIF information. The B<-htmlDump> option is also invoked if the B<-v> and B<-h> options are used together. The verbose level controls the maximum length of the blocks dumped. An I may be given to specify the base for displayed offsets. If not provided, the EXIF/TIFF base offset is used. Use B<-htmlDump0> for absolute offsets. Currently only EXIF/TIFF and JPEG information is dumped, but the -u option can be used to give a raw hex dump of other file formats. =item B<-j>[=I] (B<-json>) Use JSON (JavaScript Object Notation) formatting for console output, or import JSON file if I is specified. This option may be combined with B<-g> to organize the output into objects by group, or B<-G> to add group names to each tag. List-type tags with multiple items are output as JSON arrays unless B<-sep> is used. By default XMP structures are flattened into individual tags in the JSON output, but the original structure may be preserved with the B<-struct> option (this also causes all list-type XMP tags to be output as JSON arrays, otherwise single-item lists are output as simple strings). The B<-a> option is implied if the B<-g> or B<-G> options are used, otherwise it is ignored and duplicate tags are suppressed. Adding the B<-D> or B<-H> option changes tag values to JSON objects with "val" and "id" fields, and adding B<-l> adds a "desc" field, and a "num" field if the numerical value is different from the converted "val". The B<-b>, B<-L> and B<-charset> options have no effect on the JSON output. If I is specified, the file is imported and the tag definitions from the file are used to set tag values on a per-file basis. The special "SourceFile" entry in each JSON object associates the information with a specific target file (see the B<-csv> option for details). The imported JSON file must have the same format as the exported JSON files with the exception that the B<-g> option is not compatible with the import file format (use B<-G> instead). Additionally, tag names in the input JSON file may be suffixed with a C<#> to disable print conversion. =item B<-l> (B<-long>) Use long 2-line Canon-style output format. Adds a description and unconverted value to the XML, JSON or PHP output when B<-X>, B<-j> or B<-php> is used. =item B<-L> (B<-latin>) Use Windows Latin1 encoding (cp1252) for output tag values instead of the default UTF-8. When writing, B<-L> specifies that input text values are Latin1 instead of UTF-8. Equivalent to C<-charset latin>. =item B<-lang> [I] Set current language for tag descriptions and converted values. I is C, C, C, etc. Use B<-lang> with no other arguments to get a list of available languages. The default language is C if B<-lang> is not specified. Note that tag/group names are always English, independent of the B<-lang> setting, and translation of warning/error messages has not yet been implemented. By default, ExifTool uses UTF-8 encoding for special characters, but the the B<-L> or B<-charset> option may be used to invoke other encodings. Currently, the language support is not complete, but users are welcome to help improve this by submitting their own translations. To submit a set of translations, first use the B<-listx> option and redirect the output to a file to generate an XML tag database, then add entries for other languages, zip this file, and email it to phil at owl.phy.queensu.ca for inclusion in ExifTool. =item B<-listItem> I For list-type tags, this causes only the item with the specified index to be extracted. I is 0 for the first item in the list. Has no effect when writing or copying tags, in a B<-if> condition, or in combination with structured output options. =item B<-n> (B<--printConv>) Read and write values as numbers instead of words. By default, extracted values are converted to a more human-readable format for printing, but the B<-n> option disables this print conversion for all tags. For example: > exiftool -Orientation -S a.jpg Orientation: Rotate 90 CW > exiftool -Orientation -S -n a.jpg Orientation: 6 The print conversion may also be disabled on a per-tag basis by suffixing the tag name with a C<#> character: > exiftool -Orientation# -Orientation -S a.jpg Orientation: 6 Orientation: Rotate 90 CW These techniques may also be used to disable the inverse print conversion when writing. For example, the following commands all have the same effect: > exiftool -Orientation='Rotate 90 CW' a.jpg > exiftool -Orientation=6 -n a.jpg > exiftool -Orientation#=6 a.jpg =item B<-p> I or I (B<-printFormat>) Print output in the format specified by the given file or string (and ignore other format options). Tag names in the format file or string begin with a C<$> symbol and may contain a leading group name and/or a trailing C<#>. Case is not significant. Braces C<{}> may be used around the tag name to separate it from subsequent text. Use C<$$> to represent a C<$> symbol, and C<$/> for a newline. Multiple B<-p> options may be used, each contributing a line of text to the output. Lines beginning with C<#[HEAD]> and C<#[TAIL]> are output only for the first and last processed files respectively. Lines beginning with C<#[BODY]> and lines not beginning with C<#> are output for each processed file. Other lines beginning with C<#> are ignored. For example, this format file: # this is a comment line #[HEAD]-- Generated by ExifTool $exifToolVersion -- File: $FileName - $DateTimeOriginal (f/$Aperture, ${ShutterSpeed}s, ISO $EXIF:ISO) #[TAIL]-- end -- with this command: exiftool -p test.fmt a.jpg b.jpg produces output like this: -- Generated by ExifTool 9.34 -- File: a.jpg - 2003:10:31 15:44:19 (f/5.6, 1/60s, ISO 100) File: b.jpg - 2006:05:23 11:57:38 (f/8.0, 1/13s, ISO 100) -- end -- When B<-ee> (B<-extractEmbedded>) is combined with B<-p>, embedded documents are effectively processed as separate input files. If a specified tag does not exist, a minor warning is issued and the line with the missing tag is not printed. However, the B<-f> option may be used to set the value of missing tags to '-' (but this may be configured via the MissingTagValue API option), or the B<-m> option may be used to ignore minor warnings and leave the missing values empty. An advanced formatting feature allows an arbitrary Perl expression to be applied to the value of any tag by placing it inside the braces after a semicolon following the tag name. The expression has access to the value of this tag through the default input variable (C<$_>), and the full API through the current ExifTool object (C<$self>). It may contain any valid Perl code, including translation (C