Fixed field width number in multiple cover art file names of the same type

I’m using a simple script for naming cover art files, namely:
$if($eq(%coverart_maintype%,front),cover,%coverart_maintype%)

If there are multiple images of the same maintype, Picard automatically appends a number in parenthesis, e.g., “booklet (1).jpg”. But I’d like to make the numeric portion a zero-padded fixed field width – e.g., “booklet (01).jpg”.

Even better, I’d rather replace the automatic numbering altogether with something in the script that doesn’t use white space and parentheses, which are a pain on the Linux command line. For example:
$if($eq(%coverart_maintype%,front),cover,%coverart_maintype%-$num(%coverart_pagenum%,2))
But, I don’t know what variable name (instead of my hypothetical “coverart_pagenum” example) that can be used in the script.

Is there any way to achieve this?

No - there is no way to achieve this with scripts.

Based on a quick think through, I believe it might be possible to achieve this with a plugin that uses the " file_post_save_processor" event hook, but this would involve writing python code rather than a script.

In essence after a file has been saved, you can extract the path for the file and then use standard python functionality to scan the directory for album art and rename the files the way you want them. A few caveats:

  1. I haven’t checked the code to see how Picard handles saving album art files, so I am unsure what triggers saves (i.e. album save vs. track save or both), whether the album art save is done before or after the music files are saved i.e. before or after the plugin is called, or whether (unexpectedly) album art is saved for every track i.e. multiple times per album overwriting the previous set. If album art is saved after files, then you might need to queue the code on a timer to work after the saves are all done. You may also need to queue the tasks onto a worker thread in order to avoid blocking file activities making the UI less responsive.

  2. The code would be called once per file i.e. once per track, so you for efficiency you would probably need to do something to keep the code from running more than once per album.

  3. It is also quite possible that this would result in duplicate files the next time you saved the album because the new save would reuse the older file names, which would then get renamed etc. So you would probably need to include deduplication code in the plugin too.

In other words, this would be a non-trivial piece of python coding.

1 Like

Maybe it would be a good idea to have variables for this. Maybe %artwork_number% for a total count which piece of artwork this is and %artwork_type_number% for the count only for this specific type (cod e.g. be useful for booklets). Not sure how to handle multi type for the latter case, though.

1 Like

Thanks for the suggestion and the quick analysis of the complexities.

Based on these caveats, though, I think it would be a lot simpler to do the scripting outside of Picard entirely, either python or bash. That got me thinking, and now I almost feel silly for asking in the first place because I think I’ve got a suitable script down to a one line bash command:
find MyMP3directory -name “*\ ([0-9]).jpg” -exec rename ‘s/ (([0-9]))/-0$1/’ {} ;

It’s simple, but it seems to work for my setup. Now, I can run this incrementally (e.g., every night) as I slowly work my way through tagging my entire collection. Problem solved.

Thanks again.

Hidden variables I think.

However, @DirtwaterFox wants to be able to set the cover art filename from a script, so whilst these might be useful, they would not achieve his objective.

It doesn’t really matter here. Those are no tags written somewhere. The variables here only exist in the context of this particular script for generating a cover art file name. It’s similar to e.g. %coverart_maintype%, see also http://picard-docs.musicbrainz.org/en/config/options_location.html#ref-local-images

@DirtwaterFox So to answer your question: No, currently there is no %coverart_pagenum% or similar variable, but I think that’s a good thing to have.

I have added https://tickets.metabrainz.org/browse/PICARD-2142 for that.

1 Like