Preserving flac hash

I convert my lossless music to Apple Lossless (ALAC) for easy playback on my Apple devices. The sourcefiles are mostly FLAC files. In order to verify the music conversion and check for bitrot later on, one could extract the md5 hash from the FLAC source file and add it as Item Flexible Attribute for later verification.

The metaflac binary (part of flac) alllows for md5 extraction with the command:
metaflac --show-md5sum FLACfile
and ffmpeg can calculate a files (audiostream) md5 using the command:
ffmpeg -i Musicfile -vn -f hash -hash md5 -

I have been trying to create a conversion script for the convert plugin to extract the flac’s hash and write it to the beets library, but have not succeeded so far. This is the convert command:
command: /Users/user/ $source $dest
and this is the conversion script:
function source_hash { metaflac --show-md5sum --no-filename $1 } src_hash=$( source_hash $1 ) beet modify 'source_hash='$src_hash path:$1 ffmpeg -i $1 -y -vn -acodec alac $2

How could one go about to get the desired behavior? Is this possible using the convert plugin?
Any help with this would be very welcome.

Cool! This looks pretty good so far. Any chance you can expand on exactly what’s not working? For example, are you able to run the script manually with hand-crafted arguments to make it do what you want?

Ah I found the problem, there was a -y missing, the corrected script is:

function source_hash {
        metaflac --show-md5sum --no-filename $1
src_hash=$( source_hash $1 )
beet modify -y 'source_hash='$src_hash path:$1
ffmpeg -i $1 -y -vn -acodec alac $2

I have not yet tested it in a multi-threaded conversion where I am not sure whether variables could get mixed up.

With a verification script this could mimic flac -t functionality for non flac lossless files. In case the source is not flac and does not have a hash, I plan to verify the files using XLD that can open a folder of music files as a disc and have them checked against the AccurateRip database (no cue file required). When successfully verified I can use ffmpeg to calculate a hash over the music data of the source files.