You are not logged in.

#1 2010-11-11 02:55:50

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

pytagwrite - easily write ID3 tags to your songs

So, I eventually got fed up of manually typing in the song titles and artists in ncmpcpp. I mean, the title is the same as the file name, right?

#!/usr/bin/env python2
import os
import sys

from mutagen.id3 import ID3
from mutagen.easyid3 import EasyID3
import mutagen

for root, dirs, files in os.walk(sys.argv[1]):
    if root == sys.argv[1]:
        continue

    for f in files:
        try:
            song = EasyID3(os.path.join(root, f))
        except mutagen.id3.error:
            song = ID3()
            song.save(f)
            break

        song['title'] = f[:-4]
        song['artist'] = root.split('/')[-1]
        song.save()

Basically, it parses through your music folder. For every mp3 that has an ID3 header, it writes the title of the song, which is the file name with the extension stripped off, and the artist, which is the top directory name.

For example:

/home/brad/Music/Pink Floyd/Wish You Were Here.mp3

would become...

Title: Wish You Were Here
Artist: Pink Floyd

Dependencies:
* mutagen (mutagen-svn in the AUR)

NOTE: My python habits...err...suck. Therefore, there are probably many things that should be changed. Please do mention those. wink

EDIT (11/11/10): Updated script. Many thanks to EnvoyRising. smile

Last edited by cesura (2010-11-11 23:01:18)

Offline

#2 2010-11-11 07:55:12

EnvoyRising
Member
Registered: 2008-08-08
Posts: 118

Re: pytagwrite - easily write ID3 tags to your songs

1. Using sys.argv would allow the user to pass in a music directory instead of having it hard-coded
2. Use of os.walk might be more straight forward.

#!/usr/bin/env python2
import os
import sys

from mutagen.id3 import ID3
from mutagen.easyid3 import EasyID3
import mutagen

for root, dirs, files in os.walk(sys.argv[1]):
    if root == sys.argv[1]:
        continue

    for f in files:
        try:
            song = EasyID3(os.path.join(root, f))
        except mutagen.id3.error:
            song = ID3()
            song.save(f)
            break

        song['title'] = f[:-4]
        song['artist'] = root.split('/')[1]
        song.save()
  

That said, I didn't have much use for the script since i have my music organized as Artist/Album/## Track . Either way, this was kind of fun to hack on.

Last edited by EnvoyRising (2010-11-11 07:58:37)

Offline

#3 2010-11-11 23:03:16

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: pytagwrite - easily write ID3 tags to your songs

EnvoyRising wrote:

1. Using sys.argv would allow the user to pass in a music directory instead of having it hard-coded
2. Use of os.walk might be more straight forward.

Thank you very much! smile

EnvoyRising wrote:

That said, I didn't have much use for the script since i have my music organized as Artist/Album/## Track .

Yeah, I kind of figured that. This was more of a "I made this because so and so pissed me off. Here it is, do what you want with it" sort of thing.

Last edited by cesura (2010-11-11 23:04:08)

Offline

#4 2010-11-17 07:19:33

EnvoyRising
Member
Registered: 2008-08-08
Posts: 118

Re: pytagwrite - easily write ID3 tags to your songs

Ah, definitely had a few of those moments myself. Cool thing about python is that you can vent during those moments with relative ease -- no compile cycle. Perhaps bash would be better simply because of how ubiquitous it is, but I never got used to its syntax.

Offline

Board footer

Powered by FluxBB