ReNamer:Scripts:Exiv2

From den4b Wiki
Revision as of 15:36, 25 November 2015 by Den4b (talk | contribs) (Navigation)
Jump to navigation Jump to search

Script integrates Exiv2 library in order to extract EXIF/IPTC/XMP tags from many types of images. CRW, CR2, RAW images are also supported.

Requirements

  • ReNamer v5.70
  • exiv2.exe v0.23 in ReNamer's folder

Code 1

Author: Denis Kozlov. Date: 2013-04-01. Extract unformatted line out of the command line output of the exiv2.exe tool. This script extracts Image Timestamp. Adjust EXIV and TAG constants to extract other tags.

Hint: Image Timestamp value is usually formatted as "yyyy:mm:dd hh:mm:ss" which contains an illegal filename character ":". The illegal character can be easily replaced using an additional Replace rule.

{ Extract EXIF/IPTC/XMP tags using Exiv2 }
 
const
  EXE = 'exiv2.exe';
  TAG = 'Image timestamp\s*\:\s*(.*?)[\r\n]';
 
var
  Command, Output: String;
  Matches: TStringsArray;  
 
begin
  Command := EXE+' "'+FilePath+'"';
  if ExecConsoleApp(Command, Output) = 0 then
  begin
    Matches := SubMatchesRegEx(Output, TAG, False);
    if Length(Matches) > 0 then
      FileName := Matches[0] + WideExtractFileExt(FileName);
  end;
end.

To extract IPTC Headline tag, you would need to make following changes:

EXE = 'exiv2.exe -pi';
TAG = 'Iptc.Application2.Headline\s*\w*\s*\d*\s*(.*?)[\r\n]';

Code 2

Author: Denis Kozlov. Date: 2015-11-25. Extract EXIF date and reformat it according to user specification (DATE_FORMAT constant).

const
  EXIV = 'exiv2.exe';
  DATE_TAG = 'Image timestamp\s*\:\s*(.*?)[\r\n]';
  DATE_PARTS = '(.+)\:(.+)\:(.+)\ (.+)\:(.+)\:(.+)';
  DATE_FORMAT = 'YYYY-MM-DD HH.NN.SS';

var
  Command, Output: String;
  Matches, DateParts: TStringsArray;
  Year, Month, Day, Hours, Minutes, Seconds: Integer;
  Date: TDateTime;
  DateOK: Boolean;

begin
  Command := EXIV + ' "' + FilePath + '"';
  if ExecConsoleApp(Command, Output) = 0 then
  begin
    Matches := SubMatchesRegEx(Output, DATE_TAG, False);
    if Length(Matches) > 0 then
    begin
      DateParts := SubMatchesRegEx(Matches[0], DATE_PARTS, False);
      if Length(DateParts) > 0 then
      begin
        DateOK :=
          TryStrToInt(DateParts[0], Year) and TryStrToInt(DateParts[1], Month) and
          TryStrToInt(DateParts[2], Day) and TryStrToInt(DateParts[3], Hours) and
          TryStrToInt(DateParts[4], Minutes) and TryStrToInt(DateParts[5], Seconds);
        if DateOK then
        begin
          Date := EncodeDateTime(Year, Month, Day, Hours, Minutes, Seconds, 0);
          FileName := FormatDateTime(DATE_FORMAT, Date) + WideExtractFileExt(FileName);
        end;
      end;
    end;
  end;
end.