The pre-commit-annex hook script that automatically extracts metadata has been updated to also use exiftool. Thanks, Klaus Ethgen.
This commit is contained in:
parent
b946ca44c3
commit
764e57f1f1
4 changed files with 129 additions and 50 deletions
3
debian/changelog
vendored
3
debian/changelog
vendored
|
@ -19,6 +19,9 @@ git-annex (6.20160218) UNRELEASED; urgency=medium
|
||||||
to match ranges of numeric values.
|
to match ranges of numeric values.
|
||||||
* Similarly, support preferred content expressions like
|
* Similarly, support preferred content expressions like
|
||||||
metadata=field<number and metadata=field>number
|
metadata=field<number and metadata=field>number
|
||||||
|
* The pre-commit-annex hook script that automatically extracts
|
||||||
|
metadata has been updated to also use exiftool.
|
||||||
|
Thanks, Klaus Ethgen.
|
||||||
|
|
||||||
-- Joey Hess <id@joeyh.name> Thu, 18 Feb 2016 13:09:21 -0400
|
-- Joey Hess <id@joeyh.name> Thu, 18 Feb 2016 13:09:21 -0400
|
||||||
|
|
||||||
|
|
5
debian/copyright
vendored
5
debian/copyright
vendored
|
@ -44,6 +44,11 @@ Copyright: 2001 Ian Lynagh
|
||||||
2010-2015 Joey Hess <id@joeyh.name>
|
2010-2015 Joey Hess <id@joeyh.name>
|
||||||
License: GPL-3+
|
License: GPL-3+
|
||||||
|
|
||||||
|
Files: doc/tips/automatically_adding_metadata/pre-commit-annex
|
||||||
|
Copyright: 2014 Joey Hess <id@joeyh.name>
|
||||||
|
2016 Klaus Ethgen <Klaus@Ethgen.ch>
|
||||||
|
License: GPL-3+
|
||||||
|
|
||||||
Files: Utility/libmounts.c
|
Files: Utility/libmounts.c
|
||||||
Copyright: 1980, 1989, 1993, 1994 The Regents of the University of California
|
Copyright: 1980, 1989, 1993, 1994 The Regents of the University of California
|
||||||
2001 David Rufino <daverufino@btinternet.com>
|
2001 David Rufino <daverufino@btinternet.com>
|
||||||
|
|
|
@ -2,23 +2,48 @@ git-annex's [[metadata]] works best when files have a lot of useful
|
||||||
metadata attached to them.
|
metadata attached to them.
|
||||||
|
|
||||||
To make git-annex automatically set the year and month when adding files,
|
To make git-annex automatically set the year and month when adding files,
|
||||||
run `git config annex.genmetadata true`.
|
run: `git config annex.genmetadata true`
|
||||||
|
|
||||||
|
## git commit hook
|
||||||
|
|
||||||
A git commit hook can be set up to extract lots of metadata from files
|
A git commit hook can be set up to extract lots of metadata from files
|
||||||
like photos, mp3s, etc.
|
like photos, mp3s, etc. Whenever annexed files are committed, their
|
||||||
|
metadata will be extracted and stored.
|
||||||
|
|
||||||
1. Install the `extract` utility, from <http://www.gnu.org/software/libextractor/>
|
Download [[pre-commit-annex]] and install it in your git-annex repository
|
||||||
`apt-get install extract`
|
as `.git/hooks/pre-commit-annex`
|
||||||
2. Download [[pre-commit-annex]] and install it in your git-annex repository
|
Remember to make the script executable! `chmod +x .git/hooks/pre-commit-annex`
|
||||||
as `.git/hooks/pre-commit-annex`.
|
|
||||||
Remember to make the script executable!
|
|
||||||
3. Run: `git config metadata.extract "artist album title camera_make video_dimensions"`
|
|
||||||
|
|
||||||
Now any fields you list in metadata.extract to will be extracted and
|
### using extract
|
||||||
stored when files are committed.
|
|
||||||
|
The git commit hook can use extract to get metadata.
|
||||||
|
|
||||||
|
Install it from <http://www.gnu.org/software/libextractor/>
|
||||||
|
`apt-get install extract`
|
||||||
|
|
||||||
|
Configure which metadata fields to ask extract for: `git config metadata.extract "artist album title camera_make video_dimensions"`
|
||||||
|
|
||||||
To get a list of all possible fields, run: `extract -L | sed 's/ /_/g'`
|
To get a list of all possible fields, run: `extract -L | sed 's/ /_/g'`
|
||||||
|
|
||||||
|
### using exiftool
|
||||||
|
|
||||||
|
The git commit hook can also use exiftool to get metadata.
|
||||||
|
|
||||||
|
Install it from <http://owl.phy.queensu.ca/~phil/exiftool/>
|
||||||
|
`apt-get install libimage-exiftool-perl`
|
||||||
|
|
||||||
|
Configure which metadata fields to ask exiftool for: `git config metadata.exiftool "Model ImageSize FocusRange GPSAltitude GPSCoordinates"`
|
||||||
|
|
||||||
|
To get a list of all possible fields, run: `exiftool -list`
|
||||||
|
|
||||||
|
### using both extract and exiftool
|
||||||
|
|
||||||
|
If you want some metadata that extract knows about, and other metadata
|
||||||
|
that exiftool knows about, just install them both, and set both
|
||||||
|
`metadata.extract` and `metadata.exiftool`.
|
||||||
|
|
||||||
|
### overwriting existing metadata
|
||||||
|
|
||||||
By default, if a git-annex already has a metadata field for a file,
|
By default, if a git-annex already has a metadata field for a file,
|
||||||
its value will not be overwritten with metadata taken from files.
|
its value will not be overwritten with metadata taken from files.
|
||||||
To allow overwriting, run: `git config metadata.overwrite true`
|
To allow overwriting, run: `git config metadata.overwrite true`
|
||||||
|
|
|
@ -1,4 +1,20 @@
|
||||||
#!/bin/sh
|
#! /bin/sh
|
||||||
|
#
|
||||||
|
# Copyright (C) 2014 Joey Hess <id@joeyh.name>
|
||||||
|
# Copyright (C) 2016 Klaus Ethgen <Klaus@Ethgen.ch>
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
# This script can be used to add git-annex metadata to files when they're
|
# This script can be used to add git-annex metadata to files when they're
|
||||||
# committed. It is typically installed as .git/hooks/pre-commit-annex
|
# committed. It is typically installed as .git/hooks/pre-commit-annex
|
||||||
|
@ -6,67 +22,97 @@
|
||||||
# You can also run this script by hand, passing it the names of files
|
# You can also run this script by hand, passing it the names of files
|
||||||
# already checked into git-annex, and it will extract/refresh the git-annex
|
# already checked into git-annex, and it will extract/refresh the git-annex
|
||||||
# metadata from the files.
|
# metadata from the files.
|
||||||
#
|
|
||||||
# Copyright 2014 Joey Hess <id@joeyh.name>
|
|
||||||
# License: GPL-3+
|
|
||||||
|
|
||||||
extract="$(git config metadata.extract || true)"
|
tool="$(git config metadata.tool || :)"
|
||||||
want="$(perl -e 'print (join("|", map {s/_/ /g; "^$_ - "} (split " ", shift())))' "$extract")"
|
if [ -z "$tool" ]; then
|
||||||
|
tool=extract
|
||||||
if [ -z "$want" ]; then
|
|
||||||
exit 0
|
|
||||||
fi
|
fi
|
||||||
|
case "$tool" in
|
||||||
case "$(git config --bool metadata.overwrite || true)" in
|
exiftool)
|
||||||
true)
|
tool_exec="exiftool -unknown -zip -veryShort -ignoreMinorErrors -use MWG -dateFormat '%Y-%m-%dT%H:%M:%S'"
|
||||||
overwrite=1
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
overwrite=""
|
tool_exec="$tool"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
addmeta () {
|
extract_fields="$(git config metadata.extract || :)"
|
||||||
|
if [ -n "$extract_fields" ]; then
|
||||||
|
tools=extract
|
||||||
|
extract_want="^($(echo "$extract_fields" | sed -e 's/ /|/g' -e 's/_/ /g'))"
|
||||||
|
fi
|
||||||
|
exiftool_fields="$(git config metadata.exiftool || :)"
|
||||||
|
if [ -n "$exiftool_fields" ]; then
|
||||||
|
tools="exiftool $tools"
|
||||||
|
exiftool_want="^($(echo "$exiftool_fields" | sed -e 's/ /|/g' -e 's/_/ /g'))"
|
||||||
|
fi
|
||||||
|
if [ -z "$tools" ]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "$(git config --bool metadata.overwrite || :)" in
|
||||||
|
true)
|
||||||
|
equal="="
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
equal="?="
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if git rev-parse --verify HEAD >/dev/null 2>&1; then
|
||||||
|
against="HEAD"
|
||||||
|
else
|
||||||
|
# Initial commit: diff against an empty tree object
|
||||||
|
against="4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
||||||
|
fi
|
||||||
|
|
||||||
|
addmeta() {
|
||||||
file="$1"
|
file="$1"
|
||||||
field="$2"
|
field="$2"
|
||||||
value="$3"
|
value="$3"
|
||||||
afield="$(echo "$field" | tr ' ' _)"
|
afield="$(echo "$field" | tr ' ' '_' | tr 'A-Z' 'a-z')"
|
||||||
if [ "$overwrite" ]; then
|
git -c annex.alwayscommit=false annex metadata \
|
||||||
p="$afield=$value"
|
--set "$afield$equal$value" --quiet -- "$file"
|
||||||
|
|
||||||
else
|
|
||||||
p="$afield?=$value"
|
|
||||||
fi
|
|
||||||
git -c annex.alwayscommit=false annex metadata "$file" -s "$p" --quiet
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if git rev-parse --verify HEAD >/dev/null 2>&1; then
|
process() {
|
||||||
against=HEAD
|
|
||||||
else
|
|
||||||
# Initial commit: diff against an empty tree object
|
|
||||||
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
|
|
||||||
fi
|
|
||||||
|
|
||||||
IFS="
|
|
||||||
"
|
|
||||||
|
|
||||||
process () {
|
|
||||||
if [ -e "$f" ]; then
|
if [ -e "$f" ]; then
|
||||||
echo "adding metadata for $f"
|
echo "adding metadata for $f"
|
||||||
for l in $(extract "$f" | egrep "$want"); do
|
for tool in $tools; do
|
||||||
field="${l%% - *}"
|
case "$tool" in
|
||||||
value="${l#* - }"
|
exiftool)
|
||||||
addmeta "$f" "$field" "$value"
|
tool_exec="exiftool -unknown -zip -veryShort -ignoreMinorErrors -use MWG -dateFormat '%Y-%m-%dT%H:%M:%S'"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
tool_exec="$tool"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
LC_ALL=C $tool_exec "./$f" | eval egrep --text -i \""\$${tool}_want"\" | while read line; do
|
||||||
|
case "$tool" in
|
||||||
|
extract)
|
||||||
|
field="${line%% - *}"
|
||||||
|
value="${line#* - }"
|
||||||
|
;;
|
||||||
|
exiftool)
|
||||||
|
field="${line%%: *}"
|
||||||
|
value="${line#*: }"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if [ -n "$value" ]; then
|
||||||
|
addmeta "$f" "$field" "$value"
|
||||||
|
fi
|
||||||
|
done
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ -n "$*" ]; then
|
if [ -n "$*" ]; then
|
||||||
for f in $@; do
|
for f in "$@"; do
|
||||||
process "$f"
|
process "$f"
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
for f in $(git diff-index --name-only --cached $against); do
|
for f in "$(git diff-index --name-only --cached $against)"; do
|
||||||
process "$f"
|
process "$f"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue