Track number format in paths

I’m afraid, the answer exist already but I search the wrong terms.
Anyway, I’d like to insure for example 3 digit track number in path expressions of the config.yaml.

Is there an easy way to expand $track with padding zeroes if necessary ?

Actually I get with “$albumartist/$album%aunique{}/$disc-$track $title” as soon as I have more than 99 tracks a wrong order .

Hi! One simple way would be to use the inline plugin, which would let you use Python formatting to get this effect. For example, '{:03n}'.format(track) gives me three digits, padded with zeroes.

Great, thanks ! ( I’d never located that solution on my own )

1 Like

Hi,
now I am a few steps ahead but still kind of confused.

I applied the example above like that:

plugins: convert lastgenre edit fetchart inline
item_fields:
threeDigitTrack: ‘{:03n}’.format(track)
paths:
default: $albumartist/$album%aunique{}/$threeDigitTrack_$title

But this gives a syntax problem:

configuration error: file /home/mpcww/.config/beets/hoerbuch/config_hoerbuch.yaml could not be read: while parsing a block mapping
in “/home/mpcww/.config/beets/hoerbuch/config_hoerbuch.yaml”, line 29, column 9
expected , but found ‘’
in “/home/mpcww/.config/beets/hoerbuch/config_hoerbuch.yaml”, line 29, column 34

In the meantime I encountered the docu for “inline plugin”

According the example I changed the syntax to

item_fields:
threeDigitTrack: u’%03i’ % (track)
paths:
default: $albumartist/$album%aunique{}/$threeDigitTrack_$title

Syntax seemed to be ok but the result is not resolved:

$threeDigitTrack_Track_001.mp3

I decided to apply the example:

paths:
default: $albumartist/$album%aunique{}/‘$disc_and_track’_$title
item_fields:
disc_and_track: u’%02i.%03i’ % (disc, track) if disctotal > 1 else u’%03i’ % (track)

with little changes:
Due to underscore in disc_and_track I had to use ‘’'.
I change 2i to 3i for the track part.

The result looks kind of closer to what I want:

_001__Track_001.mp3
_017__Track_017.mp3

However now I have the leading ‘_’ in front of the tracknumber and (!) an additional wone after it and I not even see how it is produced ?

I hope I don’t have to learn python, do I ? :wink:

Regarding the syntax of padding I assume my pthyon version doesn’t support it:

python -V
Python 2.7.13

But I guessed wrong, actually it works:

Welcome to Python 2.7! This is the online help utility.
[…]
‘{:03n}’.format(11)
‘011’

From the beginning of the post (syntax error) there is the question why beets or “inline plugine” are complaing about the new syntax:

configuration error: file …/config_hoerbuch.yaml could not be read: while parsing a block mapping
in “…config_hoerbuch.yaml”, line 29, column 9
expected , but found ‘’
in “…config_hoerbuch.yaml”, line 29, column 34

and force me to switch to the old one

item_fields:
# threeDigitTrack: ‘{:03n}’.format(track)
threedigittrack: u’%03d’ % (track,)

Please use code blocks, not quotes, to paste your config syntax.

You’re seeing a YAML parse error. You can read more about YAML syntax elsewhere on the internet. Here, you might try surrounding the Python expression with quotes (‘ or “).

Sorry, but I don’t have “code blocks” as edit option, just “hyperlink, block quote, preformatted text”, which ist surroundd by “Emphasis” left and “Upload” on the right.

The “preformatted text” button gives you a code block.

Using it this way : threedigittrack: "'{:03n}'.format(track)" worked!
However, the “old” syntax threedigittrack: u’%03d’ % (track,) didn’t need extra quotation.

Both deliver the same result now with the extra ‘underscores’ which is ok.

Anyway, I don’t wan’t to undestand the details of yaml’s digestion for that purpose.

Thank you again for the hint and the slim solution .

I think the extra underscores in your paths are arising because of the extra quotes you have in your path formats, like this one:

default: $albumartist/$album%aunique{}/’$disc_and_track’_$title

You could be right !
I introduced it while fumbeling aorund with those parsing errors, hence without it

