Apple - Copy extended attributes to new file (ffmpeg)

I needed this too—also after running FFmpeg!—so I wrote a shell script to do it. It’s not very quick, and it won’t work if any individual attribute contains more than about 128 KB of data. (Behind the scenes, it’s encoding each attribute value in hexadecimal to make sure that binary data is copied correctly.) It worked fine for my needs, though.

Save this to a file called copy_xattrs:

#!/usr/bin/env zsh

if [[ $# -ne 2 ]]; then
    print >&2 "usage: copy_xattrs SOURCE_FILE TARGET_FILE"
    exit 1
fi

set -e

IFS=$'\n' attr_names=($(xattr "$1"))
for attr in $attr_names; do
    value=$(xattr -p -x "$attr" "$1" | tr -d " \n")
    xattr -w -x "$attr" "$value" "$2"
done

Then run chmod u+x copy_xattrs to make it executable. Now you can run copy_xattrs source target to copy all of the extended attributes from the file source to the file target.