Debian Codegraph @

docs/reference/pathformat.rst

raw
1 Path Formats
2 ============
3
4 The ``paths:`` section of the config file (see :doc:`config`) lets you specify
5 the directory and file naming scheme for your music library. Templates
6 substitute symbols like ``$title`` (any field value prefixed by ``$``) with the
7 appropriate value from the track's metadata. Beets adds the filename extension
8 automatically.
9
10 For example, consider this path format string: ``$albumartist/$album/$track
11 $title``
12
13 Here are some paths this format will generate:
14
15 - ``Yeah Yeah Yeahs/It's Blitz!/01 Zero.mp3``
16 - ``Spank Rock/YoYoYoYoYo/11 Competition.mp3``
17 - ``The Magnetic Fields/Realism/01 You Must Be Out of Your Mind.mp3``
18
19 Because ``$`` is used to delineate a field reference, you can use ``$$`` to emit
20 a dollars sign. As with `Python template strings`_, ``${title}`` is equivalent
21 to ``$title``; you can use this if you need to separate a field name from the
22 text that follows it.
23
24 .. _python template strings: https://docs.python.org/3/library/string.html#template-strings-strings
25
26 A Note About Artists
27 --------------------
28
29 Note that in path formats, you almost certainly want to use ``$albumartist`` and
30 not ``$artist``. The latter refers to the "track artist" when it is present,
31 which means that albums that have tracks from different artists on them (like
32 `Stop Making Sense`_, for example) will be placed into different folders!
33 Continuing with the Stop Making Sense example, you'll end up with most of the
34 tracks in a "Talking Heads" directory and one in a "Tom Tom Club" directory. You
35 probably don't want that! So use ``$albumartist``.
36
37 .. _stop making sense: https://musicbrainz.org/release/798dcaab-0f1a-4f02-a9cb-61d5b0ddfd36
38
39 As a convenience, however, beets allows ``$albumartist`` to fall back to the
40 value for ``$artist`` and vice-versa if one tag is present but the other is not.
41
42 .. _template-functions:
43
44 Template Functions
45 ------------------
46
47 Beets path formats also support *function calls*, which can be used to transform
48 text and perform logical manipulations. The syntax for function calls is like
49 this: ``%func{arg,arg}``. For example, the ``upper`` function makes its argument
50 upper-case, so ``%upper{beets rocks}`` will be replaced with ``BEETS ROCKS``.
51 You can, of course, nest function calls and place variable references in
52 function arguments, so ``%upper{$artist}`` becomes the upper-case version of the
53 track's artists.
54
55 These functions are built in to beets:
56
57 - ``%lower{text}``: Convert ``text`` to lowercase.
58 - ``%upper{text}``: Convert ``text`` to UPPERCASE.
59 - ``%capitalize{text}``: Make the first letter of ``text`` UPPERCASE and the
60 rest lowercase.
61 - ``%title{text}``: Convert ``text`` to Title Case.
62 - ``%left{text,n}``: Return the first ``n`` characters of ``text``.
63 - ``%right{text,n}``: Return the last ``n`` characters of ``text``.
64 - ``%if{condition,text}`` or ``%if{condition,truetext,falsetext}``: If
65 ``condition`` is not empty, and not one of the values ``0`` or ``false`` (case
66 insensitive), then returns the second argument. Otherwise, returns the third
67 argument if specified (or nothing if ``falsetext`` is left off).
68 - ``%asciify{text}``: Convert non-ASCII characters to their ASCII equivalents.
69 For example, "café" becomes "cafe". Uses the mapping provided by the
70 `unidecode module`_. See the :ref:`asciify-paths` configuration option.
71 - ``%aunique{identifiers,disambiguators,brackets}``: Provides a unique string to
72 disambiguate similar albums in the database. See :ref:`aunique`, below.
73 - ``%sunique{identifiers,disambiguators,brackets}``: Similarly, a unique string
74 to disambiguate similar singletons in the database. See :ref:`sunique`, below.
75 - ``%time{date_time,format}``: Return the date and time in any format accepted
76 by strftime_. For example, to get the year some music was added to your
77 library, use ``%time{$added,%Y}``.
78 - ``%first{text,count,skip,sep,join}``: Extract a subset of items from a
79 delimited string. Splits ``text`` by ``sep``, skips the first ``skip`` items,
80 then returns the next ``count`` items joined by ``join``.
81
82 This is especially useful for multi-valued fields like ``artists``,
83 ``genres``, ``remixers``, ``lyricists``, ``composers``, or ``arrangers`` where
84 you may only want the first value or a limited number of values in a path.
85
86 Defaults:
87
88 ..
89 Comically, you need to follow |semicolon_space| with some punctuation to
90 make sure it gets rendered correctly as '; ' in the docs.
91
92 - **count**: 1,
93 - **skip**: 0,
94 - **sep**: |semicolon_space|,
95 - **join**: |semicolon_space|.
96
97 Examples:
98
99 ::
100
101 %first{$genres} → returns the first genre
102 %first{$genres,2} → returns the first two genres, joined by "; "
103 %first{$genres,2,1} → skips the first genre, returns the next two
104 %first{$genres,2,0, , -> } → splits by space, joins with " -> "
105
106 - ``%ifdef{field}``, ``%ifdef{field,truetext}`` or
107 ``%ifdef{field,truetext,falsetext}``: Checks if an flexible attribute
108 ``field`` is defined. If it exists, then return ``truetext`` or ``field``
109 (default). Otherwise, returns ``falsetext``. The ``field`` should be entered
110 without ``$``. Note that this doesn't work with built-in :ref:`itemfields`, as
111 they are always defined.
112
113 .. _strftime: https://docs.python.org/3/library/time.html#time.strftime
114
115 .. _unidecode module: https://pypi.org/project/Unidecode
116
117 Plugins can extend beets with more template functions (see
118 :ref:`templ_plugins`).
119
120 .. _aunique:
121
122 Album Disambiguation
123 --------------------
124
125 Occasionally, bands release two albums with the same name (c.f. Crystal Castles,
126 Weezer, and any situation where a single has the same name as an album or EP).
127 Beets ships with special support, in the form of the ``%aunique{}`` template
128 function, to avoid placing two identically-named albums in the same directory on
129 disk.
130
131 The ``aunique`` function detects situations where two albums have some identical
132 fields and emits text from additional fields to disambiguate the albums. For
133 example, if you have both Crystal Castles albums in your library, ``%aunique{}``
134 will expand to "[2008]" for one album and "[2010]" for the other. The function
135 detects that you have two albums with the same artist and title but that they
136 have different release years.
137
138 For full flexibility, the ``%aunique`` function takes three arguments. The first
139 two are whitespace-separated lists of album field names: a set of *identifiers*
140 and a set of *disambiguators*. The third argument is a pair of characters used
141 to surround the disambiguator.
142
143 Any group of albums with identical values for all the identifiers will be
144 considered "duplicates". Then, the function tries each disambiguator field,
145 looking for one that distinguishes each of the duplicate albums from each other.
146 The first such field is used as the result for ``%aunique``. If no field
147 suffices, an arbitrary number is used to distinguish the two albums.
148
149 The default identifiers are ``albumartist album`` and the default disambiguators
150 are ``albumtype year label catalognum albumdisambig releasegroupdisambig``. So
151 you can get reasonable disambiguation behavior if you just use ``%aunique{}``
152 with no parameters in your path forms (as in the default path formats), but you
153 can customize the disambiguation if, for example, you include the year by
154 default in path formats.
155
156 The default characters used as brackets are ``[]``. To change this, provide a
157 third argument to the ``%aunique`` function consisting of two characters: the
158 left and right brackets. Or, to turn off bracketing entirely, leave argument
159 blank.
160
161 One caveat: When you import an album that is named identically to one already in
162 your library, the *first* album—the one already in your library— will not
163 consider itself a duplicate at import time. This means that ``%aunique{}`` will
164 expand to nothing for this album and no disambiguation string will be used at
165 its import time. Only the second album will receive a disambiguation string. If
166 you want to add the disambiguation string to both albums, just run ``beet move``
167 (possibly restricted by a query) to update the paths for the albums.
168
169 .. _sunique:
170
171 Singleton Disambiguation
172 ------------------------
173
174 It is also possible to have singleton tracks with the same name and the same
175 artist. Beets provides the ``%sunique{}`` template to avoid giving these tracks
176 the same file path.
177
178 It has the same arguments as the :ref:`%aunique <aunique>` template, but the
179 default values are different. The default identifiers are ``artist title`` and
180 the default disambiguators are ``year trackdisambig``.
181
182 Syntax Details
183 --------------
184
185 The characters ``$``, ``%``, ``{``, ``}``, and ``,`` are "special" in the path
186 template syntax. This means that, for example, if you want a ``%`` character to
187 appear in your paths, you'll need to be careful that you don't accidentally
188 write a function call. To escape any of these characters (except ``{``, and
189 ``,`` outside a function argument), prefix it with a ``$``. For example, ``$$``
190 becomes ``$``; ``$%`` becomes ``%``, etc. The only exceptions are:
191
192 - ``${``, which is ambiguous with the variable reference syntax (like
193 ``${title}``). To insert a ``{`` alone, it's always sufficient to just type
194 ``{``. You do, however need to use ``$`` to escape a closing brace ``$}``.
195 - commas are used as argument separators in function calls. Inside of a
196 function's argument, use ``$,`` to get a literal ``,`` character. Outside of
197 any function argument, escaping is not necessary: ``,`` by itself will produce
198 ``,`` in the output.
199
200 If a value or function is undefined, the syntax is simply left unreplaced. For
201 example, if you write ``$foo`` in a path template, this will yield ``$foo`` in
202 the resulting paths because "foo" is not a valid field name. The same is true of
203 syntax errors like unclosed ``{}`` pairs; if you ever see template syntax
204 constructs leaking into your paths, check your template for errors.
205
206 If an error occurs in the Python code that implements a function, the function
207 call will be expanded to a string that describes the exception so you can debug
208 your template. For example, the second parameter to ``%left`` must be an
209 integer; if you write ``%left{foo,bar}``, this will be expanded to something
210 like ``<ValueError: invalid literal for int()>``.
211
212 .. _itemfields:
213
214 Available Values
215 ----------------
216
217 Here's a list of the different values available to path formats. The current
218 list can be found definitively by running the command ``beet fields``. Note that
219 plugins can add new (or replace existing) template values (see
220 :ref:`templ_plugins`).
221
222 Ordinary metadata:
223
224 - title
225 - artist
226 - artist_sort: The "sort name" of the track artist (e.g., "Beatles, The" or
227 "White, Jack").
228 - artist_credit: The track-specific `artist credit`_ name, which may be a
229 variation of the artist's "canonical" name.
230 - artists: The track artists as a multi-valued field.
231 - album
232 - albumartist: The artist for the entire album, which may be different from the
233 artists for the individual tracks.
234 - albumartists: The album artists as a multi-valued field.
235 - albumartist_sort
236 - albumartist_credit
237 - genre
238 - genres: The track genres as a multi-valued field.
239 - arrangers: The track arrangers as a multi-valued field.
240 - composers: The track composers as a multi-valued field.
241 - lyricists: The track lyricists as a multi-valued field.
242 - remixers: The track remixers as a multi-valued field.
243 - grouping
244 - subtitle
245 - year, month, day: The release date of the specific release.
246 - original_year, original_month, original_day: The release date of the original
247 version of the album.
248 - track
249 - tracktotal
250 - disc
251 - disctotal
252 - lyrics
253 - comments
254 - bpm
255 - comp: Compilation flag.
256 - albumtype: The MusicBrainz album type; the MusicBrainz wiki has a `list of
257 type names`_.
258 - label
259 - asin
260 - catalognum
261 - script
262 - language
263 - country
264 - albumstatus
265 - media
266 - albumdisambig
267 - disctitle
268 - encoder
269
270 .. _artist credit: https://wiki.musicbrainz.org/Artist_Credit
271
272 .. _list of type names: https://musicbrainz.org/doc/Release_Group/Type
273
274 Audio information:
275
276 - length (in seconds)
277 - bitrate (in kilobits per second, with units: e.g., "192kbps")
278 - bitrate_mode (e.g., "CBR", "VBR" or "ABR", only available for the MP3 format)
279 - encoder_info (e.g., "LAME 3.97.0", only available for some formats)
280 - encoder_settings (e.g., "-V2", only available for the MP3 format)
281 - format (e.g., "MP3" or "FLAC")
282 - channels
283 - bitdepth (only available for some formats)
284 - samplerate (in kilohertz, with units: e.g., "48kHz")
285
286 MusicBrainz and fingerprint information:
287
288 - mb_trackid
289 - mb_releasetrackid
290 - mb_albumid
291 - mb_artistid
292 - mb_albumartistid
293 - mb_releasegroupid
294 - acoustid_fingerprint
295 - acoustid_id
296
297 Library metadata:
298
299 - mtime: The modification time of the audio file.
300 - added: The date and time that the music was added to your library.
301 - path: The item's filename.
302
303 .. _templ_plugins:
304
305 Template functions and values provided by plugins
306 -------------------------------------------------
307
308 Beets plugins can provide additional fields and functions to templates. See the
309 :doc:`/plugins/index` page for a full list of plugins. Some plugin-provided
310 constructs include:
311
312 - ``$missing`` by :doc:`/plugins/missing`: The number of missing tracks per
313 album.
314 - ``$album_artist_no_feat`` by :doc:`/plugins/ftintitle`: The album artist
315 without any featured artists
316 - ``%bucket{text}`` by :doc:`/plugins/bucket`: Substitute a string by the range
317 it belongs to.
318 - ``%the{text}`` by :doc:`/plugins/the`: Moves English articles to ends of
319 strings.
320 - ``%titlecase{text}`` by :doc:`/plugins/titlecase`: Format text according to
321 title case guidelines.
322
323 The :doc:`/plugins/inline` lets you define template fields in your beets
324 configuration file using Python snippets. And for more advanced processing, you
325 can go all-in and write a dedicated plugin to register your own fields and
326 functions (see :ref:`basic-plugin-setup`).