#------------------------------------------------------------------------------
# File:         dji.config
#
# Description:  This config file defines Composite tags to convert embedded
#               metadata in videos from DJI drones
#
# Usage:        exiftool -config dji.config -ee FILE
#
#               Example command to create .gpx log file from DJI video
#               (requires gpx.fmt available in the full Exiftool distribution):
#
#               exiftool -config dji.config -p gpx.fmt -ee -api QuickTimeUTC FILE
#
# Requires:     ExifTool version 10.75 or later
#
# Revisions:    2018/03/23 - P. Harvey Created
#------------------------------------------------------------------------------

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        GROUPS => { 2 => 'Location' },
#
# Example embedded "Text" from a DJI FC6310:
#
#   "F/3.5, SS 1000, ISO 100, EV 0, GPS (8.6499, 53.1665, 18), D 24.26m,
#    H 6.00m, H.S 2.10m/s, V.S 0.00m/s \n"
#
#   F/  = F Number
#   SS  = shutter speed
#   ISO = ISO
#   GPS = (longitude, latitude, ???)
#   D   = horizontal distance from home point
#   H   = vertical distance from home point
#   H.S = horizontal  speed
#   V.S = vertical speed
#
# Note: SubDoc flag is set for all these tags so they will be generated for
#       all embedded documents
#
        GPSDateTime => {
            Description => 'GPS Date/Time',
            Groups => { 2 => 'Time' },
            SubDoc => 1,
            Require => {
                0 => 'Text',
                1 => 'SampleTime',
                2 => 'Main:CreateDate',
                3 => 'Main:Duration',
            },
            # (assuming CreateDate is the end time of the video, we subtract
            #  Duration because a SampleTime of zero is at the start of the video)
            ValueConv => q{
                my $diff = $val[1] - $val[3];
                my $sign = $diff =~ s/^-// ? '-' : '';
                my $time = $val[2] . '.000';
                ShiftTime($time, "${sign}0:0:$diff");
                return $time;
            },
            PrintConv => '$self->ConvertDateTime($val)',
        },
        GPSLatitude => {
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /GPS \(\S+,\s*([-+]?\d*\.\d+)/ ? $1 : undef',
            PrintConv => 'Image::ExifTool::GPS::ToDMS($self, $val, 1, "N")',
        },
        GPSLongitude => {
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /GPS \(([-+]?\d*\.\d+),/ ? $1 : undef',
            PrintConv => 'Image::ExifTool::GPS::ToDMS($self, $val, 1, "E")',
        },
        GPSAltitude => {
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /,\s*H\s+([-+]?\d+\.?\d*)m/ ? $1 : undef',
        },
        GPSSpeed => {
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /,\s*H.S\s+([-+]?\d+\.?\d*)/ ? $1 * 3.6 : undef',
        },
        GPSSpeedRef => {
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /,\s*H.S\s+([-+]?\d+\.?\d*)m\/s/ ? "K" : undef',
            PrintConv => { K => 'km/h', M => 'mph', N => 'knots' },
        },
        Distance => {
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /,\s*D\s+(\d+\.?\d*)m/ ? $1 * 3.6 : undef',
            PrintConv => '"$val m"',
        },
        VerticalSpeed => {
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /,\s*V.S\s+([-+]?\d+\.?\d*)/ ? $1 : undef',
            PrintConv => '"$val m/s"',
        },
        FNumber => {
            Groups => { 2 => 'Camera' },
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /\bF\/(\d+\.?\d*)/ ? $1 : undef',
            PrintConv => 'Image::ExifTool::Exif::PrintFNumber($val)',
        },
        ExposureTime => {
            Groups => { 2 => 'Camera' },
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /\bSS\s+(\d+\.?\d*)/ ? 1/$1 : undef',
            PrintConv => 'Image::ExifTool::Exif::PrintExposureTime($val)',
        },
        ExposureCompensation => {
            Groups => { 2 => 'Camera' },
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /\bEV\s+([-+]?\d+\.?\d*)(\/\d+)?/ ? ($1 / ($2 || 1)) : undef',
            PrintConv => 'Image::ExifTool::Exif::PrintFraction($val)',
        },
        ISO => {
            Groups => { 2 => 'Camera' },
            SubDoc => 1,
            Require => { 0 => 'Text' },
            RawConv => '$val[0] =~ /\bISO\s+(\d+\.?\d*)/ ? $1 : undef',
        },
    },
);

1;  #end