default: $albumartist/$album%aunique{}/$disc-$threedigittrack_$title`
and
threedigittrack: ‘’’{:03n}’’.format(track)’
results with:
00-$threedigittrack_Track_162.mp3

I’ll check the other quoation-form
default: $albumartist/$album%aunique{}/$disc-"$threedigittrack"_$title
and rececive the well-known :
00-_162__Track_162.mp3

Quotation ( " or ’ ) seem to be inevitable in the path, doesn’t it ?

Again, it’s super helpful if you paste you configuration in code blocks. Otherwise, the indentation is removed and it’s hard to see what you actually wrote.

It would be useful to look at general documentation about YAML. Putting quotes inside a value includes those quotes in the string; surrounding a value with quotes just delimits the string.

I don’t have the edit option for block quotes here (hence I didn’t apply any other neither ).
I’d like to use a screenshot, but I don’t know how to show my edit menu .

Let’s leave it how it is now.

```yaml
your config goes here
```

You can use 3 backticks (see above) as well if you want to paste your config file.

Can you please show us your config file with this method as well as the filenames you get and and filenames you want.

I don’t clearly understand your problem, but you can try to surround threedigittrack with curly braces like this: ${threedigittrack}
The problem might be that the parse cannot find the variable named threedigittrack_ and simply uses the string itself in the filename. This is because it doesn’t know where the variable name ends ( “_” might be a valid character for variable names).

This is also mentioned in the wiki:
${title} is equivalent to $title; you can use this if you need to separate a field name from the text that follows it.
However I can see how it can benefit from, a clearer description, as it is non-trivial to figure out (for non-programmers) the meaning, its implications, and which errors it might cause if used incorrectly.

As far as I can see, currently for you the " act as a separator for the variable name (which should be solved with the curly braces), and then get converted to underscores because they’re invalid characters for filenames.

1 Like

Thank you, the curly braces did it finally !

For excercise purposes I paste the whole but small config now and how I use it:

plugins: convert lastgenre edit fetchart inline
directory: /marvin/mucke/hoerbuch/
library: library_hoerbuch.db
replace:
    ' ': _
    '[\\/]': _ 
    '^\.': _
    '[\x00-\x1f]': _   
    '[<>:"\?\*\|;,\.]': _ 
    '[\(\)]': __ 
    '\.$': _ 
    '\s+$': ''  
    '^\s+': ''  
    '&': _
    '\''' : _ 
    '\’' : _ 
log: /marvin/mucke/beet_hoerbuch.log
languages: en de es
import:
     languages: de en
     media: CD
convert:
   dest: ~/converted_music
   copy_album_art: yes
   formats:
    mp3: ffmpeg -i $source -acodec libmp3lame -b:a 320k $dest
per_disc_numbering: yes
item_fields:
        threedigittrack: "'{:03n}'.format(track)"
#        threedigittrack: u'%03d' %  (track,)
paths:
    default: $albumartist/$album%aunique{}/$disc-${threedigittrack}_$title
#	default: "$albumartist/$album%aunique{}/$disc-$threedigittrack_$title"
#   default: $albumartist/$album%aunique{}/$disc-$threedigittrack_$title
#   default: $albumartist/$album%aunique{}/'$disc_and_track'_$title
lastgenre:
    auto:  yes 
    whitelist:  ~/.config/beets/whitelist.yaml
    canonical:  ~/.config/beets/genres-tree.yaml
    force: no
edit:
    albumfields: album albumartist genre
fetchart:
    minwidth: 500

There you can see the variants I tried to have a file name very close to the original default path,
but with at leas three digits for the track with padding zeros.
That is exactly what I get now: 00-001_Track_001.mp3

So thats fine.
The file itself doesn’t have a lot of tags. (no musicbrainz id … )
id3v2 --list-rfc822 00-001_Track_001.mp3 -:

Filename: 00-001_Track_001.mp3
TIT2: Track 001
TPE1: <artistname>
TPE2: <artistname>
TALB: <albumtitle>
TYER: 2011
TCON: Unknown (183)
TRCK: 001
00-001_Track_001.mp3: No ID3v1 tag
`` 
As you notice it is not the standard config.yaml .
I created a separated configuration for audiobooks and here tracks > 100 isn't so unusually.

Nevertheless I had to rename the standard config.yaml as well because even with --config full-path to config_hoerbuch.yaml , I had the experience that both where used .

So this is actually my configuration tree:

├── beetsConfig
│ ├── config.yaml
│ ├── library.db
│ └── state.pickle
├── config_albumartist.yaml
├── config_mucke.yaml
├── genres-tree.yaml
├── hoerbuch
│ └── config_hoerbuch.yaml
├── library.db
├── library_hoerbuch.db
├── state.pickle
└── whitelist.yaml

1 Like