Skip to content

Instantly share code, notes, and snippets.

@ahacke
Last active July 28, 2023 02:15
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahacke/23c7b51b8811c78712b1068a86900760 to your computer and use it in GitHub Desktop.
Save ahacke/23c7b51b8811c78712b1068a86900760 to your computer and use it in GitHub Desktop.
Contains exiftool cheats and tricks, mostly used for Synology Photostation

Exiftool Cheats (for Synology Photostation).md

Contains scripts to modify exif metadata on media files. Generally, I used them for the synology photostation.

Another good reference with exif tricks: https://gist.github.com/rjames86/33b9af12548adf091a26

All of the commands or scripts require ExifTool by Phil Harvey.

Recommended Order of Running the Commands and Scripts

  1. Set CreateDate for Video-Media
  2. Set 'CreateDate' Based on FileName Supporting Different Formats
  3. Rename Files based on 'CreateDate'
  4. Copy All Files Without 'CreateDate' to SubDir

Shows all Metadata Time Tags with Their Groups

exiftool -a -s -G1 -time:all .

Set DateTimeOriginal Based On CreateDate

Sets DateTimeOriginal based on CreateDate if the DateTimeOriginal is not present and CreateDate is present.

exiftool -r -if '(not $DateTimeOriginal)' -if '($CreateDate)' "-DateTimeOriginal<CreateDate" -overwrite_original .

Rename Files based on 'CreateDate'

Renames all files which have a CreateDate tag to the following format: YYYY-MM-DD-HHMMSS, e.g. 2019-02-25-180220.jpg.

exiftool -d %Y-%m-%d-%H%M%S%%-c.%%e -r "-filename<CreateDate" .

Copy All Files Without 'CreateDate' to SubDir

exiftool -r -if '(not $createdate)' "-filename=%d/no_createDate/%f.%e" .

Rename File Extension to FileTypeExtension

Renames image.jpeg to image.jpg.

exiftool '-filename<%f.$fileTypeExtension' .

Set CreateDate Based on FileName

Uses the filename to set the CreateDate.

exiftool -r "-createDate<filename" .

Set 'CreateDate' Based on FileName Supporting Different Formats

Sets the CreateDate metadata tag based on the filename. It supports different types of filenames, e.g. WhatsApp and Signal mobile app patterns.

#!/bin/sh
echo "~~~~~~~~~ Fix No 'Create Date' Script ~~~~~~~~~"
echo ""

path=$1
if [ -z "$path" ]
then
	echo "Please specify first argument: directory to fix"
	exit
else
	echo "Fixing dates in directory: $path"
fi

script_execution_directory=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
echo "Script execution directory: $script_execution_directory"

echo ""
echo "Switching to path: $path"
cd $path

for filename in *.jpg; do
    [ -e "$filename" ] || continue
	echo ""

	if [[ "$filename" =~ WA ]]
	then
		echo "WhatsApp match: $filename"
		exiftool -overwrite_original '-CreateDate<${filename;$_=substr($_,4,12)} 00:00:00' "$filename"
	elif [[ "$filename" =~ signal ]]
	then
		echo "Signal match: $filename"
		exiftool -overwrite_original '-CreateDate<${filename;$_=substr($_,7,17)}'  "$filename"
	else
		echo "No match: $filename"
	fi	
done

exiftool -d %Y-%m-%d-%H%M%S%%-c.%%e -r "-filename<CreateDate" .

Set CreateDate for Video-Media

Motivation

The Synology photostation uses specific metadata per file type and date sort option (e.g. date taken, date created) to sort the files. These metadata is not always available or changed by the photostation for uploaded media files leading to an incorrect sorting for date options. Albums containing images and videos end up in an incorrect sorted variant.

For example, for video files and sorting option date taken the photostation uses the metadata FileModifyDate. But if the video files are uploaded to the photostation, this metadata tag is set to the upload time. The sorting will be completely off.

Solution

As the photostation uses different media metadata to sort the media depending on the file type and not all systems (e.g. cameras or the photostation itself) use or correctly create these files, a modification to the metadata of these files is necessary.

The script does the following:

  1. looks for video files of defined video formats in the specified path
  2. copies these files to the directory where the script is executed
  3. updates the FileModifyDate
  4. moves the files back to the specified path

It is important to copy the files and overwrite the source files later for the photostation to start the indexing automatically again.

Script

#!/bin/sh
echo "~~~~~~~~~ Set CreateDate Script ~~~~~~~~~"
echo ""

path=$1
if [ -z "$path" ]
then
	echo "Please specify first argument: directory to fix"
	exit
else
	echo "Fixing dates in directory: $path"
fi

script_execution_directory=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
echo "Script execution directory: $script_execution_directory"

echo ""
echo "Switching to path: $path"
cd $path

for filename in *.mp4 *.m4v *.mov; do
    [ -e "$filename" ] || continue
	echo ""
	echo "Working on $filename"
	
	filename_CreateDate=$(exiftool '-CreateDate' '-s' '-s' '-s' "$filename")
	filename_FileModifyDate=$(exiftool '-FileModifyDate' '-s' '-s' '-s' "$filename")
	filename_FileModifyDateWithoutTimeZone=$(echo $filename_FileModifyDate | cut -d'+' -f 1) 
	
	if [ "$filename_CreateDate" = "$filename_FileModifyDateWithoutTimeZone" ] 
	then
		echo "	IGNORE: CreateDate and FileModifyDate are equal."
	else
		filename_temp="$script_execution_directory/$filename"
		echo "	Copy NAS => LOCAL: $filename to $filename_temp"
		cp "$filename" "$filename_temp"

		echo "	Modifying Exif Metada"
		exiftool -q -overwrite_original -if '$ContentCreateDate' '-ContentCreateDate>CreateDate' "$filename_temp"
		echo "		Copy CreateDate to FileModifyDate"
		exiftool -q -overwrite_original '-CreateDate>FileModifyDate' "$filename_temp"

		echo "	Move NAS <= LOCAL: $filename_temp to $filename"
		mv "$filename_temp" "$filename"
	fi	
done

Synology Photostation Behavior

The scripts are written for Photostation 6 but may also work for other versions.

The official documentation does not provide any information about the tags used by the Synology photostation for each date sorting option.

Used Media Metadata Per Sorting Option

The photostation uses different media metadata tags to sort the media depending the file type.

  • date taken:
    • mp4, m4v, mov
      1. FileModifyDate without timezone suffix
    • jpg
      1. DateTimeOriginal
      2. Filename (if DateTimeOriginal is not present)

Used Media Metadata Source

The following metadata is used to fix the sorting.

  • date taken
    • mp4, m4v, mov
      1. ContentCreateDate (if available)
      2. CreateDate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment