Skip to content

Instantly share code, notes, and snippets.

@joshskidmore
Last active February 15, 2024 15:14
Show Gist options
  • Save joshskidmore/9c27332d5914b72ab74ebc89e1216542 to your computer and use it in GitHub Desktop.
Save joshskidmore/9c27332d5914b72ab74ebc89e1216542 to your computer and use it in GitHub Desktop.
img-url-exif.sh
#!/usr/bin/env bash
# Josh Skidmore <josh@josh.sc>
# Basic example of using an HTTP range header to extract EXIF data
# from a JPEG file without having to download the entire image.
# This has no error handling and assumes the EXIF data is embedded
# within the first $KB_TO_DOWNLOAD kilobytes of the image
# Requirements:
# * curl
# * exiftool
# Example JPEGs:
# https://raw.githubusercontent.com/ianare/exif-samples/master/jpg/Ricoh_Caplio_RR330.jpg
# https://live.staticflickr.com/3866/14356072311_4ef21a1001_o_d.jpg
[[ -z "$1" ]] && \
echo "Usage: $(basename $0) [JPEG_URL]" && \
exit
IMG_URL=$1
KB_TO_DOWNLOAD=100
B_TO_DOWNLOAD=$((KB_TO_DOWNLOAD*1024))
TMP_DIR=$(mktemp -d)
TMP_IMG=$TMP_DIR/temp.jpg
THU_IMG=$TMP_DIR/thumbnail.jpg
EXIF_DUMP_FILE=$TMP_DIR/exif.txt
# grab the image with a range query
curl -s -o $TMP_IMG --header "Range: bytes=0-${B_TO_DOWNLOAD}" $IMG_URL
# (debugging) determine exif starting byte range
exif_starting_byte=$(cat $TMP_IMG | grep -a -bo Exif | cut -d : -f 1)
# extract exif data
exif_data=$(exiftool $TMP_IMG)
# dump exif to file
echo -e "$exif_data" > $EXIF_DUMP_FILE
# dump exif to console
echo "#### START EXIF ####"
echo -e "$exif_data"
echo "#### END EXIF ####"
echo
echo "Image URL: $IMG_URL"
echo "Bytes Transferred: $B_TO_DOWNLOAD"
echo "EXIF Starting Marker Byte: $exif_starting_byte"
echo "EXIF Dump: $EXIF_DUMP_FILE"
# if an exif thumbnail is available, extract
[[ "$exif_data" =~ Thumbnail ]] && \
exiftool -b -ThumbnailImage $TMP_IMG > $THU_IMG && \
echo "Extracted Thumbnail: $THU_IMG"
@joshskidmore
Copy link
Author

@almoorthi Videos are a bit more difficult than this. Can you provide an example URL to a video?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment