Audio Tags editor script

From Vifm Wiki
Revision as of 22:05, 25 May 2021 by Viminator25 (talk | contribs) (Fix Comma Rules)
Jump to navigation Jump to search

This scripts allows one to edit the metadata of audio files using mutagen.

It takes the selected files and spits out a temporary csv file of the artist, album, and song title in that order. You then make your changes and write to the file to apply them. This script will not delete any info except for the stuff you overwrite. It simply updates the fields that are shown to the user.

If you want to edit other attributes of the file, then it's very easy to do so by changing a single variable yourself.

Source Code

#!/usr/bin/env python3

import sys
import os
import tempfile
import csv
import mutagen

# Options
editor = os.environ['EDITOR']
attrs = ['artist', 'album', 'title']
escape = '*'

songs = [] # List of mutagen objects
song_attrs = [] # Dictionaries containing attributes to be inspected

# Make sure mutagen uses the easy version, as
# it uses the normal one by default
if os.path.splitext(sys.argv[1])[1] == ".mp3":
    import mutagen.easyid3
    mut_init = mutagen.easyid3.EasyID3
    mut_init = mutagen.File

for f in sys.argv[1:]:
    for attr in attrs:
        song_attrs[-1][attr] = songs[-1].get(attr, [''])[0]
        if attr == 'artist' and not song_attrs[-1][attr]:
            del song_attrs[-1][attr]
            _attr = 'albumartist'
            song_attrs[-1][attr] = songs[-1].get(_attr, [''])[0]

with tempfile.NamedTemporaryFile('w+', suffix='.csv') as tmpf:
    csvwriter = csv.writer(tmpf, quoting=csv.QUOTE_NONE, escapechar=escape)
    csvwriter.writerows([row.values() for row in song_attrs])
    # Ensure that the file has been written to disk
    os.system("{} {}".format(editor,
    csvreader = csv.reader(tmpf, quoting=csv.QUOTE_NONE, escapechar=escape)
    for song,row in zip(songs, csvreader):
        new_song_metadata = dict(zip(attrs, row))


Pyhton 3 is required. You will also need the mutagen python package which can be installed using pip: sudo pip install mutagen, or you can probably obtain it through your package manager.

Save the file as in either your $PATH or your $VIFM/scripts directory. Make sure that the file is executable. Then place the following into your vifm rc:

filetype {*.flac,*.mp3,*.ogg,*.m4a} %f


Aligning Entries

It's much easier to work with csv files when the commas are aligned. If you use vim, then this is very easy to do with the easy-align plugin. Place the following into your vim rc file once installed:

augroup csv
autocmd BufWritePre *.csv %EasyAlign *, {'a': 'r'}
autocmd BufReadPost *.csv %EasyAlign *, {'a': 'r'}
augroup end

This will auto-align the commas.

Fields with Commas in Them

If a field has a comma in it, then escape that character with an asterisk. A field with an asterisk in it will show up as a double asterisk **

For example, output for the first song of Never, Neverland by Annihilator will show up like this:

Annihilator, Never*, Neverland, The Fun Palace

If for whatever reason you want to change the escape character you can do so in the source.